在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.");
}
}
答案 0 :(得分: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.");
}
}
答案 1 :(得分: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();
答案 2 :(得分: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