简体   繁体   English

为什么不在关闭中重置变量(Javascript)

[英]Why Don't Variables Reset in a Closure (Javascript)

I've been trying to learn about closures, but one thing still perplexes me. 我一直在努力学习关闭,但有一件事仍困扰着我。 If I have the following code: 如果我有以下代码:

var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();

add();
add();
add();

// Returns "3"

If I call add() three times, why dosen't it set counter to zero every time, then return the anonymous funtion that increments counter by one? 如果我调用add()三次,为什么不每次都将counter设置为零,然后返回一个递增计数器的匿名函数? Does it skip over it once the self-invoking function runs? 一旦自动调用函数运行,它是否会跳过它? Sorry if the question seems simple, I'm having a hard time understanding it. 对不起,如果问题看起来很简单,我很难理解它。 Any help would be greatly appreciated. 任何帮助将不胜感激。

If I call add() three times, why dosen't it set counter to zero every time, then return the anonymous funtion that increments counter by one? 如果我调用add()三次,为什么不每次都将counter设置为零,然后返回一个递增计数器的匿名函数?

Because add is that anonymous function, because the function containing counter got called and its result was assigned to add : 因为add 匿名函数,因为包含counter的函数被调用并且其结果被赋值为add

var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();
//^^----------- calls the outer function, returns the anonymous inner function

If you didn't call it: 如果你没有打电话:

var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
});
//^--- no () here

...then add would do what you said, it would return a new function with its own counter, each time you called it: ...然后add会按照你说的做,它会返回一个带有自己的计数器的新函数,每次你调用它:

 var add = (function () { var counter = 0; return function () {return counter += 1;} }); var a = add(); var b = add(); var c = add(); console.log("a's first call: " + a()); console.log("a's second call: " + a()); console.log("a's third call: " + a()); console.log("b's first call: " + b()); console.log("b's second call: " + b()); console.log("b's third call: " + b()); console.log("a's fourth call: " + a()); console.log("b's fourth call: " + b()); 
 .as-console-wrapper { max-height: 100% !important; } 

That's not resetting counter , that's creating a new counter each time. 这不是重置 counter ,每次创建一个新的计数器。

By calling add() you are not actually executing outer function but instead you are executing inner function. 通过调用add()您实际上并不执行外部函数,而是执行内部函数。 For inner functtion, counter is like a global variable that has been set once to 0 and then it was never set again to 0 . 对于内部函数,counter就像一个全局变量,它已被设置为0 ,然后它再也没有被设置为0 On calling add() you are executing lines inside inner function thus increamenting counter. 在调用add()您正在执行内部函数内的行,从而增加计数器。

The value assigned to add is the result of the IIFE, in which the closure was created. 分配给add的值是IIFE的结果,其中创建了闭包。 Maybe it's more obvious what will happen when add() is called, when its creating is written as follows (equivalent to your original code): 也许更明显当调用add()时会发生什么,当它的创建编写如下(相当于原始代码):

var add;
(function () {
    var counter = 0;
    add = function () {return counter += 1;};
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM