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

UC Berkeley proposes a multitask framework slip

Compréhension de la technologie gslb (Global Server load balance)

Day 9 HomeWrok-ClassHierarchyAnalysis

Morning flowers and evening flowers
![Intimacy communication -- [repair relationship] - use communication to heal injuries](/img/c2/f10405e3caf570dc6bd124d65b2e93.jpg)
Intimacy communication -- [repair relationship] - use communication to heal injuries

Imitation Netease cloud music applet

On my first day at work, this API timeout optimization put me down!

What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics

Blue Bridge Cup Guoxin Changtian MCU -- program download (III)

90 后,辞职创业,说要卷死云数据库
随机推荐
MySQL——JDBC
Is it OK for fresh students to change careers to do software testing? The senior answered with his own experience
Cognitive fallacy: what is Fredkin's paradox
仿网易云音乐小程序
Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
What is the difference between res.send() and res.end() in the node express framework
Tkinter Huarong Road 4x4 tutorial III
gslb(global server load balance)技術的一點理解
A little understanding of GSLB (global server load balance) technology
Luogu deep foundation part 1 Introduction to language Chapter 6 string and file operation
Pengcheng cup Web_ WP
Day 9 HomeWrok-ClassHierarchyAnalysis
2022-2-14 acwing1027 grid access
WiFi 2.4g/5g/6g channel distribution
Advanced technology management - how to examine candidates in the interview and increase the entry probability
China's Call Center Industry 14th five year plan direction and operation analysis report Ⓔ 2022 ~ 2028
Netfilter ARP log
How to install sentinel console
pivot ROP Emporium
MySQL——JDBC