当前位置:网站首页>闭包的常见问题
闭包的常见问题
2022-06-27 09:55:00 【Fx_cap】
定义:
闭包: 函数A嵌套函数B,函数B使用函数A的变量
场景:
- 函数作为返回值
- 函数作为参数
- 函数嵌套
- 事件处理的回调(异步执行)
// 闭包的使用场景
// 1、函数作为返回值
function mail() {
let content = '信';
return function () {
console.log(content);
};
}
const envelop = mail();
envelop();
// 2、函数作为参数
let count = 0;
function envelop(fn) {
count = 1;
fn();
}
function mail() {
console.log(count);
}
envelop(mail);
// 3、函数嵌套
let count = 0;
function outerFn() {
function innerFn() {
count++;
console.log(count);
}
return innerFn;
}
outerFn()();
// 4、事件处理回调(异步执行)
let lis = document.querySelectorAll('li');
for (var i = 0; i < lis.length; i++) {
(function (i) {
lis[i].addEventListener('click', function () {
console.log(i);
});
})(i);
}常见面试的追问
- 立即执行嵌套
- 当立即执行函数遇到块级作用域
- 拆分执行
- 实现私有变量
// 立即执行嵌套
(function immediateA(a) {
return (function immediateB(b) {
console.log(a);
})(1);
})(0);
// 当立即执行函数遇到块级作用域;
let count = 0;
(function immediate() {
if (count === 0) {
let count = 1;
console.log(count);
}
console.log(count);
})();
// 拆分执行;
function createIncrement() {
let count = 0;
function increment() {
count++;
}
let message = `count is ${count}`;
// console.log('======') //只执行一次
function log() {
console.log(message);
}
return [increment, log];
}
const [increment, log] = createIncrement();
increment();
increment();
increment();
log();
// 实现私有变量;
function createStack() {
const items = [];
return {
push(item) {
items.push(item);
// console.log(items);
},
};
}
const res = createStack();
res.push(1);边栏推荐
- 【OpenCV 例程200篇】211. 绘制垂直矩形
- Oracle连接MySQL报错IM002
- es 根据索引名称和索引字段更新值
- 新旧两个界面对比
- Arduino PROGMEM静态存储区的使用介绍
- DNS standby server information, DNS server address (how many DNS preferred and standby are filled in)
- MySQL proficient-01 addition, deletion and modification
- Location and solution of network connection failure of primary online mobile terminal Report
- 不容置疑,这是一个绝对精心制作的项目
- C语言学习-Day_04
猜你喜欢

手机影像内卷几时休?

This application failed to start because it could not find or load the QT platform plugin

通俗易懂理解樸素貝葉斯分類的拉普拉斯平滑

不容置疑,这是一个绝对精心制作的项目

Source insight 工具安装及使用方法
![leetcode:522. Longest special sequence II [greed + subsequence judgment]](/img/43/9b17e9cb5fee9d14c2986a2141889d.png)
leetcode:522. Longest special sequence II [greed + subsequence judgment]

. Net
![[200 opencv routines] 212 Draw a slanted rectangle](/img/cf/da8fff386d011c939946326c55671f.png)
[200 opencv routines] 212 Draw a slanted rectangle

你睡觉时大脑真在自动学习!首个人体实验证据来了:加速1-4倍重放,深度睡眠阶段效果最好...

.NET 中的引用程序集
随机推荐
Scientists develop two new methods to provide stronger security protection for intelligent devices
oracle触发器 存储过程同时写入
通俗易懂理解樸素貝葉斯分類的拉普拉斯平滑
std::memory_order_seq_cst内存序
运维一线工作常用shell脚本再整理
Easy to understand Laplace smoothing of naive Bayesian classification
C语言学习-Day_04
JS 客户端存储
Multi thread implementation rewrites run (), how to inject and use mapper file to operate database
Advantages and disadvantages of distributed file storage system
三层架构中,数据库的设计在哪一层实现,不是在数据存储层吗?
Apache POI的读写
C语言学习-Day_05
前馈-反馈控制系统设计(过程控制课程设计matlab/simulink)
基于STM32设计的蓝牙健康管理设备
如何获取GC(垃圾回收器)的STW(暂停)时间?
Arduino PROGMEM静态存储区的使用介绍
[200 opencv routines] 212 Draw a slanted rectangle
Win10快捷键整理
torchvision.models._utils.IntermediateLayerGetter使用教程