繁体   English   中英

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

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

我一直在努力学习关闭,但有一件事仍困扰着我。 如果我有以下代码:

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

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

// Returns "3"

如果我调用add()三次,为什么不每次都将counter设置为零,然后返回一个递增计数器的匿名函数? 一旦自动调用函数运行,它是否会跳过它? 对不起,如果问题看起来很简单,我很难理解它。 任何帮助将不胜感激。

如果我调用add()三次,为什么不每次都将counter设置为零,然后返回一个递增计数器的匿名函数?

因为add 匿名函数,因为包含counter的函数被调用并且其结果被赋值为add

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

如果你没有打电话:

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

...然后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; } 

这不是重置 counter ,每次创建一个新的计数器。

通过调用add()您实际上并不执行外部函数,而是执行内部函数。 对于内部函数,counter就像一个全局变量,它已被设置为0 ,然后它再也没有被设置为0 在调用add()您正在执行内部函数内的行,从而增加计数器。

分配给add的值是IIFE的结果,其中创建了闭包。 也许更明显当调用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