2
我不記得if語句的規則,所以我查看了第12.5節,但對於我而言,如果大括號是必要的,它仍然不明顯。那麼,我問,在Ecmascript 5.1中它提到if語句中沒有左括號可以接受?Ecmascript 5.1中提到在if語句中沒有左大括號可以接受?
我不記得if語句的規則,所以我查看了第12.5節,但對於我而言,如果大括號是必要的,它仍然不明顯。那麼,我問,在Ecmascript 5.1中它提到if語句中沒有左括號可以接受?Ecmascript 5.1中提到在if語句中沒有左大括號可以接受?
這裏是的,如果在12.5節中語句的語法:
IfStatement : if ( Expression ) Statement else Statement if ( Expression ) Statement
從不需要花括號它的外觀,但我們需要澄清的聲明到底是什麼。這個語法見第12節。
Statement : Block VariableStatement EmptyStatement ExpressionStatement IfStatement IterationStatement ContinueStatement BreakStatement ReturnStatement WithStatement LabelledStatement SwitchStatement ThrowStatement TryStatement DebuggerStatement
如果我們深入到12.1節,我們會找到塊語句的定義。
Block : { StatementListopt}
我們發現了大括號,但這是否意味着它們是必需的?答案是不。在檢查語句的語法時,冒號後面的每個元素都被視爲語句,因此是if語句語法的有效部分。以下是有效if語句的實際示例。
// if (Expression) ReturnStatement
if (true)
return false;
// if (Expression) Block
if (true) {
return false;
}
// if (Expression) TryStatement
if (true) try {
return false;
} catch (e) {
return e;
}
// if (Expression) EmptyStatement
if (true) ;
// if (Expression) IfStatement
if (true)
if (false)
return false;
哇,非常感謝你。 – 2012-08-29 00:02:23