関数宣言構文は、ブロックステートメント内では使用できません。
法的:
function a() {
function b() {
}
}
違法:
function a() {
if (c) {
function b() {
}
}
}
ただし、これは可能です。
function a() {
var b;
if (c) {
b = function() {
};
}
}
私たちの間の言語オタクについては、仕様のセクション12.1、13.1、および14を参照することをお勧めします。次の構文の説明があります。
12.1ブロック
構文
ブロック:
{ StatementList opt }
StatementList:
ステートメント
StatementListステートメント
13関数の定義
構文
FunctionDeclaration:
関数識別子( FormalParameterList opt ){ FunctionBody }
FunctionExpression:
function Identifier opt ( FormalParameterList opt ){ FunctionBody }
FormalParameterList:
識別子
FormalParameterList 、識別子
FunctionBody:
SourceElements
14プログラム
構文
プログラム:
SourceElements opt
SourceElements:
SourceElement
SourceElements SourceElement
SourceElement:
ステートメント
FunctionDeclaration