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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:34:34  来源:igfitidea点击:

Do I need a last `else` clause in an `if...else if` statement?

javascriptif-statementconditional

提问by chharvey

Every if...else ifexample I've seen includes a final elseclause:

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...elsestatements:

我知道这基本上是嵌套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 ifthat omits the last elseclause. But seeing as plain ifstatements (without elseclauses) 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 elseis not mandatory. As for whether it is needed, it depends on what you want to achieve.

结尾else不是强制性的。至于是否需要,就看你想达到什么目的了。

The trailing elseclause will execute when none of the specified conditions is true. If the conditions are guaranteed to be collectively exhaustive, then an elseclause is entirely superfluous, except possibly to contain an assertion that catches the "impossible" condition. In your case, whether you need an elseclause depends on whether you want specific code to run if and only if neither of condition1, condition2, and condition3are true.

else当指定的条件都不为真时,将执行尾随子句。如果保证条件是整体详尽的,那么else子句就完全是多余的,除非可能包含捕获“不可能”条件的断言。在你的情况,你是否需要一个else条款取决于您是否希望特定的代码,当且仅当没有运行condition1condition2以及condition3是真实的。

elsecan be omitted for anyifstatement, there is nothing special in the last ifof an if/else ifchain. This is documented in any JavaScript grammar, e.g. in the specification.

else可以省略任何if声明,没有在最后没有什么特别if的的if/else if链。这在任何 JavaScript 语法中都有记录,例如在规范中

回答by Pointy

You never needan elseclause. (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

这是指向站点线程的链接:单击此。和 stackoverflow 上的一个类似线程:单击此处