当前位置:网站首页>The concept, function, problem and solution of closure
The concept, function, problem and solution of closure
2022-04-22 19:40:00 【ck_ wr】
Closure (closure)
** Concept :** Functions that have access to variables in the scope of another function
** effect :** Extend the scope of the variable
Special point :
There are two kinds of variables in the code : Global and local variables , Global variables can be provided to the functions used globally , Local variables can only be supplied to the current function , And only when the function is called , Destroy immediately after the function is executed . Closure is a special function , It can access local variables in another function
Explanation examples :
function fn(){
var num = 10;
function fun(){
num++
console.log(num)
}
fun()
}
fn()
The function is called fn(), The result will be through the function fun() Get the return value 10;
reflection :fun() There is no variable defined in the function num,num The definition of is written in its upper function fn() Inside , It can be understood as fn() Local variables of , however fun() Function is called , This is irregular in terms of the characteristics of local variables
Explain and analyze :
js The chain index rule of the interpreter States , When an internal undefined variable is used in a function , Will send letters to the upper level in turn Number to index values , The destruction time of the local variables of the upper function is after the execution of the function , So when its embedded function fun() Execution time , The local variables of its upper function have not been destroyed , In this way, the principle of closure can be explained . also , In such closures ,fun() The variable num The modification of the value will affect the variables in the upper function num Value
Another way of writing :
function fn(){
var num = 10;
return function fun(){
console.log(num)
}
}
var f = fn()
f()
var f2 = fn()
The problem is :
The above said , Because the upper function fn() Not completed , So local variables are not destroyed , The embedded function in it fn() Can continue to use through the chain index fn() Local variables of , And here when f = fn(),fn() It is obvious that All right, it's over , Logically speaking num It has been destroyed , But the fact is that we get the inner function body fun() The variable of f You can still use variables num
Explain and analyze :
When executed f=fn() when , because fun() Its upper function is used in fn() Local variable in , This leads to fn() It's not done , But be fun() Get stuck , So the variables in it num Not destroyed , therefore f() Can be called many times ,num But will not be destroyed , You can even record cumulative changes to values ( Only in fn() Internal records , Does not affect fn(), This is a difference from the previous writing , The reason is var f = fn() when , take fun() The function body is stored in a new space ), For example, add up ; The problem is : because fn() stuck , Cause it to occupy memory all the time .
The whole can be understood as :
num about fn() And the code as a whole , It's a local variable ( Cannot be called by an external function ), But for f() for , It's from fn() inherited , Can cumulatively record changes , Local variables that will not be destroyed , This value is only in f() For internal use , The update of the value will not affect the upper function fn() The corresponding value , So no matter f() How did the value change ,f2() The initial value inside is still from fn() It's got 10
版权声明
本文为[ck_ wr]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221939453267.html
边栏推荐
猜你喜欢

Official component amap of Gaud fluent_ Flutter_ Map draw a circle on the map

The function of final and why string is immutable

Excel consolidation calculation of multiple similar tables (consolidation calculation tool)

Incorrect string value: ‘\xF0\x9F\x92\x95\’

Linux环境下部署redis教程详解

quarkus依赖注入之九:bean读写锁

LeetCode_ 343 integer split

10.4.4 51单片机控制系统8个LED“跑马灯”实验

Detailed explanation of redis deployment in Linux Environment

【AI视野·今日Robot 机器人论文速览 第三十三期】Thu, 21 Apr 2022
随机推荐
14 sprak set automatic zoning
从零开始学安卓(kotlin)七——跨程序共享数据
mmocr DBLoss
时间日期格式化
The function of final and why string is immutable
. net using supersocket to build socket server
07-Complex Types
Garbage collector and CMS G1
How can Xinhua 3 enable the whole stack of data center carbon neutralization?
2022年中国数字科技专题分析
LeetCode_ 343 integer split
asp.net反编译和防止反编译方法
Detailed network structure diagram of dbnet character detection
09-Partitioning
13-Set Time Zone
SqlServer判断表中某列是否包含中文,英文,纯数字
Linux环境下部署redis教程详解
Revit (5) - 二开 -创建墙体
JS copy and paste, clipboard js
10.4.4 51单片机控制系统8个LED“跑马灯”实验