当前位置:网站首页>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 .
边栏推荐
- js demo 计算本年度还剩下多少天
- 油猴插件
- Under the double reduction policy, research travel may become a big winner
- Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
- Compréhension de la technologie gslb (Global Server load balance)
- Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
- 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
- Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
- Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
- 抓包整理外篇——————autoResponder、composer 、statistics [ 三]
猜你喜欢

Code in keil5 -- use the code formatting tool astyle (plug-in)
![[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]](/img/55/309c9d52e503405b289bcfc4912be9.jpg)
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]

MySQL——索引

Selenium has three waiting methods (forced waiting, implicit waiting, and display waiting)

Why use pycharm to run the use case successfully but cannot exit?

Advanced technology management - how to examine candidates in the interview and increase the entry probability

Morning flowers and evening flowers

Teach you how to install aidlux (1 installation)

Imitation Netease cloud music applet

Getting started with postman -- environment variables and global variables
随机推荐
Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Monkey/ auto traverse test, integrate screen recording requirements
Analysis report on the development prospect and investment strategy of global and Chinese modular automation systems Ⓟ 2022 ~ 2027
十大券商开户注册安全靠谱吗?有没有风险的?
Base ring tree Cartesian tree
技术管理进阶——如何在面试中考察候选人并增大入职概率
Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
The 14th five year plan for the construction of Chinese Enterprise Universities and the feasibility study report on investment Ⓓ 2022 ~ 2028
No more! Technical team members resign collectively
Collection | pytoch common loss function disassembly
股票炒股开户注册安全靠谱吗?有没有风险的?
Investment analysis and prospect trend prediction report of China's boron nitride industry Ⓨ 2022 ~ 2028
Getting started with DOM
Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
MySQL - SQL injection problem
An expression that regularly matches one of two strings
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
Persistence of Nacos
常用sql集合