这个函数是javascript中的闭包吗?

发布于 2024-11-17 10:58:27 字数 570 浏览 0 评论 0原文

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

函数 f2() 是一个闭包吗?如果不?为什么?

但在这篇文章中。 JavaScript 闭包如何工作? 为什么它说:

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

这不是闭包。闭包是指当您返回内部函数时。内部函数在离开之前会关闭 foo 的变量。为什么?

但我不知道我做的例子和我引用的另一个例子有什么区别,一个不是闭包,但一个是。我认为这两个例子是相同的。

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

is the function f2() a closure? if not? why?

but in this post. How do JavaScript closures work?
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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下客 2024-11-24 10:58:27

是的,f2 是一个闭包,因为它从外部作用域访问变量 (n)。 (好吧,这并不是真正的闭包——请参阅下面的更新)。

n 是在 f1 内声明的,而不是 f2 内;这使得它属于 f1 的范围。因此,当您创建引用 n 的函数 f2 时,根据定义,它是一个闭包,因为它使用了其他人的变量。

更新

好吧,如果我理解你的答案链接正确,它说 f2 不是一个闭包,因为它只是访问其范围内的变量(就像 if 语句一样,它在以下范围内有自己的范围大括号*,可以使用外部作用域中的变量而不需要闭包)。

* 更新: 事实证明,只有函数在 Javascript 中拥有自己的作用域,而不是任何旧的块。但我的观点仍然成立...

但是,如果 f2 离开 f1 的范围(例如通过被退回);在这种情况下,它仍然可以访问 f1 的变量 n,即使 f1 的原始作用域不再存在(它是当控制离开函数时退出)。 f2 将“封闭”f1 的变量,从而人为地延长 f1 作用域的生命周期。

就我个人而言,我仍然将 f2 称为闭包,即使它永远不会离开 f1。如果一个函数只需在其声明范围之外使用就可以成为闭包,并且无论它在技术上是否是一个闭包,该函数在该范围内的行为都没有什么不同,那么我认为没有必要进行区分。我什至可以说,如果 f2 永远不会离开 f1 的作用域,那么无论 f2 是否是一个闭包,它都是一个实现细节。

另一方面,忽略这种区别意味着任何使用全局变量的函数都将被称为“闭包”,因为它从外部作用域访问变量。因此,只有当它离开它所使用的变量定义的范围时,它才成为闭包,这一事实是一个值得(尽管很微妙)的区别。

我想我能给出的最明确的答案是它还没有结束

Yes, f2 is a closure, since it accesses a variable (n) from an outer scope. (OK, it's not really a closure -- see update below).

n was declared inside f1, not f2; this makes it belong to f1's scope. So when you create the function f2 which references n, it is a closure by definition, since it uses someone else's variable.

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

* Update: Turns out that only functions get their own scope in Javascript, not any old blocks. But my point still stands...

However, f2 would become a closure if it left f1's scope (e.g. by being returned); 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). f2 would have "closed over" f1's variables, thus artificially extending the lifespan of f1's scope.

Personally, I would still call f2 a closure, even if it never leaves 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.

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.

I guess the clearest answer I can give is that it's not a closure yet.

已下线请稍等 2024-11-24 10:58:27

它是一个闭包,因为它取决于中间(即不是本地或全局)范围内的变量值,并且如果变量发生变化,那么闭包的操作也会发生变化。值总是相同是偶然的。

但是,您可能希望将闭包返回给 f1() 的调用者,以便可以在其他地方使用它。

It is a closure, because it depends on the values of variables in an intermediate (i.e. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文