为什么这个闭包范围的变量失去其价值? [英] Why does this closure-scoped variable lose its value?

查看:128
本文介绍了为什么这个闭包范围的变量失去其价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到这个Javascript测验: http://www.netfxharmonics.com/2008/ 01 / NetFX-Harmonics-JavaScript-Quiz



我无法找出这个问题:

 (function(){
var a = 1;
var b = 2;

; var b;})();

console.log('a:'+ a); // =>a:undefined
console.log '+ b); // =>b:2
})()

但是,如果从内部函数中删除 var b; 声明,则 a == 2



(您可以在这里玩:

< http://jsfiddle.net/gnhMZ/\">http://jsfiddle.net/gnhMZ/

解决方案

这是因为这个函数:

 (function(){a = b; var b;}) 

...指派未定义 a var 自其中写入的范围,它在分步代码中。当你声明一个变量时,它的初始值是 undefined 。所以上面写的更明确,但功能完全相同,看起来像这样:

 (function(){
var b = undefined;
a = b;
})();

具体来说,当执行进入一个执行上下文时,会发生以下情况: / p>


  1. 为执行上下文创建一个幕后的变量对象,并放在< > code>在上下文中声明,而不管 var 语句在哪里。每个变量的初始值为 undefined

  2. 对于在上下文中声明的每个函数(具有函数声明,而不是函数表达式),都会在变量对象上创建属性,而不管函数声明是

  3. 处理函数声明并将结果分配给这些函数的属性。

  4. 在上下文中的逐步代码。当遇到具有初始化程序的 var 语句时,会将其作为简单的赋值语句处理。

是使闭包也工作的东西。 这里更多,但基本上,当一个函数被创建时,它得到一个持久的引用到该点的范围链中的所有变量对象。这是它用来查找它关闭的变量。这是很重要的,因为闭包不仅对实际使用的变量具有持久的引用,而且对于其定义的所有变量,无论是否使用它们,这都可能影响这些变量的生命周期。


I saw this Javascript quiz here: http://www.netfxharmonics.com/2008/01/NetFX-Harmonics-JavaScript-Quiz

and I could not figure out this problem:

(function(){
    var a = 1;
    var b = 2;

    (function( ) { a = b; var b; })( );

    console.log('a:'+ a);  // => "a:undefined"
    console.log('b:'+ b);  // => "b:2"
})()

However, if you remove the var b; declaration from the inner function, then a == 2 as you would expect.

Why is this happening?

(You can play with it here: http://jsfiddle.net/gnhMZ/)

解决方案

It's happening because this function:

(function( ) { a = b; var b; })( );

...assigns undefined to a. var takes effect as of the beginning of the scope in which it's written, not where it is in the step-by-step code. And when you declare a variable, its initial value is undefined. So the above written more explicitly, but with exactly the same functionality, looks like this:

(function( ) {
    var b = undefined;
    a = b;
})( );

Specifically, when execution enters an execution context, these things happen:

  1. A behind-the-scenes variable object is created for the execution context and put at the top of the scope chain (the chain of variable objects used to resolve unqualified references).
  2. Properties are created on that variable object for each var declared within the context, regardless of where the var statement is. The initial value of each variable is undefined. Initializers are not handled at this point.
  3. Properties are created on the variable object for each function declared (with a function declaration, not a function expression) within the context, regardless of where the function declaration is.
  4. The function declarations are processed and the results assigned to the properties for those functions.
  5. Execution continues with the first line of step-by-step code in the context. When a var statement with an initializer is encountered, it's processed as a simple assignment statement.

The variable object is the thing that makes closures work, too, by the way. More here, but basically, when a function is created it gets an enduring reference to all of the variable objects in the scope chain at that point. That's what it uses to look up the variables it closes over. This is important, because a closure doesn't only have an enduring reference to the variables it actually uses, but to all variables in-scope where it's defined, whether it uses them or not, which can have implications for the lifecycle of those variables.

这篇关于为什么这个闭包范围的变量失去其价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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