当前位置:网站首页>JS closure knowledge points essence
JS closure knowledge points essence
2022-07-03 21:58:00 【Madrid Tianxin】
//1. What is a closure ? Method is called inside the method
//2. The meaning of closures --1. Extend the life cycle of variables 2. Create a private environment
// Scope chain Find the rules : Nearby principle
let a = 1;
function fn1(){
let b = 2;
console.log(a);
}
// console.log(b)
fn1();Pictured a Global variable ,b It's a local variable . So you can get it anywhere a Value , And outside the method ( Outside closure ) You can't get b Value , So the output is 1.
// Bridge internal and external methods
function outer(){
let a1 = 111;
let a2 = 222;
return function inner(){
return a1;
}
}
function fn5(){
let getInnerdata = outer();
console.dir(getInnerdata);
}
fn5();
// Closure will reside in memory => Be careful with closures There are closures in the domain only return One. a1, therefore closure Only a1.
It will always exist dir in , So it will be resident in memory , So be careful with closures

let makeCount = function(){
let num = 0 ;
function changeBy(val){
num+=val;
}
return{
add:function(){
changeBy(1);
},
reduce:function(){
changeBy(-1);
},
value:function(){
return num;
}
}
}
let count1 = makeCount();
let count2 = makeCount();
count1.add()
count1.add()
count2.add()
console.log(count1.value());
console.log(count2.value());The output of the above code is count1 Of value by 2;count2 Of value by 1.
It can be explained in this way , Although they are all in the same way , But they don't interfere with each other . It's like a father with two sons , But the two sons have their own families , Independent relationship . so count1 Two more times ,count2 The addition is a recalculation .
边栏推荐
- Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
- MySQL -- standardize database design
- Control loop of program (while loop)
- Cognitive fallacy: what is dimensional curse
- 常用sql集合
- Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
- Decompile and modify the non source exe or DLL with dnspy
- [vulnhub shooting range] impulse: lupinone
- 鹏城杯 WEB_WP
- MySQL - idea connects to MySQL
猜你喜欢
![[SRS] build a specified version of SRS](/img/01/0d2d762e01b304220b8924d20277e3.jpg)
[SRS] build a specified version of SRS

UC Berkeley proposes a multitask framework slip

Functions and differences between static and Const

MySQL——JDBC

How PHP gets all method names of objects

How PHP adds two numbers

Decompile and modify the non source exe or DLL with dnspy

仿网易云音乐小程序

Correlation

Common SQL sets
随机推荐
Rest参考
MySQL - SQL injection problem
Dahua series books
pivot ROP Emporium
China HDI market production and marketing demand and investment forecast analysis report Ⓢ 2022 ~ 2028
Kali2021.4a build PWN environment
Supply and demand situation and market scale calculation report of China's portable energy storage power PES industry Ⓛ 2022 ~ 2028
Analysis report on the development trend and Prospect of global and Chinese supercontinuum laser source industry Ⓚ 2022 ~ 2027
Décompiler et modifier un exe ou une DLL non source en utilisant dnspy
MySQL——JDBC
The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
2022 free examination questions for safety management personnel of hazardous chemical business units and reexamination examination for safety management personnel of hazardous chemical business units
Global and Chinese market of wall mounted kiosks 2022-2028: Research Report on technology, participants, trends, market size and share
The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
2022 high altitude installation, maintenance and removal of examination question bank and high altitude installation, maintenance and removal of examination papers
Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
Imitation Netease cloud music applet
Rest reference