JavaScript 嵌套函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7295634/
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 Nested function
提问by Thomas
I got a piece of code for javascript which I just do not understand:
我得到了一段我不明白的 javascript 代码:
function dmy(d) {
function pad2(n) {
return (n < 10) ? '0' + n : n;
}
return pad2(d.getUTCDate()) + '/' +
pad2(d.getUTCMonth() + 1) + '/' +
d.getUTCFullYear();
}
function outerFunc(base) {
var punc = "!";
//inner function
function returnString(ext) {
return base + ext + punc;
}
return returnString;
}
How can a function be defined within another function? Can we call pad2() from outside of my() function?
如何在另一个函数中定义一个函数?我们可以从 my() 函数外部调用 pad2() 吗?
Please put some light on it. Thanks
请给它点灯。谢谢
回答by zzzzBov
Functions are another type of variable in JavaScript (with some nuances of course). Creating a function within another function changes the scope of the function in the same way it would change the scope of a variable. This is especially important for use with closures to reduce total global namespace pollution.
函数是 JavaScript 中的另一种变量(当然有一些细微差别)。在另一个函数中创建一个函数会改变函数的作用域,就像改变变量的作用域一样。这对于与闭包一起使用以减少全局命名空间污染尤其重要。
The functions defined within another function won't be accessible outside the function unless they have been attached to an object that is accessible outside the function:
在另一个函数中定义的函数将无法在函数外部访问,除非它们已附加到可在函数外部访问的对象:
function foo(doBar)
{
function bar()
{
console.log( 'bar' );
}
function baz()
{
console.log( 'baz' );
}
window.baz = baz;
if ( doBar ) bar();
}
In this example, the baz function will be available for use after the foo
function has been run, as it's overridden window.baz
. The bar function will not be available to any context other than scopes contained within the foo
function.
在此示例中, baz 函数将在该foo
函数运行后可用,因为它已被覆盖window.baz
。除了函数中包含的作用域之外,bar 函数将不可用于任何上下文foo
。
as a different example:
作为一个不同的例子:
function Fizz(qux)
{
this.buzz = function(){
console.log( qux );
};
}
The Fizz
function is designed as a constructor so that, when run, it assigns a buzz
function to the newly created object.
该Fizz
函数被设计为一个构造函数,以便在运行时为buzz
新创建的对象分配一个函数。
回答by Tadeck
It is called closure.
它被称为闭包。
Basically, the function defined within other function is accessible only within this function. But may be passed as a result and then this result may be called.
基本上,在其他函数中定义的函数只能在该函数中访问。但可以作为结果传递,然后可以调用此结果。
It is a very powerful feature. You can see more explanation here:
这是一个非常强大的功能。你可以在这里看到更多的解释:
回答by Andreas
function x() {}
is equivalent (or very similar) to
相当于(或非常相似)
var x = function() {}
unless I'm mistaken.
除非我弄错了。
So there is nothing funny going on.
所以没有什么有趣的事情发生。
回答by 0x499602D2
Function-instantiation is allowed inside and outside of functions. Inside those functions, just like variables, the nested functions are local and therefore cannot be obtained from the outside scope.
函数内部和外部都允许函数实例化。在这些函数内部,就像变量一样,嵌套函数是局部的,因此无法从外部作用域获取。
function foo() {
function bar() {
return 1;
}
return bar();
}
foo
manipulates bar
within itself. bar
cannot be touched from the outer scope unless it is defined in the outer scope.
foo
bar
在自己内部操纵。bar
除非在外部作用域中定义,否则不能从外部作用域触及。
So this will not work:
所以这行不通:
function foo() {
function bar() {
return 1;
}
}
bar(); // throws error: bar is not defined
回答by pedrochaves
When you declare a function within a function, the inner functions are only available in the scope in which they are declared, or in your case, the pad2
can only be called in the dmy
scope.
当您在函数中声明函数时,内部函数仅在声明它们的范围内可用,或者在您的情况下,pad2
只能在该dmy
范围内调用。
All the variables existing in dmy
are visible in pad2
, but it doesn't happen the other way around :D
中存在的所有变量都在dmy
中可见pad2
,但反过来不会发生:D
回答by Joaquim Rendeiro
It's perfectly normal in Javascript (and many languages) to have functions inside functions.
在 Javascript(和许多语言)中,在函数中包含函数是完全正常的。
Take the time to learn the language, don't use it on the basis that it's similar to what you already know. I'd suggest watching Douglas Crockford's series of YUI presentations on Javascript, with special focus on Act III: Function the Ultimate(link to video download, slides, and transcript)
花时间学习这门语言,不要因为它与你已经知道的相似而使用它。我建议观看 Douglas Crockford 关于 Javascript 的一系列 YUI 演示,特别关注第三幕:终极功能(视频下载、幻灯片和脚本的链接)
回答by Justin Liu
function foo() {
function bar() {
return 1;
}
}
bar();
会抛出错误。由于
bar
bar
是在里面定义的foo
foo
,所以bar
bar
只能在里面访问foo
foo
。要使用
bar
bar
你需要在里面运行它foo
foo
。function foo() {
function bar() {
return 1;
}
bar();
}