这个函数是javascript中的闭包吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,
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 insidef1
, notf2
; this makes it belong tof1
's scope. So when you create the functionf2
which referencesn
, 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 anif
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 leftf1
's scope (e.g. by being returned); in that case, it would still have access tof1
's variablen
, even thoughf1
'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 off1
's scope.Personally, I would still call
f2
a closure, even if it never leavesf1
. 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 whetherf2
is a closure or not, if it never leavesf1
'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.
它是一个闭包,因为它取决于中间(即不是本地或全局)范围内的变量值,并且如果变量发生变化,那么闭包的操作也会发生变化。值总是相同是偶然的。
但是,您可能希望将闭包返回给
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.