javascript 如果条件中断

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6423960/
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-10-25 20:37:16  来源:igfitidea点击:

If condition break

javascript

提问by John Cooper

if(Condition) {

} 

if(condition) {

}

if(condition) { 

} else { 

}

If the first condition fails, it should break instead of executing the last if/elseconditon

如果第一个条件失败,它应该中断而不是执行最后一个if/else条件

回答by Lightness Races in Orbit

if (condition1) { 
   if (condition2) {
   }

   if (condition3) {
   }
   else {
   }
}

Or am I missing something?

或者我错过了什么?

回答by Lightness Races in Orbit

What type are we working with here? What is Condition?

我们在这里工作的是什么类型?什么是条件?

If you have more than 2 possible values for ConditionI recommend that you use Switch - Case

如果您有 2 个以上的可能值,Condition我建议您使用 Switch - Case

switch (Condition)
{
    case 'Case1' :
        // Insert logic here
        break;
    case 'Case2' :
        // Insert logic here
        break;
    case 'Case3' :
        // Insert logic here
        break;
}

回答by Lightness Races in Orbit

Seems to me, you could just do the following:

在我看来,您可以执行以下操作:

if(Conditon) {
  // code
} else if(Condition) {
  // code
} else if(Condition) {
  // code
} else {
  // code
}

Hope it helps!

希望能帮助到你!

回答by drdrej

all above named solutions are functionally correct. i think the most popular is this one:

以上所有解决方案在功能上都是正确的。我认为最受欢迎的是这个:

if(Conditon1) {
  // code
} else if(Condition2) {
  // code
} else if(Condition3) {
  // code
} else {
  // code
}

If you ask some Design-Pattern and Refactoring fans, you will maybe get this one:

如果你问一些 Design-Pattern 和 Refactoring 的粉丝,你可能会得到这个:

if(Conditon1) {
  return doSomething();
} 

if(Condition2) {
  return doSomething2();
} 

if(Condition3) {
  return doSomething3();
}

it depends on your programming-style and what kind of books you've read :)

这取决于你的编程风格和你读过什么样的书:)

回答by brymck

Erm, use nested if statements? I'm not entirely sure what you want to do with the result of the second condition, but here's what it looks like if the first condition isn't met:

嗯,使用嵌套的 if 语句?我不完全确定您想对第二个条件的结果做什么,但如果不满足第一个条件,则如下所示:

if (condition1) {
  // First condition succeeds

  // Assuming you want to execute this either way
  if (condition2) {
  }

  // Only execute if first condition succeeds
  if (condition3) {
  } else {
  }
} else {
  // First condition fails

  // Assuming you want to execute this either way; ignore otherwise
  if (condition2) {
  }
}

Note also that if you want to return a value from a function, you can use something like if (!condition1) return false;.

另请注意,如果要从函数返回值,可以使用类似if (!condition1) return false;.

As for whether the above is what you're looking for: this question is mad ambiguous.

至于以上是否是您要查找的内容:这个问题非常模棱两可。