当前位置:网站首页>Frequently asked questions about closures
Frequently asked questions about closures
2022-06-27 10:11:00 【Fx_ cap】
Definition :
Closure : function A Nested function B, function B Using functions A The variable of
scene :
- Function as return value
- Function as parameter
- Nested function
- Callback for event handling ( Asynchronous execution )
// The use of closures
// 1、 Function as return value
function mail() {
let content = ' Letter ';
return function () {
console.log(content);
};
}
const envelop = mail();
envelop();
// 2、 Function as parameter
let count = 0;
function envelop(fn) {
count = 1;
fn();
}
function mail() {
console.log(count);
}
envelop(mail);
// 3、 Nested function
let count = 0;
function outerFn() {
function innerFn() {
count++;
console.log(count);
}
return innerFn;
}
outerFn()();
// 4、 Event handling callback ( Asynchronous execution )
let lis = document.querySelectorAll('li');
for (var i = 0; i < lis.length; i++) {
(function (i) {
lis[i].addEventListener('click', function () {
console.log(i);
});
})(i);
}Common interview questions
- Perform nesting now
- When the immediate execution function encounters a block level scope
- Split execution
- Implement private variables
// Perform nesting now
(function immediateA(a) {
return (function immediateB(b) {
console.log(a);
})(1);
})(0);
// When the immediate execution function encounters a block level scope ;
let count = 0;
(function immediate() {
if (count === 0) {
let count = 1;
console.log(count);
}
console.log(count);
})();
// Split execution ;
function createIncrement() {
let count = 0;
function increment() {
count++;
}
let message = `count is ${count}`;
// console.log('======') // Only once
function log() {
console.log(message);
}
return [increment, log];
}
const [increment, log] = createIncrement();
increment();
increment();
increment();
log();
// Implement private variables ;
function createStack() {
const items = [];
return {
push(item) {
items.push(item);
// console.log(items);
},
};
}
const res = createStack();
res.push(1);边栏推荐
- 小哥凭“量子速读”绝技吸粉59万:看街景图0.1秒,“啪的一下”在世界地图精准找到!...
- Curiosity mechanism in reinforcement learning
- 运维一线工作常用shell脚本再整理
- 测试同学怎么参与codereview
- R语言plotly可视化:plotly可视化基础小提琴图(basic violin plot in R with plotly)
- The R language uses the preprocess function of the caret package for data preprocessing: Center all data columns (subtract the average value from each data column), and set the method parameter to cen
- Freemarker
- leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】
- js 所有的网络请求方式
- 感应电机直接转矩控制系统的设计与仿真(运动控制matlab/simulink)
猜你喜欢
![leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]](/img/70/3954b0871cc31d24ae016eb99d871e.png)
leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!

以后发现漏洞,禁止告诉中国!

通俗易懂理解朴素贝叶斯分类的拉普拉斯平滑

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

Decompile the jar package and recompile it into a jar package after modification

详解各种光学仪器成像原理

oracle触发器 存储过程同时写入

Comparison between new and old interfaces

产品力对标海豹/Model 3,长安深蓝SL03预售17.98万起
随机推荐
C language learning day_ 06
详解各种光学仪器成像原理
12 necessary tools for network engineers
文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
CPU design (single cycle and pipeline)
【OpenCV 例程200篇】212. 绘制倾斜的矩形
mongodb跨主机数据库拷贝以及常用命令
通俗易懂理解樸素貝葉斯分類的拉普拉斯平滑
Flutter wechat sharing
lvi-sam 总结
[从零开始学习FPGA编程-47]:视野篇 - 第三代半导体技术现状与发展趋势
小哥凭“量子速读”绝技吸粉59万:看街景图0.1秒,“啪的一下”在世界地图精准找到!...
用户认证技术
R语言plotly可视化:可视化多个数据集归一化直方图(historgram)并在直方图中添加密度曲线kde、设置不同的直方图使用不同的分箱大小(bin size)、在直方图的底部边缘添加边缘轴须图
For a moment, the ban of the US e-cigarette giant has been postponed, and products can be sold in the US for the time being
[registration] infrastructure design: from architecture hot issues to industry changes | tf63
leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】
Border affects the height of the parent element - solution
Arduino PROGMEM静态存储区的使用介绍