javascript语言中的闭包 2

作者在 2007-04-22 05:49:00 发布以下内容

但是ECMAScript提供的with表达式会修改scope chain.with表达式,我是能不用就不用了,<javascript权威指南>中也说with会造成性能的集聚下降。原文贴在下面。有时间再仔细研究。

The with statement evaluates an expression and if that expression is an object it is added to the scope chain of the current execution context (in front of the Activation/Variable object). The with statement then executes another statement (that may itself be a block statement) and then restores the execution context's scope chain to what it was before.

A function declaration could not be affected by a with statement as they result in the creation of function objects during variable instantiation, but a function expression can be evaluated inside a with statement:-

/* create a global variable - y - that refers to an object:- */
var y = {x:5}; // object literal with an - x - property
function exampleFuncWith(){
    var z;
    /* Add the object referred to by the global variable - y - to the
       front of he scope chain:-
    */
    with(y){
        /* evaluate a function expression to create a function object
           and assign a reference to that function object to the local
           variable - z - :-
        */
        z = function(){
            ... // inner function expression body;
        }
    }
    ...
}

/* execute the - exampleFuncWith - function:- */
exampleFuncWith();
When the exampleFuncWith function is called the resulting execution context has a scope chain consisting of its Activation object followed by the global object. The execution of the with statement adds the object referred to by the global variable y to the front of that scope chain during the evaluation of the function expression. The function object created by the evaluation of the function expression is assigned a [[scope]] property that corresponds with the scope of the execution context in which it is created. A scope chain consisting of object y followed by the Activation object from the execution context of the outer function call, followed by the global object.

When the block statement associated with the with statement terminates the scope of the execution context is restored (the y object is removed), but the function object has been created at that point and its [[scope]] property assigned a reference to a scope chain with the y object at its head.

3、Identifier Resolution
   关于这部分我决定不按照原文直译。Identifier Resolution是一个过程,而不是具体的概念,我举个例子可能就明白了。

<SCRIPT LANGUAGE="JavaScript">
<!--
var s_global='global';//scope chain {global} 中
var s_outer='global';//scope chain {global} 中
var s_inner='global';//scope chain {global} 中
function outerfun(){//scope chain {global} 中
    var s_outer='outer';//scope chain  {outerfun调用对象,global}
 pf('outer代码开始');
 pf(s_global);//global
    pf(s_outer);//outerfun调用对象
    pf(s_inner);//global

 function innerfun(){////scope chain  {outerfun调用对象,global}
    var s_inner='inner';//scope chain  {innerfun调用对象,outerfun调用对象,global}
 pf('inner代码开始');
 pf(s_global);//global
    pf(s_outer);//outerfun调用对象
    pf(s_inner);//innerfun调用对象
 }
 return innerfun;
}
function pf(msg){document.writeln('</br>'+msg);};
pf('global代码开始');
pf(s_global);//global
pf(s_outer);//global
pf(s_in

我的日志 | 阅读 1041 次
文章评论,共0条
游客请输入验证码
浏览114679次