JavaScript - 为嵌套函数声明全局范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5198579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript - Declaring Global Scope for Nested Function?
提问by nestor
My attempts at giving global scope to a nested JavaScript function are not working:
我试图为嵌套的 JavaScript 函数提供全局范围的尝试不起作用:
//DECLARE FUNCTION B IN GLOBAL SCOPE
function B;
function A() {
//DEFINE FUNCTION B INSIDE NEST
B() {
alert("function B is running");
}
}
//CALL FUNCTION B FROM GLOBAL SCOPE
B();
This is just curiosity -- you are correct that I don't really have any good reason to want to do this.
这只是好奇心——你说得对,我真的没有任何好的理由想要这样做。
TIA -- I don't have an SO account to respond to your answers...
TIA - 我没有 SO 帐户来回复您的答案...
回答by Felix Kling
function B;will simply generate a syntax error.
function B;只会产生一个语法错误。
You can use a function expression. As functions are first class objects, you can assign a function to a variable:
您可以使用函数表达式。由于函数是第一类对象,您可以将函数分配给变量:
var B; // declare (global) variable (outer scope)
function A() {
// assign a function to it
B = function() {
alert("function B is running");
};
}
// we have to call A otherwise it won't work anyway
A();
// call B
B();
You could also let Areturn a function:
你也可以让A返回一个函数:
function A() {
return function() {
alert("function B is running");
};
}
B = A();
This would make the relation between Aand Ba bit clearer.
这将使之间的关系A和B更清楚一点。
Of course you can always define a global variable by omitting var, but you should use this very carefully. Use as less global variables as possible.
当然,您始终可以通过省略 来定义全局变量var,但是您应该非常小心地使用它。尽可能少使用全局变量。
function A() {
B = function() {
alert("function B is running");
};
}
And I bet there is a better way of doing it, depending on what your actual goal is.
我敢打赌,有更好的方法可以做到,这取决于您的实际目标是什么。
回答by Ross Here
What about:
关于什么:
function A() {
window.B = function() {
alert("function B is running");
}
}
//CALL FUNCTION B FROM GLOBAL SCOPE
B();
回答by Pointy
You can do something like this:
你可以这样做:
function outer() {
function inner() {
// ..
}
window['inner'] = inner;
}
It's a little icky to have a direct reference to "window", so you could do this (from the global context):
直接引用“window”有点麻烦,所以你可以这样做(从全局上下文):
(function (global) {
function inner() {
// code code code ...
}
global['inner'] = inner;
})(this);
回答by JaredPar
There appear to be a couple of issues with your code
您的代码似乎存在一些问题
- The first line doesn't appear to be legal Javascript (JSLint agrees). To declare an uninitialized variable use the
var B;syntax - The code never calls A to initialize B so calling B() is invoking an uninitialized variable
- I'm fairly certain the code to initialize B inside of A is also not legal.
- 第一行似乎不是合法的 Javascript(JSLint 同意)。要声明未初始化的变量,请使用
var B;语法 - 代码从不调用 A 来初始化 B 所以调用 B() 是调用一个未初始化的变量
- 我相当肯定在 A 内部初始化 B 的代码也不合法。
Try the following
尝试以下
var B; // Establish B as a global scope variable
function A() {
B = function() {
alert('B is running');
};
}
A(); // Call the function which will cause B to be initialized
B(); // Run B
回答by Robert Koritnik
You're close, but not completely correct.
你很接近,但不完全正确。
- You have to define B as a variable and then assign a function to it.
- Also run A() before executing B, otherwise B will be undefined. The easiest way of running it is the way that I show in my code example.
- 您必须将 B 定义为变量,然后为其分配函数。
- 在执行 B 之前也要运行 A(),否则 B 将是未定义的。最简单的运行方式是我在代码示例中展示的方式。
These are the smallest amount of changesto your code to make it work as you asked:
这些是对您的代码进行的最少更改,以使其按您的要求工作:
var B;
(function A() {
// define function B
B = function() {
alert("function B is running");
}
})();
// call function B globally
B();

