当前位置:网站首页>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);边栏推荐
- C language learning day_ 06
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.1 生成树协议(Spanning Tree Protocol (STP))
- User authentication technology
- Advantages and disadvantages of distributed file storage system
- 新旧两个界面对比
- DNS standby server information, DNS server address (how many DNS preferred and standby are filled in)
- C语言学习-Day_05
- 多线程实现 重写run(),怎么注入使用mapper文件操作数据库
- 10 common website security attack means and defense methods
- BufferedWriter 和 BufferedReader 的使用
猜你喜欢

Bluetooth health management device based on stm32

Record in detail the implementation of yolact instance segmentation ncnn

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

audiotrack与audioflinger

leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】

When does the mobile phone video roll off?

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

手机影像内卷几时休?

Une compréhension facile de la simplicité de la classification bayésienne du lissage laplacien

Comparison between new and old interfaces
随机推荐
Only one confirmcallback is supported by each rabbittemplate
[noodle classic] Yunze Technology
浅析基于边缘计算的移动AR实现(中)
12 necessary tools for network engineers
Quartz (timer)
torchvision.models._utils.IntermediateLayerGetter使用教程
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
[STM32] Hal library stm32cubemx tutorial 12 - IIC (read AT24C02)
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
3D移动 translate3d
反编译jar包,修改后重新编译为jar包
以后发现漏洞,禁止告诉中国!
Prometheus alarm process and related time parameter description
6月23日《Rust唠嗑室》第三期B站视频地址
Decompile the jar package and recompile it into a jar package after modification
Xiaobai can also understand how the basic network 03 | OSI model works (classic push)
torchvision. models._ utils. Intermediatelayergetter tutorial
2021 CSP J2入门组 CSP-S2提高组 第2轮 视频与题解
强化学习中好奇心机制
C language learning day_ 06