当前位置:网站首页>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 calculate how many days are left in this year
- Cognitive fallacy: what is Fredkin's paradox
- treevalue——Master Nested Data Like Tensor
- MySQL - SQL injection problem
- 2022-02-15 Daily: 2022 AAAI fellow release
- UI automation test: selenium+po mode +pytest+allure integration
- An expression that regularly matches one of two strings
- pivot ROP Emporium
- How PHP drives mongodb
- 仿网易云音乐小程序
猜你喜欢

Dahua series books

Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)

A little understanding of GSLB (global server load balance) technology
Implementation principle of inheritance, encapsulation and polymorphism

MySQL——数据库备份

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

90 後,辭職創業,說要卷死雲數據庫

treevalue——Master Nested Data Like Tensor

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

2022 electrician (elementary) examination questions and electrician (elementary) registration examination
随机推荐
Introduction to kubernetes
Leetcode problem solving - 230 The k-th smallest element in the binary search tree
Preliminary understanding of C program design
Netfilter ARP log
Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
JS demo calculate how many days are left in this year
Imitation Netease cloud music applet
Asynchronous artifact: implementation principle and usage scenario of completable future
常用sql集合
Control loop of program (while loop)
MySQL - SQL injection problem
Getting started with postman -- environment variables and global variables
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Common SQL sets
gslb(global server load balance)技術的一點理解
Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files
Covariance
JS notes (III)
Getting started with DOM