当前位置:网站首页>面试复习之---闭包
面试复习之---闭包
2022-06-11 09:51:00 【这是一个大人物】
一、闭包是什么?
首先,我们要明确闭包的概念,这里引用百度百科的概念
闭包就是能够读取其他函数内部变量的函数。例如在javascript中,只有函数内部的子函数才能读取局部变量,所以闭包可以理解成“定义在一个函数内部的函数“。在本质上,闭包是将函数内部和函数外部连接起来的桥梁。
[1]
简而言之,就是包在函数里面的函数,形成一个闭环,里面的函数可以访问到父函数的变量,函数可以作为另一个函数的返回值,也可以作为参数
- 第一种,作为参数
function fn1(fn){
const a = 100
fn()
}
const a = 200
function fn(){
console.log(a)
}
fn1(fn)
-第二种作为返回值
function demo(){
let a = 100
return function(){
console.log(a)
}
}
let a = 200
demo()
二、手写一个简单的闭包
代码如下(示例):
function sum(a){
return function sum2(b){
return a+b
}
}
var l=sum(1)(2) //传参
console.log(l);
2.小案例
代码如下(示例):
错误示范案例
function sum() {
//var没有自己的作用域块,所以每一循环都会覆盖前面的值
for (var index = 0; index < 5; index++) {
//定定时器循环是异步编程, 每一轮循环设置定时器, 无需等定时器触发执行, 继续下一轮循环。
// (定时器触发的时候,循环已经结束); 5次循环都已经结束了,
// 设置了5次定时器, 循环已经结束, 定时器还没有开始。
// 循环结束 i = 5;
setTimeout(() => {
console.log(index);
}, 1000);
}
}
var l = sum()
console.log(l); //输出5 5 5 5 ,因为这里的index用的是var ,
//每次循环不会保存相应的值,可以换成let块级变量,就会输出1 2 3 4
//这是闭包的写法
function sum() {
//var没有自己的作用域块,所以每一循环都会覆盖前面的值
for (var index = 1; index < 5; index++) {
//嵌套函数
(function(j){
setTimeout(function a(){
console.log(j);
}, j*1000)
})(index)
//这里的index是记录的每次循环的值,会赋值给j
}
}
var l = sum()
console.log(l);
//输出1 2 3 4
//用let来实现,let有自己的作用域块,将重新绑定到每一次的迭代中,每次循环都会重新赋值
function sum() {
for (let index = 1; index < 5; index++) {
//嵌套函数
setTimeout(function a(){
console.log(index);
}, index*1000)
//这里的index是记录的每次循环的值,会赋值给j
}
}
var l = sum()
console.log();
//输出1 2 3 4
总结
闭包很简单,认真分析总会能行的
边栏推荐
- 鼠标点击坐标转换生成
- Oracle DG physical standby database uses alias data file to change path to OMF path
- mysql基础学习笔记03
- ESP8266_GET请求天气预报、json解析
- About prototype and prototype chain
- 一万字彻底学会堆和二叉树
- Esp8266 Smartconfig
- CVE-2021-40449 NtGdiResetDC UAF
- Chemical composition of q355hr steel plate
- ESP8266_ Access to the core suite of Baidu Internet of things and use mqtt protocol for communication
猜你喜欢

What hydraulic oil is used for Denison hydraulic pump? What are the requirements

Beginning简约博客emlog主题模板V3

Document object

CISP-PTE XSS基础

An error can't locate env pm in @INC

ESP8266_ Access to the core suite of Baidu Internet of things and use mqtt protocol for communication

An error will be reported when the RAC modifies the scanip to different network segments

Troubleshooting the error ora-12545 reported by scanip in Oracle RAC

图片规则翻页

js中关键字this的理解
随机推荐
Beginning简约博客emlog主题模板V3
Oracle 11g RAC disk group has space and cannot add data files?
BeanFactory 与FactoryBean的区别
JS foundation -- Operator
Oracle XDB组件的重建
Where is it safer to open an account for soda ash futures? How much can soda ash futures do now?
How much do you know about software compatibility testing? How to select a software compatibility testing organization?
锐意进取,砥砺前行,JASMINER持续深耕品牌实力
Events in JS
ORACLE DG物理备库使用别名数据文件改变路径到OMF路径
ESP8266_SNTP(Simple Network Time Protocol)
图表背后的秘密 | 技术指标讲解:唐奇安通道
我们是如何连上WiFi的?
整型提升例题
Esp8266 Smartconfig
What hydraulic oil is used for Denison hydraulic pump? What are the requirements
[image denoising] image denoising based on median + wavelet + Wiener + filter, including Matlab source code
【音视频】SEI简介
Finish C language
Integer lifting example