0

在 JavaScript 中,是否可以将内部函数从一个函数移动到全局范围?我还没有找到任何直接的方法来做到这一点。

function moveMethodsIntoGlobalScope(functionName){
    //move all of functionName's methods into the global scope
    //methodsToPutIntoGlobalScope should be used as the input for this function.
}

//I want all of the methods in this function to be moved into the global scope so that they can be called outside this function.
function methodsToPutInGlobalScope(){
    function alertSomething(){
        alert("This should be moved into the global scope, so that it can be called from outside the function that encloses it.");
    }
    function alertSomethingElse(){
        alert("This should also be moved into the global scope.");
    }
}
4

3 回答 3

2

如果您不想进行更改,methodsToPutInGLobalSpace可以使用以下肮脏的技巧:

var parts = methodsToPutInGlobalScope.toString().split('\n');
eval(parts.splice(1, parts.length - 2).join(''));

作为最终解决方案,我们可以使用:

moveMethodsIntoGlobalScope(methodsToPutInGlobalScope);
alertSomething(); //why doesn't this work?

function moveMethodsIntoGlobalScope(functionName){
    var parts = functionName.toString().split('\n');
    eval.call(window, parts.splice(1, parts.length - 2).join(''));  
}

//I want all of the methods in this function to be moved into the global scope so that they can be called outside this function.
function methodsToPutInGlobalScope(){
    function alertSomething(){
        alert("This should be moved into the global scope, so that it can be called from outside the function that encloses it.");
    }
    function alertSomethingElse(){
        alert("This should also be moved into the global scope.");
    }
}

现场演示

于 2013-04-24T17:16:04.600 回答
1

不是很复杂,但会工作。

function copyInto(arr, context) {
    //move all of functionName's methods into the global scope
    //methodsToPutIntoGlobalScope should be used as the input for this function.
    for (var i = 0; i < arr.length; i += 2) {
        var exportName = arr[i];
        var value = arr[i + 1];
        eval(exportName + "=" + value.toString());
    }
}

//I want all of the methods in this function to be moved into the global scope so that they can be called outside this function.
function methodsToPutInGlobalScope() {
    function alertSomething() {
        alert("This should be moved into the global scope, so that it can be called from outside the function that encloses it.");
    }

    function alertSomethingElse() {
        alert("This should also be moved into the global scope.");
    }

    copyInto(["alertSomething", alertSomething, "alertSomethingElse", alertSomethingElse], window);
}

methodsToPutInGlobalScope();
alertSomething();
alertSomethingElse();
于 2013-04-24T17:29:53.623 回答
0

是的,这是可能的。如果您使用var关键字声明变量,则该变量仅成为本地变量,但如果您不这样做,它将成为全局变量。

function foo(){

   var test = 'test'; // <- Using var keyword
}

foo(); // <- execute the function

console.log(test); // undefined

但是如果我们在没有var关键字的情况下做同样的事情:

function foo(){

   test = 'test'; // <- Using var keyword
}

foo(); // <- execute the function

console.log(test); // test

为了使您的内部函数全局化,您需要声明不带关键字的匿名函数var

function methodsToPutInGlobalScope() {

    alertSomething = function () {
        alert("This should be moved into the global scope, so that it can be called from outside the function that encloses it.");
    }

    alertSomethingElse = function () {
        alert("This should also be moved into the global scope.");
    }

}


methodsToPutInGlobalScope(); // <- Don't forget to execute this function


alertSomething(); // Works
alertSomethingElse(); // Works as well
于 2013-04-24T17:41:09.343 回答