Why is variable n not reset?
function f1(){
var n=999;
nAdd=function(){n+=1}
function f2(){
alert(n);
}
return f2;
}
var result=f1();
result(); // 999
nAdd();
result(); // 1000
Explain that variable n is a global variable. Is variable n promoted to a global variable in f2?
Because js will create a stack for each function call, functions within the function can also access this stack.
First you can call
nAdd
,是因为你没加var
,等于是在函数调用时定义了一个全局作用域下的nAdd
,你加上var
If you write like this, an error will be reported.Although your
var result=f1();
调用了函数f1
,也就创建了一个栈,保存了n=999
,并返回了f2
。之后你再怎么调用result()
,其实都是在调用同一个f2
,而这个f2
引用的外部栈,自然还是第一次调用f1
时候创建的那个。同样的nAdd
works globally, it also accesses the data in the same stack.So, it’s not that n is promoted to a global variable because nAdd is a global variable, but that the function pointed to by nAdd and the closure you return are basically accessing the same data.
You can try to rewrite it as
In this code, result is actually the closure f2 function. It was run twice, the first time the value was 999, the second time the value was 1000. This proves that the local variable n in function f1 is always stored in memory and is not automatically cleared after f1 is called.
Why is this happening? The reason is that f1 is the parent function of f2, and f2 is assigned to a global variable, which causes f2 to always be in memory, and the existence of f2 depends on f1, so f1 is always in memory and will not be deleted after the call is completed. , recycled by the garbage collection mechanism (garbage collection).
Another thing worth noting in this code is the line "nAdd=function(){n+=1}". First of all, the var keyword is not used before nAdd, so nAdd is a global variable, not a local variable. Secondly, the value of nAdd is an anonymous function, and the anonymous function itself is also a closure, so nAdd is equivalent to a setter, which can operate on local variables inside the function outside the function.
http://www.ruanyifeng.com/blo...
Not
n
被提升为全局变量了,这就是闭包。。。。是
nAdd
是全局变量。nAdd
和result
中涉及的n
都是var n = 999
那个n
,而没有全局的n
http://zonxin.github.io/post/...
var nAdd = ... If you try again you will know why
If there is no var declaration, it will be promoted to a global variable
The variable n is not a global variable. Just writing it like this prevents the memory of n from being released
The f2 function has a persistent reference to the local variable n in the f1 function. After f2 returns, n will not be released, and nAdd, as a global function, can of course operate on n
n is still a local variable, because the operation of local variable n is always performed in function f1. nAdd() is a global function. When it is executed, n in the scope to which it belongs will be increased by 1
var result=f1(); When called, an internal function f2 is returned and the variable n of the external function is referenced. Due to the garbage collection mechanism, when f1 is completed. n has not been recycled. When result() is executed for the second time, n becomes 1000
nAdd global variable, no var is added, so it is a global scope
n is a local variable
var result=f1()
: The f1 function returns the f2 functionAssign the returned f2 function to the result global variable, (the scope chain of f2 is saved to the result global variable)
result()
: Call result(), which forms a closure: has the right to access variables in another function scopeBecause the scope in f2 refers to the local variable n in f1, when f1 is executed Finally, the garbage collection mechanism finds that the n variable is still referenced in result, so the garbage collection mechanism will not release n.
So that n is always saved in the result scope chain. The scope chain of result can normally access the local variable n in f1, forming a closure.
nAdd()
: nAdd does not write var, so nAdd is a global variable. When calling nAdd() and result(), a closure will be formed. The anonymous function function(){n+=1} has the local n in the scope chain. variable, so when nAdd=funtion(){n+=1}, the scope chain of this anonymous function is saved to the global variable nAdd to form a closure, and nAdd() is called to find the f1 local variable n=999, n+ in the scope chain 1=1000.result()
: result() outputs 1000This is just my understanding, if there are any mistakes please let me know
n is not a global variable, it is a closure. Why does n change? Because you did not write var in front of your nAdd and it defaults to global, but your function is defined in the closure, and the scope and so on are all inside.