【问题标题】:JS - If multiple conditions can be true at the same time, which one runs?JS - 如果多个条件可以同时为真,哪一个运行?
【发布时间】:2020-03-25 05:46:50
【问题描述】:

如果多个 if 语句可以同时为真,那么 JS 如何确定运行哪一个?在下面的示例中,我知道“酷,现在我自己有很多玉米片”。将被退回,但我只是想知道这是如何确定的?如果不是friendsAtYourParty === 0 而是friendsAtYourParty < 3 会怎样(“酷,现在我自己有很多玉米片。​​”会再次返回,但为什么呢?)。我在想也许是更具体/更接近“获胜”价值的那个?


if (friendsAtYourParty === 0) {
  console.log("Cool, now I have a lot of nachos to myself.");
} else if (friendsAtYourParty < 4) {
  console.log("Perfect amount to play some Mario Kart.");
} else {
  console.log("Wooooo turn on the dance music!");
}

【问题讨论】:

  • 第一个匹配的。你毕竟在使用if/else - else 部分将仅在if 部分不被执行时执行。

标签: javascript if-statement


【解决方案1】:

当你有一个if/else 链时只有第一个匹配的if 条件将被执行。毕竟,if/else 就是这样工作的:

if (condition) {
  //only executed when condition is true
} else {
  //only executed when condition is false 
}

当条件为true 时,else 部分将不会执行。当您拥有多个 if/else 时,没有任何变化:

if (condition1) {
  //only executed when condition1 is true
} else if (condition2) {
  //only executed when condition2 is true and condition1 is false
} else {
 //only executed when both condition1 and condition2 are false
}

if/else 链描述了以下逻辑流程,因此它的评估总是可以预测的:

【讨论】:

    【解决方案2】:

    基本上第一个真正的条件运行。这是因为 JS 代码逐行运行和执行。当 JS 引擎看到 if 语句并且条件为 false 时,它​​会继续读取代码。当它看到条件为真时,它执行在条件之后声明的语句,然后从调用堆栈中删除 if 语句。换句话说:它在看到 true 后不会再读取其他条件。它会忽略它们

    【讨论】:

    • "这是因为 JS 代码逐行运行和执行。" 但是在 Java、C、C++ 中存在 相同 机制, PHP、Python、Ruby 以及我所知道的任何其他语言,包括编译和解释。所以不,这不是原因。这是遵循您给计算机的说明的唯一方法。
    【解决方案3】:

    让我们考虑 friendsAtYourParty 的值为 8

    1. if (friendsAtYourParty === 0) { // 8 == 0 返回 false,因此它会跳过此块

    2. console.log("酷,现在我自己有很多玉米片。​​");

    3. } else if (friendsAtYourParty // 8

    4. console.log("玩马里奥赛车的完美数量。");

    5. } else { // else 中没有条件,所以如果上述块中的所有条件都失败,它将执行此

    6. console.log("Wooooo 打开舞曲!");

    7. }

    让我们考虑另一种情况,friendsAtYourParty 为 0

    1. if (friendsAtYourParty === 0) { // 0 == 0 返回 true,因此它运行此块并退出 if else 链并忽略 if else 链中的所有块

    2. console.log("酷,现在我自己有很多玉米片。​​");

    3. } else if (friendsAtYourParty // 0

    4. console.log("玩马里奥赛车的完美数量。");

    5. } else { // 这也被忽略了

    6. console.log("Wooooo 打开舞曲!");

    7. }

    按 Ctrl+shift+i 打开开发工具,运行以下 sn-p 和 按f19逐步了解流程。

    debugger
    var friendsAtYourParty=8;
    if (friendsAtYourParty === 0) {
      console.log("Cool, now I have a lot of nachos to myself.");
    } else if (friendsAtYourParty < 4) {
      console.log("Perfect amount to play some Mario Kart.");
    } else {
      console.log("Wooooo turn on the dance music!");
    }
    friendsAtYourParty=0;
    if (friendsAtYourParty === 0) {
      console.log("Cool, now I have a lot of nachos to myself.");
    } else if (friendsAtYourParty < 4) {
      console.log("Perfect amount to play some Mario Kart.");
    } else {
      console.log("Wooooo turn on the dance music!");
    }

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      相关资源
      最近更新 更多