我正在查看EcmaScript.NET中的一些代码,特别是,我正在查看FunctionNode.cs中的定义。他们在定义之上提供了一个相对描述性的评论,但我不确定下面的例子符合以下条件:
/// <summary>
/// There are three types of functions that can be defined. The first
/// is a function statement. This is a function appearing as a top-level
/// statement (i.e., not nested inside some other statement) in either a
/// script or a function.
///
/// The second is a function expression, which is a function appearing in
/// an expression except for the third type, which is...
///
/// The third type is a function expression where the expression is the
/// top-level expression in an expression statement.
///
/// The three types of functions have different treatment and must be
/// distinquished.
/// </summary>
public const int FUNCTION_STATEMENT = 1;
public const int FUNCTION_EXPRESSION = 2;
public const int FUNCTION_EXPRESSION_STATEMENT = 3;
以下是我大致看到的内容:
<script>
(function(){document.write("The name is Bond, ")})(),
(function(){document.write("James Bond.")})()
</script>
这是否有资格成为FUNCTION_STATEMENT
,FUNCTION_EXPRESSION
,FUNCTION_EXPRESSION_STATEMENT
?
我想我的问题是关于逗号的作用:
// Expression
(function(){ document.write('Expression1<br>'); })();
(function(){ document.write('Expression2<br>'); })();
// Expression
var showAlert=function(){ document.write('Expression3<br>'); };
showAlert();
// Declaration
function doAlert(){ document.write('Declaration<br>'); }
doAlert();
// What about this?
(function(){ document.write('What about'); })(), // <-- Note the comma
(function(){ document.write(' this?<br>'); })();
// And now this?
var a = ((function(){ return 1; })(), // <-- Again, a comma
(function(){ return 2; })());
document.write("And now this? a = " + a);
最后两个是什么?表达式或表达式声明?
答案 0 :(得分:4)
不了解EcmaScript.NET,但据我所知,所有这些函数都是函数表达式。它们是IIFE - 调用的一部分,它也是comma-operator表达式的一部分 - 绝不是“顶级”。
我会说第三种类型是函数语句,它们在语法上是不允许的,比如
if (false) {
function doSomething() {…}
}
查看Kangax' famous article about named function expressions,了解他们在引擎间的行为总结(例如Gecko的function statements)。