将JavaScript方法移动到全局范围
Move JavaScript methods into global scope
在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
相关文章:
- 正在全局范围中查找JavaScript函数
- 为什么IIFE的这个变量指的是全局范围
- 从全局范围调用 jQuery 中的函数
- socket.io 客户端是否必须存在于全局范围内
- 从脚本标记的全局范围调用webpack模块中的函数
- 如何在 javascript 中更改方法调用的全局范围
- webpack在全局范围内执行模块
- ng-repeat=“(key, value)” 不能将 {{key}} 的值用作另一个指令的全局范围的一部分
- JS:增量计数器,不在全局范围内设置计数器变量
- (文档).ready是一个全局范围
- 如何在Jint中从全局范围中获取所有变量(和函数名)
- Chrome扩展:如何在全局范围内捕获/处理内容脚本错误
- 在全局范围内执行文件,而不是返回导出的对象
- 在全局范围内访问嵌套变量的 Nodejs
- AngularJS需要JS浏览器化和Javascript模块/全局范围的噩梦
- JavaScript 中全局范围内存在未定义变量的原因是什么?
- 全局范围内的函数表达式与函数声明的区别
- 在函数内设置全局范围变量
- 全局范围内的变量在 javascript 中无法识别
- 我应该使用 self 还是窗口来引用全局范围