gpt4 book ai didi

javascript - 在 Ecmascript 5.1 中,哪里提到 if 语句中的左花括号是 Not Acceptable ?

转载 作者:行者123 更新时间:2023-11-29 17:22:24 29 4
gpt4 key购买 nike

我不记得 if 语句的规则,所以我查看了第 12.5 条,但我仍然不清楚是否需要大括号。那么,我想问一下,在 Ecmascript 5.1 中哪里提到 if 语句中没有左花括号是可以接受的?

最佳答案

这是 12.5 节中的 if 语句的语法:

IfStatement :     if ( Expression ) Statement else Statement     if ( Expression ) Statement

From the looks of it no curly braces are required but we need to clarify what exactly a statement is. This syntax is found in section 12.

Statement :     Block     VariableStatement     EmptyStatement     ExpressionStatement     IfStatement     IterationStatement     ContinueStatement     BreakStatement     ReturnStatement     WithStatement     LabelledStatement     SwitchStatement     ThrowStatement     TryStatement     DebuggerStatement 

If we dig a bit deeper into section 12.1 we'll find the definition of the block statement.

Block :     { StatementListopt } 

We found the curly braces but does this mean they are required? The answer is no. When examining the syntax of a statement each element following the colon is considered a statement and therefore a valid part of the if statement syntax. The following are actual examples of valid if statements.

// 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;

关于javascript - 在 Ecmascript 5.1 中,哪里提到 if 语句中的左花括号是 Not Acceptable ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11745617/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com