当前位置:网站首页>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);边栏推荐
猜你喜欢

Easy to understand Laplace smoothing of naive Bayesian classification

2-4Kali下安装nessus

Your brain is learning automatically when you sleep! Here comes the first human experimental evidence: accelerate playback 1-4 times, and the effect of deep sleep stage is the best

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

感应电机直接转矩控制系统的设计与仿真(运动控制matlab/simulink)

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

Product strength benchmarking seal /model 3, with 179800 pre-sales of Chang'an dark blue sl03

【报名】基础架构设计:从架构热点问题到行业变迁 | TF63

LVI Sam summary
随机推荐
【云享新鲜】社区周刊·Vol.68-华为云招募工业智能领域合作伙伴,强力扶持+商业变现
C language learning day_ 06
【OpenCV 例程200篇】211. 绘制垂直矩形
JS 文件上传下载
C any() and aii() methods
torch.utils.data.RandomSampler和torch.utils.data.SequentialSampler的区别
Basic violin plot in R with plot
【SO官方采访】为何使用Rust的开发者如此深爱它
C语言学习-Day_06
dns备用服务器信息,dns服务器地址(dns首选和备用填多少)
Your brain is learning automatically when you sleep! Here comes the first human experimental evidence: accelerate playback 1-4 times, and the effect of deep sleep stage is the best
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
队列,双向队列,及其运用
Google browser chropath plug-in
unity--newtonsoft.json解析
leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】
强化学习中好奇心机制
一次线上移动端报表网络连接失败问题定位与解决
torchvision. models._ utils. Intermediatelayergetter tutorial
C language learning day_ 05