Javascript 条件切换语句

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

Javascript conditional switch statement

javascript

提问by AP257

Is there a way to write a conditional switch statement in javascript?

有没有办法在 javascript 中编写条件 switch 语句?

I'm guessing not, since the following is always going to default:

我猜不是,因为以下总是默认的:

    var raw_value = 11.0;
    switch(raw_value)
    {
        case (raw_value > 10.0):
          height = 48;
          width = 36;
          break;
        case (raw_value > 5.0):
          height = 40;
          width = 30;
          break;
        default:
          height = 16;
          width = 12;
          break;
    }

If not, what should I use instead - a long if/else statement?

如果没有,我应该使用什么来代替 - 一个很长的 if/else 语句?

thanks :)

谢谢 :)

采纳答案by Gumbo

In a switchstatement, the evaluated value of the switchexpression is compared the the evaluated values of the cases. So here the value of raw_value(number) is compared to raw_value > 10.0(comparison expression) and raw_value > 5.0(comparison expression).

switch语句中,将switch表达式的评估值与案例的评估值进行比较。所以这里将raw_value(number) 的值与raw_value > 10.0(comparison expression) 和raw_value > 5.0(comparison expression) 进行比较。

So unless one of your case expressions yield a number equal to 11.0or you use the switchexpression true, you will always get the default case.

因此,除非您的 case 表达式之一产生等于11.0或您使用该switch表达式的数字,否则true您将始终获得默认情况。

Just use a simple if/elseinstead:

只需使用简单的if/else代替:

var raw_value = 11.0;
if (raw_value > 10.0) {
    height = 48;
    width = 36;
} else if (raw_value > 5.0) {
    height = 40;
    width = 30;
} else {
    height = 16;
    width = 12;
}

回答by DanMan

Like this:

像这样:

var raw_value = 11.0;
switch(true) {
    case (raw_value > 10.0):
      height = 48;
      width = 36;
      break;
    case (raw_value > 5.0):
      height = 40;
      width = 30;
      break;
    default:
      height = 16;
      width = 12;
}

The expressions in the casestatements will evaluate to trueor false, and if that matches the switchcondition... voilà. The defaultacts like an else.

case语句中的表达式将计算为trueor false,如果它与switch条件匹配......瞧。该default就像一个else

Bonus: you can invert the whole logic by simply replacing truewith false. With if ... else ifstatements, you'd have to edit every if-clause individually.

奖励:您可以通过简单地替换true为来反转整个逻辑false。对于if ... else if语句,您必须单独编辑每个 if 子句。

回答by Juan Mendes

Don't try this at home, or take it too seriously, this is just for sugary fun...

不要在家里尝试这个,也不要太当真,这只是为了甜蜜的乐趣......

function conditionalSwitch(value, cond, callback /* cond, callback, cond, callback, ... */ ) {
  for (var i = 1; i < arguments.length; i += 2) {
    if (arguments[i](value)) {
      arguments[i + 1](value);
      return;
    }
  }
}



function test(val) {
  let width, height;

  conditionalSwitch(val,
  
    (val) => val > 10,
    () => [height, width] = [48,36],

    (val) => val > 5,
    () => [height, width] = [40, 30],

    // Default
    () => true,
    () => [height, width] = [16, 12]
  )
  console.log(width, height);
}


test(4.9);  // 12 16
test(5.1);  // 30 40
test(10.1); // 36 48

回答by Romain Linsolas

No, the switchstatement does not work used like that. However, this statement is not always simpler. For example, the switchversion takes 15 lines:

不,该switch语句不能像那样使用。然而,这种说法并不总是更简单。例如,该switch版本需要 15 行:

var raw_value = 11.0;
switch(raw_value) {
    case (raw_value > 10.0):
      height = 48;
      width = 36;
      break;
    case (raw_value > 5.0):
      height = 40;
      width = 30;
      break;
    default:
      height = 16;
      width = 12;
      break;
}

and the "long" if/elseversion only11:

而“长”if/else版本只有11:

var raw_value = 11.0;
if (raw_value > 10.0) {
      height = 48;
      width = 36;
} else if (raw_value > 5.0) {
      height = 40;
      width = 30;
} else {
      height = 16;
      width = 12;
}

So in your case, it is better to use the second one than the first...

所以在你的情况下,最好使用第二个而不是第一个......