将JavaScript方法移动到全局范围
在JavaScript中,是否可以将内部函数从一个函数移动到全局范围?我还没有找到任何直接的方法来做到这一点将JavaScript方法移动到全局范围,javascript,function,Javascript,Function,在JavaScript中,是否可以将内部函数从一个函数移动到全局范围?我还没有找到任何直接的方法来做到这一点 function moveMethodsIntoGlobalScope(functionName){ //move all of functionName's methods into the global scope //methodsToPutIntoGlobalScope should be used as the input for this function.
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.");
}
}
如果您不想在
方法StopUtilitGlobalSpace
中进行更改,可以使用以下恶意攻击:
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();
函数copyInto(arr,上下文){
//将functionName的所有方法移到全局范围中
//methodsToPutIntoGlobalScope应用作此函数的输入。
对于(变量i=0;i
不是很复杂,但可以工作
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();
函数copyInto(arr,上下文){
//将functionName的所有方法移到全局范围中
//methodsToPutIntoGlobalScope应用作此函数的输入。
对于(变量i=0;i
是的,这是可能的。如果您使用var
关键字声明变量,则该变量将仅成为局部变量,如果您不这样做,它将成为全局变量
function foo(){
var test = 'test'; // <- Using var keyword
}
foo(); // <- execute the function
console.log(test); // undefined
为了使内部函数全局化,您需要声明匿名函数,而不使用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
函数方法StoputinGlobalScope(){
alertSomething=函数(){
警报(“应该将其移动到全局范围,以便可以从包含它的函数外部调用它。”);
}
alertSomethingElse=函数(){
警报(“这也应移到全局范围内。”);
}
}
MethodStopRoutinGlobalScope();// 是的,这是可能的。如果您使用var
关键字声明变量,则该变量将仅成为局部变量,如果您不这样做,它将成为全局变量
function foo(){
var test = 'test'; // <- Using var keyword
}
foo(); // <- execute the function
console.log(test); // undefined
为了使内部函数全局化,您需要声明匿名函数,而不使用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
函数方法StoputinGlobalScope(){
alertSomething=函数(){
警报(“应该将其移动到全局范围,以便可以从包含它的函数外部调用它。”);
}
alertSomethingElse=函数(){
警报(“这也应移到全局范围内。”);
}
}
MethodStopRoutinGlobalScope();//这个答案仍然没有解释如何实现moveMethodsIntoGlobalScope
,这正是我最初尝试做的。没有简单的方法可以做到这一点。我想您需要解析moveMethodsIntoGlobalScope
(使用moveMethodsIntoGlobalScope.toString()
)然后在全局范围内声明内部函数。完成!您可以看到更改。您测试过此解决方案吗?我无法让它工作:它在控制台中产生了这个错误:uncaughtreferenceerror:alertSomething没有定义
我喜欢它,但是有一些函数。prototype.toString
需要警惕-。这个答案仍然没有解释如何将移动方法实现到lobalscope
,这正是我一开始想要做的。没有一个简单的方法可以做到这一点。我想您需要解析moveMethodsIntoGlobalScope
(使用moveMethodsIntoGlobalScope.toString()
)然后在全局范围内声明内部函数。完成!您可以看到更改。您测试过此解决方案吗?我无法让它工作:它在控制台中产生此错误:uncaughtreferenceerror:alertSomething未定义
我喜欢它,但有一些函数。prototype.toString
需要警惕-。这种方式不可能,您可以将methodsToPutInGlobalScope
设置为对象而不是函数,然后您就可以访问其中的函数。methodsToPutInGlobalScope
是否仅在其内部具有函数声明,或者是否还有其他声明?例如,使用eval可以工作,但如果函数中有多个函数声明,则可能不是您想要的。这种方式是不可能的,您可以将methodsToPutInGlobalScope
设置为对象而不是函数,然后您就可以访问其中的函数了。methodsToPutInGlobalScope
是否仅在其内部有函数声明,或者