简体   繁体   English

显示模块模式的Javascript范围问题

[英]Javascript scope issues with Revealing Module Pattern

One more JavaScript scoping problem. 一个JavaScript范围界定问题。 I believe this is not related to 'hoisting'. 我相信这与“吊装”无关。

I've a small block of code here - http://jsfiddle.net/0oqLzsec/2/ 我在这里有一小段代码-http: //jsfiddle.net/0oqLzsec/2/

var c = function(){
    var x = 'before',
        callBefore = function(){
            alert(x);
        },
        callAfter = function(){
            _callAfter();
        };

    return{
        callBefore : callBefore,
        callAfter : callAfter
    }

    var y = 'after';

    function _callAfter(){
        alert(y);
    }
};

var obj = c();
obj.callBefore();
obj.callAfter();

callAfter() always alerts 'undefined' even if the variable y is defined before _callAfter() . callAfter()总是提醒“未定义”,即使变量y之前定义_callAfter() But If I move the variable y declaration before variable x it is working. 但是,如果我将变量y声明移到变量x之前,它将起作用。

I'm wondering if callAfter() can _callAfter() which is defined below, why can't it read variable y? 我想知道callAfter()可以定义下面的_callAfter() ,为什么它不能读取变量y?

_callAfter and y are both hoisted, which makes them known as local variables through the function invocation. _callAftery都被提升,这通过函数调用将它们称为局部变量。 The _callAfter function is able to be called (as you do), and it also closes over the variable y . 可以调用_callAfter函数(就像您所做的那样),并且它也覆盖变量y However, this variable does not get a value assigned before you return from the c function. 但是,从c函数返回之前,此变量不会获得分配的值。 Thus, you get undefined . 因此,您将获得undefined It is the same if you put var y; 如果您输入var y; ,则结果相同var y; BEFORE the return, and y='after'; 返回之前,并且y='after'; after the return. 返回之后。

In other words, the following two are equivalent: 换句话说,以下两个是等效的:

 function foo(){ var a; return function(){ return a }; a = 42; // never runs } alert( foo()() ); function bar(){ return function(){ return a }; var a = 42; } alert( bar()() ); 

Here's an example showing that the local variable is hoisted and closed over, even when the function literal occurs before the var in code: 这是一个示例,显示了本地变量被吊起和关闭,即使函数文字出现在代码中的var之前:

 function build() { return function(newValue) { alert("was: " + oldValue); oldValue = newValue; alert("now: " + oldValue); } var oldValue = 42; } f = build(); f(17); // was: undefined // now: 17 f(99); // was: 17 // now: 99 

It is related to hoisting, var y is moved to the top of the function, but the assignment ( y = 'after' ) is not, so it never runs because it's after the return statement . 它与提升有关, var y移动到函数的顶部,但是赋值( y = 'after' )不是,因此它永远不会运行,因为它位于return statement

The JS engine rewrites your code to look something like: JS引擎将您的代码重写为如下所示:

var c = function(){           
    // functions also get hoisted to the top
    function _callAfter(){
        alert(y);
    }
    var x = 'before',
        callBefore = function(){
            alert(x);
        },
        callAfter = function(){
            _callAfter();
        },
        // y declaration is hoisted to the top of the function
        y;

    return {
        callBefore : callBefore,
        callAfter : callAfter
    };

    // the assignment never gets called because it's after the return
    y = 'after';
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM