将JavaScript方法移动到全局范围

Move JavaScript methods into global scope

本文关键字:全局 范围 移动 JavaScript 方法      更新时间:2023-09-26

在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.");
    }
}

如果您不想在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.");
    }
}

实时演示

不是很复杂,但可以工作。

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();

是的,这是可能的。如果使用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