search

Home  >  Q&A  >  body text

Questions about javascript closures?

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?

高洛峰高洛峰2829 days ago997

reply all(9)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:52:47

    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,你加上varIf 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

    function f1(){
        var n=999;
        nAdd=function(){n+=1}
        function f2(){
          alert(n);
        }
        return f2;
      }
    
      f1()(); // 调用f1,创建了一个栈,栈内n=999,创建了一个匿名函数,返回了一个闭包。
      nAdd(); // 调用了那个匿名函数
      f1()(); // 又调用f1,又创建了一个栈,栈内n=999,创建了另一个匿名函数,返回了另一个闭包。

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-18 10:52:47

    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...

    reply
    0
  • ringa_lee

    ringa_lee2017-05-18 10:52:47

    Not n 被提升为全局变量了,这就是闭包。。。。
    nAdd是全局变量。
    nAddresult中涉及的n 都是 var n = 999那个n,而没有全局的n

    http://zonxin.github.io/post/...

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-18 10:52:47

    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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:52:47

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-05-18 10:52:47

    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

    reply
    0
  • 阿神

    阿神2017-05-18 10:52:47

    • 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

    reply
    0
  • 黄舟

    黄舟2017-05-18 10:52:47

    1. var result=f1(): The f1 function returns the f2 function
      Assign the returned f2 function to the result global variable, (the scope chain of f2 is saved to the result global variable)

    2. result(): Call result(), which forms a closure: has the right to access variables in another function scope
      Because 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.

    3. 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.

    4. result(): result() outputs 1000

    This is just my understanding, if there are any mistakes please let me know

    reply
    0
  • PHP中文网

    PHP中文网2017-05-18 10:52:47

    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.

    reply
    0
  • Cancelreply