简体   繁体   English

这个函数是JavaScript的闭包吗?

[英]is this function a closure in javascript?

    function f1(){
        var n=999;
        function f2(){
            alert(n); // 999
        }
    }

is the function f2() a closure? 函数f2()是闭包吗? if not? 如果不? why? 为什么?

but in this post. 但在这篇文章中。 How do JavaScript closures work? JavaScript闭包如何工作? why it says: 为什么说:

function foo(x) {
  var tmp = 3;
  function bar(y) {
    alert(x + y + (++tmp));
  }
  bar(10);
}
foo(2)

That is not a closure.A closure is when you return the inner function. 那不是闭包,闭包就是当你返回内部函数的时候。 The inner function will close-over the variables of foo before leaving. 内部函数将在离开前关闭foo的变量。 why? 为什么?

but i don't know what's the difference between the example i made and another i cited, one is not a closure.but one is. 但我不知道我所举的例子与我引述的另一个例子之间有什么区别,一个不是闭包,而是一个闭包。 i think the two example is the same. 我认为这两个例子是相同的。

Yes, f2 is a closure, since it accesses a variable ( n ) from an outer scope. 是的, f2是一个闭包,因为它从外部范围访问变量( n )。 (OK, it's not really a closure -- see update below). (好的,这并不是真正的闭包-请参阅下面的更新)。

n was declared inside f1 , not f2 ; n是在f1而不是f2声明的; this makes it belong to f1 's scope. 这使其属于f1的范围。 So when you create the function f2 which references n , it is a closure by definition, since it uses someone else's variable. 因此,当您创建引用n的函数f2 ,它就定义为闭包,因为它使用了其他人的变量。

UPDATE UPDATE

Alright, if I understand the answer you've linked to correctly, it says that f2 is not a closure because it is merely accessing a variable within its scope (just like an if statement, which gets its own scope within the braces * , can use variables from the outer scope without needing a closure). 好吧,如果我正确理解了您所链接的答案 ,它表示f2 不是闭包,因为它只是访问其范围内的变量(就像if语句在括号*中得到了自己的范围一样,可以使用外部作用域中的变量而无需关闭)。

* Update: Turns out that only functions get their own scope in Javascript, not any old blocks. * 更新:事实证明,只有函数在Javascript中获得了自己的作用域,而没有任何旧块。 But my point still stands... 但是我的观点仍然存在

However, f2 would become a closure if it left f1 's scope (eg by being returned); 但是,如果f2离开f1的范围(例如,通过返回), 它将成为闭包。 in that case, it would still have access to f1 's variable n , even though f1 's original scope would no longer exist (it's exited when control leaves the function). 在那种情况下,即使f1的原始作用域不再存在(当控制离开函数时它已退出),它仍然可以访问f1的变量n f2 would have "closed over" f1 's variables, thus artificially extending the lifespan of f1 's scope. f2将“封闭” f1的变量,从而人为地延长了f1范围的寿命。

Personally, I would still call f2 a closure, even if it never leaves f1 . 就个人而言,即使f2从未离开过f1 ,我仍然称其为闭包。 If a function can become a closure simply by being used outside of its declaring scope, and its behaviour inside that scope is no different whether it's technically a closure or not, then I see no point in making a distinction. 如果一个函数可以简单地通过在声明范围之外使用而成为闭包,并且在该范围内的行为在技术上是否不是闭包都没有什么不同,那么我认为没有区别。 I would even go so far as to say that it's an implementation detail whether f2 is a closure or not, if it never leaves f1 's scope. 我什至可以说,如果f2是否为闭包,则它是一个实现细节,如果它从不离开f1的范围的话。

On the other hand, ignoring that distinction would mean that any function that uses a global variable would be called a "closure", since it accesses a variable from an outer scope. 另一方面,忽略该区别将意味着使用全局变量的任何函数都将称为“关闭”,因为它从外部作用域访问变量。 So the fact that it becomes a closure only when it leaves the scope(s) that the variables it is using are defined in is a worthwhile (albeit subtle) distinction. 因此,它成为一个封闭,只有当它的叶子,它使用的变量中定义的范围(S)的事实值得的(虽然细微)的区别。

I guess the clearest answer I can give is that it's not a closure yet . 我想最清晰的答案,我可以给的是, 它不是一个封闭

It is a closure, because it depends on the values of variables in an intermediate (ie not local or global) scope, and if the variable changes then so will the operation of the closure. 这是一个闭包,因为它取决于中间(即不是本地或全局)范围内的变量的值,并且如果变量发生更改,那么闭包的操作也将更改。 That the value is always the same is incidental. 该值始终相同是偶然的。

You may however want to return the closure to the caller of f1() so that it can be used elsewhere. 但是,您可能希望将闭包返回给f1()的调用者,以便可以在其他地方使用它。

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

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