简体   繁体   English

变量调用 function 和直接调用 function

[英]variable calling function and calling function directly

I'm learning JavaScript with Manning's book, Get Programming with JavaScript (by John Larsen).我正在学习 JavaScript 和 Manning 的书 Get Programming with JavaScript(作者 John Larsen)。 In chapter 11, Listing 11.8, the code:在第 11 章清单 11.8 中,代码:

var getCounter = function () {
    var counter = 0;
    var countUpBy1 =  function () {
        counter = counter + 1;
        return counter;
    };  
    return countUpBy1;
};
var count = getCounter();

And I'm using jsbin.com to experiment and learn.我正在使用 jsbin.com 进行实验和学习。 In the console:在控制台中:

Q1) why calling count(), the result is not always 1? Q1) 为什么调用count(),结果并不总是1? Why is it, counter is set to 0 at the start, before countUpBy1, shouldn't counter inside countUpBy1 also be 0?为什么呢,计数器一开始就设置为0,在countUpBy1之前,countUpBy1里面的计数器不应该也是0吗?

Q2) why calling getCounter() is different from count()? Q2) 为什么调用 getCounter() 与 count() 不同? calling count() get me a number (which is what I expect), but calling getCounter() get me:调用 count() 得到一个数字(这是我所期望的),但调用 getCounter() 得到我:

function () {
    counter = counter + 1;
    return counter;
}

Thanks for explaining in advance.感谢您提前解释。

4/Nov: @DavidFleeman: Am I correct in my understand to first question (I read all 3 links, plus JavaScript Closures 101: What is a closure ?, I never understood JavaScript closures & JavaScript Closures Explained by Mailing a Package : 4/Nov: @DavidFleeman: Am I correct in my understand to first question (I read all 3 links, plus JavaScript Closures 101: What is a closure ?, I never understood JavaScript closures & JavaScript Closures Explained by Mailing a Package :

(deleted my understanding of stepping through the closure) (删除了我对逐步关闭的理解)

12/Nov: I never understood JavaScript closures Read this link few more times, and it explained way better than I can. 11 月 12 日: 我从来不理解 JavaScript 闭包多读几次这个链接,它比我解释得更好。

  1. Why is calling count() not always equal to 1?为什么调用 count() 并不总是等于 1?

These nested methods create a closure where the initialization only happens once, and the nested internal method is returned each time.这些嵌套方法创建了一个闭包,其中初始化只发生一次,并且每次都返回嵌套的内部方法。 Please read this link to understand:请阅读此链接以了解:

https://www.w3schools.com/js/js_function_closures.asp https://www.w3schools.com/js/js_function_closures.asp

If you prefer MDN, use below link.如果您更喜欢 MDN,请使用以下链接。 However, the above link has example that I believe matches the member's scenario:但是,上面的链接有我认为与成员的情况相匹配的示例:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

And for even more detail, this seems to be a well thought out explanation:对于更多细节,这似乎是一个深思熟虑的解释:

https://blogs.msdn.microsoft.com/ericlippert/2003/09/17/what-are-closures/ https://blogs.msdn.microsoft.com/ericlippert/2003/09/17/what-are-closures/

  1. Why does getCounter() return the function object instead of executing the method?为什么 getCounter() 返回 function object 而不是执行方法?

Reading the same link above, you can see how to define getCounter() to do exactly what you are asking it to do.阅读上面的相同链接,您可以看到如何定义 getCounter() 以完全按照您的要求执行。 You must define it as a self-invoking function.您必须将其定义为自调用 function。 In your example, it is not defined using the self invoking syntax.在您的示例中,它不是使用自调用语法定义的。 See example below if you want to define it to work using getCounter() instead of count().如果您想使用 getCounter() 而不是 count() 来定义它,请参见下面的示例。

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

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

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