更深入的了解Javascript中的闭包 [英] deeper understanding of closure in Javascript

查看:87
本文介绍了更深入的了解Javascript中的闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于答案的意见,并看到此评论

I was reading the comments on an answer and saw this comment:


[closure]不会持久化foo的状态,因为创建了一个特殊的范围,包含返回函数和(2)返回时引用的所有外部变量。这个特殊范围称为闭包。

[the closure] doesn't persist the state of foo so much as creates a special scope containing (1) the returned function and (2) all the external variables referenced at the time of the return. This special scope is called a closure.

好吧,到目前为止还不错。现在这里是我不知道的有趣的部分:

OK, so far so good. Now here is the interesting part that I didn't know about:


case in point ...如果你在foo中定义了另一个var

Case in point... if you had another var defined in foo that was not referenced in the return function, it would not exist in the closure scope.

我猜想,在返回函数中未引用这有意义,但这具有什么含义,除了内存使用/ perfomance

I guess that makes sense, but what implications does this have other than memory use / perfomance?

问题 - 如果范围中的所有变量都包含在闭包中,那么什么允许我这样做,模型?

Question -- If all variables in scope were included in the closure, what would that allow me to do that I cannot do with the current model?

推荐答案

我认为你的评论太字面上。注释只是说你不能访问它的函数范围之外(它不可公开访问),而不是在函数中根本不可用。返回的函数将可以访问所有的外部函数范围,无论什么。

I think you're taking that comment too literally. The comment is just saying that you can't access it outside the function scope (it's not publicly accessible), not that its not available at all within the function. The returned function will have access to all of the outer functions scope no matter what. You just can't access that scope outside the outer function if the inner function doesn't provide a way of accessing it.

例如,这个表达式的计算结果为4:如果内部函数没有提供访问外部函数的方法,

For instance, this expression evaluates to 4:

function testClosure(){
 var x = 2;
    return function(y){
        alert(eval(y));
    }

}

var closure = testClosure();

closure("x+2");  //4

http://jsfiddle.net/dmRcH/

所以 x 尽管没有被直接引用

So x is available despite not being directly referenced

Chrome和Firefox似乎至少尝试优化如果你不提供任何方式来引用 x 变量,它不会显示为在调试器中可用。在闭包中使用断点运行此操作会在Chrome 26和Firefox 18上显示 x 不可用。

It appears that chrome and firefox at least do attempt to optimize this in the sense that if you're not providing ANY way to reference the x variable, it doesn't show up as being available in the debugger. Running this with a breakpoint inside a closure shows x as unavailable on Chrome 26 and Firefox 18.

http://jsfiddle.net/FgekX/1/

但这只是一个内存管理细节,而不是语言的相关属性。如果有可能的方式,你可以引用的变量,它被传递,我的怀疑是,其他浏览器可能不会以同样的方式优化。它总是更好的代码规范比一个实现。在这种情况下,虽然规则真的是:如果有任何可能的方式,您可以访问它,它将可用。此外,不要使用eval,因为它会确保您的代码不会优化任何

But thats just a memory management detail, not a relevant property of the language. If there is any possible way that you could reference the variable, it is passed, and my suspicion is that other browsers may not optimize this in the same way. Its always better to code to the spec than to an implementation. In this case though the rule really is: "if there's any possible way for you to access it, it will be available". And also, don't use eval because it really will keep your code from optimizing anything.

这篇关于更深入的了解Javascript中的闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆