问题描述
在为我的小型代码库制作几个测试项目时,我遇到了许多教程,这些教程以许多不同的方式来制作函数.
While making several test projects for my small library of code I have come across many tutorials that go about making functions in many different ways.
-
函数声明
Function Declarations
-
FunctionDeclaration :function Identifier ( FormalParameterList opt ){ FunctionBody }
FunctionDeclaration :function Identifier ( FormalParameterList opt ){ FunctionBody }
函数表达式
-
FunctionExpression :function Identifier opt ( FormalParameterList opt ){ FunctionBody }
FunctionExpression :function Identifier opt ( FormalParameterList opt ){ FunctionBody }
匿名自执行功能
-
(function(msg){alert(msg);})('SO'); // alerts "SO"
(function(msg){alert(msg);})('SO'); // alerts "SO"
匿名自执行函数可能不正确,但我想确保
Anonymous Self-Executing Functions that I have seen in source code that I suspect may be incorrect but I want to make sure
-
(function(msg){alert(msg);}('SO')); // alerts "SO", but the passed parameter appears inside the parenthesis that wrap the function, not after like I think is correct.
(function(msg){alert(msg);}('SO')); // alerts "SO", but the passed parameter appears inside the parenthesis that wrap the function, not after like I think is correct.
说了这么多,我想澄清一下关于每种功能类型的几个问题.
函数声明:
function display(){
//this function is evaluated at parse time,
can be called any time
}
是否可以使用问题似乎表明并且此源代码似乎要备份吗?:
Is it true that a function declaration (that is evaluated at parse time and does not require parenthesis after the brackets) can be overwritten by function expressions as this question seems to indicate and this source code seems to back up?:
var display = function(){
//this function will be called as opposed to the
//display() declaration (above) any time
//display() is used after this variable assignment
};
函数表达式:
var display = function nameOptional(){
};
函数表达式中括号后的分号是否已分配给变量?而且,这是否也意味着此:
Is the semi-colon after the bracket in function expressions that has been assigned to a variable necessary? And also, does that mean that this:
function name(){
//...
};
不同于此:?
function name(){
//...
}//no semi-colon
第一个name()
是函数表达式,第二个name()
是函数声明吗?或者,第一个name()
是函数表达式,第二个name()
是具有可选 left-out 分号的函数表达式.因为我已经阅读过函数表达式中的内容,所以仅建议使用分号.令我感到困惑的是,在函数声明中,您必须有一个标识符和一个主体.在表达式中,标识符是可选的,但是您必须具有主体.所以如果你有
Is the first name()
a function expression, and the second name()
a function declaration? Or is the first name()
a function expression and the second name()
a function expression with an optional left-out semi-colon. Because I have read that in function expressions, the semi-colon is only recommended.What confuses me is that in a function declaration you must have an identifier, and a body. In an expression, the identifier is optional, but you must have a body. So if you have
function identifier(){
//...body...
}
是声明还是表达式,因为表达式或声明中不需要分号.对于表达式,仅在变量分配的情况下才建议获得最佳结果.
is this a declaration, or an expression since the semi-colon is not required on expressions or declarations. On expressions it is only recommended for optimum results in the case of variable assigning.
我知道这些是如何工作的,但是我已经看到了两种写法,一种我几乎可以肯定是错误的.我只想解决这个问题:
I understand how these work, but I have seen two ways of writing this, one I am almost certain is wrong. I just would like to clear this up:
(function(msg){
alert(msg);
})("SO");
和
(function(msg){
alert(msg);
}("SO"));
我已经读到,执行匿名函数的原因是因为它包装在()
中,并且如果您希望将任何参数传递给它,则必须在这些()
之后和分号绑定之前添加这些参数.整件事,如果这是真的...第二个匿名函数编写不正确...是真的吗?
I have read that the reason an anonymous function executes is because it is wrapped in ()
and if you want any parameters to be passed to it, they must be added after those ()
and before a semi-colon tying the whole thing off, if this is true...the second anonymous function is written incorrectly...is this true?
推荐答案
只有您在此处确定的前两种函数是真实的".另外两个是函数表达式的应用.
Only the first two types of functions you've identified here are "real". The other two are applications of function expressions.
依次回答您的问题:
是的. (不幸的是.)第二次声明一个函数会覆盖第一个定义.但是,如果已将原始功能的引用分配给变量,则仍可以访问该原始功能.例如:
Yes. (Unfortunately.) Declaring a function a second time overwrites the first definition. The original function may still be accessible if a reference to it has been been assigned to a variable, though; for instance:
function a() { console.log(1); }
var a1 = a;
function a() { console.log(2); }
a1(); // outputs "1"
a(); // outputs "2"
通常,是—它表示语句的结尾,就像var a = 1;
中的分号一样.与Java中的大多数分号一样,它有时是可选的,但不建议这样做.
Generally, yes — it denotes the end of the statement, just like the semicolon in var a = 1;
. It's sometimes optional, like most semicolons in Javascript, but omitting it is inadvisable.
在函数声明的末尾,分号不是必需的,也不是有意义的.实际上,它被解释为声明之间的独立分号.
The semicolon is not required, or meaningful, at the end of a function declaration. It's actually interpreted as a standalone semicolon in between the declarations, which has no effect.
他们都很好;这是一种风格选择.该行必须以function
以外的其他内容开头,因此它不会被解释为函数定义,因此使用了初始括号,但是括号的确切位置并不重要.如果用a
替换整个函数表达式,则比较只是在(a)()
和(a())
之间进行;没有什么真正的区别.
They're both fine; it's a stylistic choice. The line has to begin with something other than function
, so that it's not interpreted as a function definition, so an initial parenthesis is used, but the exact placement of the parentheses isn't significant. If we replace the whole function expression with a
, the comparison is simply between (a)()
and (a())
; there's no real difference.
这篇关于所有JavaScript函数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!