JavaScript Dynamic Closure Scope

I am running into an issue with closures and want to confirm a suspicion. I am trying to give a function access to a scope outside of where it was originally defined. For example the following code block works as I would expect.

(function(){

    function Outer () {
        var test = "closure access";
        return function Inner () {
            alert(test);
        };
    };

    // call outer then inner
    Outer()();

}());

When run the string "closure access" is alerted.

However if the inner function is defined outside of the "Outer" scope and then returned from the "Outer" function it no longer has access to the local "test" variable. For example:

(function(){

    function Inner () {
        alert(test);
    };

    function Outer () {
        var test = "closure access";
        return Inner;
    };

    // call outer then inner
    Outer()();

}());

This behavior makes sense to me as the "Outer" function is actually looking beyond it's normal scope to find the "Inner" function defined above it, and the Inner function is not defined within the scope of the "Outer"'s local var "test". My question is, is there a way to define a function somewhere (elsewhere) but dynamically return it from another function while still preserving the variables of its closure function?

Please let me know if I must explain better.

==== Edit ====

Ok, so defining the function creates the scope and not using the function. Does the following code snippet not define a function?

(function(){

    function Outer () {
        var test = "closure access";
        return new Function("alert(test);");
    };

    // call outer then inner
    Outer()();

}());

If the new Function is defining a function at that point in memory should it not have access to the test variable?


My question is, is there a way to define a function somewhere (elsewhere) but dynamically return it from another function while still preserving the variables of its closure function?

No. You need to define a function there to access variables from that scope.

You could however dynamically use other functions in that one, so that you can somehow simulate this behaviour; nonetheless you always need to create a new function.

Does the following code snippet not define a function?

return new Function("alert(test);");

No. The Function constructor creates a function without a scope chain, ie as if it was declared in the global scope. You'd need to use eval .


If all functions are defined under same object then u can use this

(function(){
    function Inner () {
       alert(this.test);
    };
    function Outer () {
       this.test = "closure access";
       return Inner;
    };
    // call outer then inner
    Outer()();
}());

but if functions are defined under different object then u can use either prototype.call(this) or prototype.apply(this) to pass the reference and in that way u could have access to other object property.

链接地址: http://www.djcxy.com/p/51614.html

上一篇: 为什么嵌套的IIFE不创建闭包?

下一篇: JavaScript动态关闭范围