当前位置:网站首页>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 .
边栏推荐
- QFileDialog
- 大神们,我想发两个广播流1 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
- 2022-02-15 Daily: 2022 AAAI fellow release
- How to store null value on the disk of yyds dry inventory?
- C程序设计的初步认识
- Control loop of program (while loop)
- DOM light switch case
- Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
- Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
- MySQL - idea connects to MySQL
猜你喜欢

Covariance

Morning flowers and evening flowers

使用dnSpy对无源码EXE或DLL进行反编译并且修改

How PHP gets all method names of objects

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

TiDB 之 TiCDC6.0 初体验

What is the difference between res.send() and res.end() in the node express framework

Teach you how to install aidlux (1 installation)

Nacos common configuration

2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination
随机推荐
Global and Chinese market of wireless hard disk 2022-2028: Research Report on technology, participants, trends, market size and share
Nacos common configuration
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
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.
Notes on MySQL related knowledge points (startup, index)
Mysql - - Index
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
MySQL - idea connects to MySQL
The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
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
Functions and differences between static and Const
国泰君安证券开户是安全可靠的么?怎么开国泰君安证券账户
[vulnhub shooting range] impulse: lupinone
Leetcode problem solving - 230 The k-th smallest element in the binary search tree
Rest参考
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
MySQL -- standardize database design
Under the double reduction policy, research travel may become a big winner
2022-2-14 acwing1027 grid access