当前位置:网站首页>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 .
边栏推荐
- Is the account opening of Guotai Junan Securities safe and reliable? How to open Guotai Junan Securities Account
- 鹏城杯 WEB_WP
- [vulnhub shooting range] impulse: lupinone
- Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share
- Go language slice interview real question 7 consecutive questions
- Collections SQL communes
- MySQL——数据库备份
- Is it OK for fresh students to change careers to do software testing? The senior answered with his own experience
- Sed、Awk
- Correlation
猜你喜欢
Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- led lamp module (V)
On my first day at work, this API timeout optimization put me down!
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.
Asynchronous artifact: implementation principle and usage scenario of completable future
2022 electrician (elementary) examination questions and electrician (elementary) registration examination
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
Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files
随机推荐
90 后,辞职创业,说要卷死云数据库
十大券商开户注册安全靠谱吗?有没有风险的?
Dynamic research and future planning analysis report of China's urban water supply industry Ⓝ 2022 ~ 2028
Global and Chinese market of wireless hard disk 2022-2028: Research Report on technology, participants, trends, market size and share
90 後,辭職創業,說要卷死雲數據庫
Code in keil5 -- use the code formatting tool astyle (plug-in)
Cognitive fallacy: what is dimensional curse
China HDI market production and marketing demand and investment forecast analysis report Ⓢ 2022 ~ 2028
Common SQL sets
Farmersworld farmers world, no faith, how to talk about success?
treevalue——Master Nested Data Like Tensor
Go language slice interview real question 7 consecutive questions
Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
gslb(global server load balance)技術的一點理解
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
Kali2021.4a build PWN environment
pivot ROP Emporium
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Dahua series books
UI automation test: selenium+po mode +pytest+allure integration