当前位置:网站首页>Interview review - closure
Interview review - closure
2022-06-11 09:59:00 【This is a big shot】
List of articles
One 、 What is a closure ?
First , We need to define the concept of closure , The concept of Baidu Encyclopedia is quoted here
Closures are functions that can read internal variables of other functions . For example, in javascript in , Only subfunctions inside a function can read local variables , So closures can be understood as “ A function defined inside a function “. In essence , Closures are bridges that connect functions inside and outside .
[1]
In short , Is the function wrapped in the function , Form a closed loop , The function inside can access the variables of the parent function , Function can be used as the return value of another function , It can also be used as a parameter
- The first one is , As a parameter
function fn1(fn){
const a = 100
fn()
}
const a = 200
function fn(){
console.log(a)
}
fn1(fn)
- The second is the return value
function demo(){
let a = 100
return function(){
console.log(a)
}
}
let a = 200
demo()
Two 、 Write a simple closure
The code is as follows ( Example ):
function sum(a){
return function sum2(b){
return a+b
}
}
var l=sum(1)(2) // The ginseng
console.log(l);
2. Small cases
The code is as follows ( Example ):
Error demonstration cases
function sum() {
//var Does not have its own scope block , So each loop will overwrite the previous values
for (var index = 0; index < 5; index++) {
// Timer cycles are asynchronous programming , Set the timer for each cycle , There is no need to wait for the timer to trigger execution , Continue the next cycle .
// ( When the timer triggers , The cycle is over ); 5 The second cycle is over ,
// Set up 5 Sub timer , The cycle is over , The timer hasn't started yet .
// The loop ends i = 5;
setTimeout(() => {
console.log(index);
}, 1000);
}
}
var l = sum()
console.log(l); // Output 5 5 5 5 , Because of the index It's using var ,
// Each cycle does not save the corresponding value , Can be replaced by let Block level variables , Will be output 1 2 3 4
// This is how to write a closure
function sum() {
//var Does not have its own scope block , So each loop will overwrite the previous values
for (var index = 1; index < 5; index++) {
// Nested function
(function(j){
setTimeout(function a(){
console.log(j);
}, j*1000)
})(index)
// there index Is the value of each cycle recorded , Will be assigned to j
}
}
var l = sum()
console.log(l);
// Output 1 2 3 4
// use let To achieve ,let Has its own scope block , Rebinding to each iteration , Each cycle will be reassigned
function sum() {
for (let index = 1; index < 5; index++) {
// Nested function
setTimeout(function a(){
console.log(index);
}, index*1000)
// there index Is the value of each cycle recorded , Will be assigned to j
}
}
var l = sum()
console.log();
// Output 1 2 3 4
summary
Closures are simple , Careful analysis will always work
边栏推荐
猜你喜欢

Integer lifting example

ESP8266_MQTT协议

With determination to forge ahead, JASMINER continues to deepen its brand strength

Rebuilding Oracle XdB components

What are the functions and applications of Mogg test controller

BeanFactoryPostProcessor 与BeanPostProcessor的区别

Chemical composition of q355hr steel plate

关于马格齿轮泵的应用领域都有哪些?总结一下

What are the functions and functions of the EMG actuator

图片规则翻页
随机推荐
New feature in ES6 -- arrow function
C+每日练题(15)
Esp8266 Smartconfig
mysql基础学习笔记03
steamVR简介
不同CV任务的标注类型
【torch】: 并行训练并且可以动态设置第一个gpu的batch size
12.5 concurrent search + violent DFS - [discovery ring]
ESP8266_ Connect to Alibaba cloud through mqtt protocol
等待事件 enq: KO - fast object checkpoint可行的一些处理方法
Interface, abstract class and relationship between classes
什么是数字孪生?一个实时而虚拟的表现形式
P4147 "jade toad Palace"
Tenthousand words thoroughly learn heap and binary tree
你对软件兼容性测试知道多少?如何选择软件兼容性测试机构?
RSA签名问题
What are the functions and functions of the EMG actuator
对于力士乐电磁换向阀的功能与作用,你知道多少
Oracle XDB組件的重建
What are the application fields of MAG gear pump? To sum up