Javascript 在`if...else if` 语句中是否需要最后一个`else` 子句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38536554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need a last `else` clause in an `if...else if` statement?
提问by chharvey
Every if...else if
example I've seen includes a final else
clause:
if...else if
我见过的每个例子都包含一个最后的else
子句:
if (condition1) {
doA();
} else if (condition2) {
doB();
} else if (condition3) {
doC();
} else {
noConditionsMet();
}
alwaysDoThis();
I understand that this is basically syntactic sugar for nested if...else
statements:
我知道这基本上是嵌套if...else
语句的语法糖:
if (condition1) {
doA();
} else {
if (condition2) {
doB();
} else {
if (condition3) {
doC();
} else {
noConditionsMet();
}
}
}
alwaysDoThis();
I have never seen any examples of an if...else if
that omits the last else
clause. But seeing as plain if
statements (without else
clauses) are valid, and going by the equivalent “nested statements” above, my gut tells me that this is okay to do:
我从来没有见过的任何例子if...else if
是省略了最后else
条款。但是看到简单的if
语句(没有else
子句)是有效的,并且通过上面等效的“嵌套语句”,我的直觉告诉我这样做是可以的:
if (condition1) {
doA();
} else if (condition2) {
doB();
} else if (condition3) {
doC();
}
alwaysDoThis();
Can someone point me to a resource or example that explicitly says whether or not it's valid?
有人可以指出我明确说明它是否有效的资源或示例吗?
And on another level, if it isvalid, would it be recommended or is it considered “bad practice”?
在另一个层面上,如果它是有效的,它会被推荐还是被认为是“不好的做法”?
采纳答案by user4815162342
The ending else
is not mandatory. As for whether it is needed, it depends on what you want to achieve.
结尾else
不是强制性的。至于是否需要,就看你想达到什么目的了。
The trailing else
clause will execute when none of the specified conditions is true. If the conditions are guaranteed to be collectively exhaustive, then an else
clause is entirely superfluous, except possibly to contain an assertion that catches the "impossible" condition. In your case, whether you need an else
clause depends on whether you want specific code to run if and only if neither of condition1
, condition2
, and condition3
are true.
else
当指定的条件都不为真时,将执行尾随子句。如果保证条件是整体详尽的,那么else
子句就完全是多余的,除非可能包含捕获“不可能”条件的断言。在你的情况,你是否需要一个else
条款取决于您是否希望特定的代码,当且仅当没有运行condition1
,condition2
以及condition3
是真实的。
else
can be omitted for anyif
statement, there is nothing special in the last if
of an if
/else if
chain. This is documented in any JavaScript grammar, e.g. in the specification.
else
可以省略任何if
声明,没有在最后没有什么特别if
的的if
/else if
链。这在任何 JavaScript 语法中都有记录,例如在规范中。
回答by Pointy
You never needan else
clause. (It's hard to offer examples of something that is not necessary, so I'll leave it at that.)
你永远不需要一个else
条款。(很难举出一些不必要的例子,所以我会留在这里。)
editas a comment notes, square brackets in language syntax notation usuallyindicate that something is optional.
作为注释注释编辑,语言语法符号中的方括号通常表示某些内容是可选的。
回答by KevBot
It is 100% valid. No, it is not bad practice.If you don't need it, don't write it.
它是 100% 有效的。不,这不是坏习惯。如果你不需要它,不要写它。
Take the following for example:
以以下为例:
function doStuff(data) {
if (data.something) {
// go format the data in some way
}
else if (data.somethingElse) {
// go format the data in some other way
}
// must be formatted correctly, don't do anything else
...
}
回答by Ivan
No need for a else block, they are not if else statement but if statements. Consider else, elseif as an extension.
不需要 else 块,它们不是 if else 语句而是 if 语句。考虑 else, elseif 作为扩展。
Here's a link to a sitepoint thread : click this. And a similar thread on stackoverflow : click here