if (true) {
let x = 5
}
按预期工作(没有语法错误),但
if (true) let x = 5
在节点4.1.0和babel
中抛出SyntaxError: Unexpected strict mode reserved word
这是预期的行为吗?我知道这是一个愚蠢的例子。我只是想知道这是不是一个错误。
答案 0 :(得分:9)
是的,这是预期的行为。 The production rule of an if
statement是
if ( Expression[In, ?Yield] ) Statement[?Yield, ?Return]
但是let
声明is not a Statement
,因此不允许这个位置:
Statement[Yield, Return] :
BlockStatement[?Yield, ?Return]
VariableStatement[?Yield]
EmptyStatement
ExpressionStatement[?Yield]
IfStatement[?Yield, ?Return]
BreakableStatement[?Yield, ?Return]
ContinueStatement[?Yield]
BreakStatement[?Yield]
[+Return] ReturnStatement[?Yield]
WithStatement[?Yield, ?Return]
LabelledStatement[?Yield, ?Return]
ThrowStatement[?Yield]
TryStatement[?Yield, ?Return]
DebuggerStatement
Declaration[Yield] :
HoistableDeclaration[?Yield]
ClassDeclaration[?Yield]
LexicalDeclaration[In, ?Yield]
LexicalDeclaration[In, Yield] :
LetOrConst BindingList[?In, ?Yield] ;