如果没有大括号就放进去 [英] let in if without braces
本文介绍了如果没有大括号就放进去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if (true) {
let x = 5
}
按预期工作(无语法错误),但
works as expected (no syntax error), but
if (true) let x = 5
throws SyntaxError:意外严格模式保留字
在Node 4.1.0和babel
throws SyntaxError: Unexpected strict mode reserved word
in Node 4.1.0 and babel
这是预期的行为吗?我知道这是一个愚蠢的例子。我只是想知道这是否是一个错误。
Is this expected behavior? I know that this is a stupid example. I'm just wondering wether this is a bug or not.
推荐答案
是的,是预期的行为。 的生产规则,如果
语句是
Yes it is expected behavior. The production rule of an if
statement is
if ( Expression[In, ?Yield] ) Statement[?Yield, ?Return]
但一个 let
声明不是一个声明
因此不允许在此位置:
but a let
declaration is not a Statement
and is therefore not allowed in this position:
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] ;
这篇关于如果没有大括号就放进去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文