1

私は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);

最後の2つは何ですか?式または式ステートメント?

4

1 に答える 1

4

EcmaScript.NETについてはわかりませんが、私の理解では、これらの関数はすべて関数式です。これらはIIFE呼び出しの一部であり、これもコンマ演算子式の一部であり、決して「トップレベル」ではありません。

3番目のタイプは、構文的に許可されていない関数ステートメントです。

if (false) {
    function doSomething() {…}
}

名前付き関数式に関するKangaxの有名な記事をチェックしてください。ここでは、エンジン全体での動作が要約されています(Geckoの関数ステートメントなど)。

于 2012-12-03T23:06:37.063 に答える