如果开关/案例可以比较多个值?

时间:2021-06-24 12:36:59

here is what I have now,

这是我现在得到的,

switch (true) {
  case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
  case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
  case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
    break;

does switch accept multiple expression or can I use || and && in case value?

开关是否接受多重表达式,或者我是否可以使用||和& in case值?

If not, I'll just use if/else instead

如果没有,我就用If /else代替

3 个解决方案

#1


1  

I have absolutely no idea what you are trying to do but I will try and explain your code. Here is your code:

我完全不知道您要做什么,但是我将尝试解释您的代码。这是你的代码:

switch (true) {
  case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
  case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
  case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
    break;

Explanation

解释

All that will happen is the expression will be evaluated, in this case the expression is the true within the switch and since true is true, execution will go into the block if any of the cases provided matches a true. Otherwise execution will not go into the block.

所发生的一切是表达式将被计算,在这种情况下,表达式在交换机中是true,由于true是true,如果提供的任何情况都匹配true,那么执行将进入块。否则执行将不会进入到块中。

Here is a test. Please note I have added the number 1 to the end of the variable values so none of the cases check to true:

这是一个测试。请注意,我在变量值的末尾添加了数字1,所以没有一个情况是正确的:

var moveOneType = "rock1";
var moveTwoType = "paper1"; 
var moveThreeType = "scissors1";

switch(true)
{
    case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
        alert('case 1');
    case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
        alert('case 2');
    case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
        alert('case 3');
        break;
}

<== Fiddle Me ==>

< = =小提琴我= = >

Now that it is clear how execution goes into the block, here is another test which will clarify why your code is full of surprises. In the code below we are switching on the value of hello and since it equals some garbage, execution will go inside the block. Why? Because case 'some garbage' evaluates to true. So guess what: Since you have no break between the cases, every case will be evaluated and you will see 4 alerts.

现在很清楚执行是如何进入这个块的,下面是另一个测试,它将阐明为什么您的代码充满了惊喜。在下面的代码中,我们打开hello的值,因为它等于一些垃圾,所以执行将进入block中。为什么?因为case 'some garbage'的值是true。所以你猜怎么着:既然你在案例之间没有间断,每个案例都会被评估,你会看到4个警报。

var moveOneType = "rock";
var moveTwoType = "paper"; 
var moveThreeType = "scissors";

var hello = 'some garbage';

switch(hello)
{
    case 'some garbage': alert('garbage');
    case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
        alert('case 1');
    case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
        alert('case 2');
    case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
        alert('case 3');
    break;
}

<== Fiddle Me ==>

< = =小提琴我= = >

If you do not want all the cases to be evaluated then add a break after the case.

如果您不希望所有的案例都被评估,那么在案例之后添加一个break。

does switch accept multiple expression

交换机接受多重表达式吗

Sure it does as shown below. The expression moveOneType === "rock" && moveTwoType === "paper" will be evaluated and the value will be true and if at least one case is also true then execution will go inside the block:

当然,如下所示。表达式moveOneType === "rock" & moveTwoType === "paper"将被评估,该值将为true,如果至少有一个case为true,那么执行将进入block中:

var moveOneType = "rock";
var moveTwoType = "paper"; 
var moveThreeType = "scissors";

switch(moveOneType === "rock" && moveTwoType === "paper")
{
    case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
        alert('case 1');
    case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
        alert('case 2');
    case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
        alert('case 3');
        break;
}

<== Fiddle Me ==>

< = =小提琴我= = >

#2


3  

Your code is fine, although it is probably not the most common use of switch.

您的代码很好,尽管它可能不是最常见的switch用法。

The reason why it works is that the expression in the case statement is evaluated and the result is used, so a case statement with an expression evaluating to true would match in switch(true).

它起作用的原因是对case语句中的表达式进行了计算并使用了结果,所以表达式为true的case语句在switch中会匹配(true)。

Here is a simple example

这里有一个简单的例子

function getValue() {
  console.log('get value');
  return 2;
}


switch (true) {
case getValue() === 1 || getValue() === 2:
  console.log('ok');
  break;
case false:
  console.log('not ok');
  break;
default:
  break;
}

You can see that it outputs get value twice, one for the first getValue() and one for the second (i.e. after the ||). The result evaluates to true, as getValue() === 2 returns 2. As we are using switch(true), as long as the expression in the case statement returns true, the case statement will be executed, so in this example, ok will be outputted.

您可以看到,它输出get值两次,第一次getValue()一次,第二次(即||之后)。结果计算为true, getValue() === 2返回2。当我们使用switch(true)时,只要case语句中的表达式返回true,则case语句将被执行,因此在本例中,ok将被输出。

Now, I am not exactly sure what your switch statement is supposed to do in the first place, so I cannot tell you if this is a good way to handle your issue or not, but it is by no mean incorrect to use an expression returning a boolean and match it with true.

现在,我不确定你的switch语句是什么应该做的,所以我不能告诉你这是一个很好的方式来处理你的问题,但这并不意味着不正确使用一个表达式返回一个布尔匹配和真实的。

#3


2  

You have to use if/else instead. Your switch statement can be an expression. But the case conditions are the value you are the possible value you are expecting from the output of your switch statement. Here is the Documentation

你必须用if/else代替。您的switch语句可以是一个表达式。但情况是你的值是你期望从switch语句输出的值。这是文档

    var moveOneType;

    if(moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors"){}
    else if(moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors"){}
    else if(moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors"){}

#1


1  

I have absolutely no idea what you are trying to do but I will try and explain your code. Here is your code:

我完全不知道您要做什么,但是我将尝试解释您的代码。这是你的代码:

switch (true) {
  case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
  case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
  case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
    break;

Explanation

解释

All that will happen is the expression will be evaluated, in this case the expression is the true within the switch and since true is true, execution will go into the block if any of the cases provided matches a true. Otherwise execution will not go into the block.

所发生的一切是表达式将被计算,在这种情况下,表达式在交换机中是true,由于true是true,如果提供的任何情况都匹配true,那么执行将进入块。否则执行将不会进入到块中。

Here is a test. Please note I have added the number 1 to the end of the variable values so none of the cases check to true:

这是一个测试。请注意,我在变量值的末尾添加了数字1,所以没有一个情况是正确的:

var moveOneType = "rock1";
var moveTwoType = "paper1"; 
var moveThreeType = "scissors1";

switch(true)
{
    case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
        alert('case 1');
    case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
        alert('case 2');
    case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
        alert('case 3');
        break;
}

<== Fiddle Me ==>

< = =小提琴我= = >

Now that it is clear how execution goes into the block, here is another test which will clarify why your code is full of surprises. In the code below we are switching on the value of hello and since it equals some garbage, execution will go inside the block. Why? Because case 'some garbage' evaluates to true. So guess what: Since you have no break between the cases, every case will be evaluated and you will see 4 alerts.

现在很清楚执行是如何进入这个块的,下面是另一个测试,它将阐明为什么您的代码充满了惊喜。在下面的代码中,我们打开hello的值,因为它等于一些垃圾,所以执行将进入block中。为什么?因为case 'some garbage'的值是true。所以你猜怎么着:既然你在案例之间没有间断,每个案例都会被评估,你会看到4个警报。

var moveOneType = "rock";
var moveTwoType = "paper"; 
var moveThreeType = "scissors";

var hello = 'some garbage';

switch(hello)
{
    case 'some garbage': alert('garbage');
    case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
        alert('case 1');
    case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
        alert('case 2');
    case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
        alert('case 3');
    break;
}

<== Fiddle Me ==>

< = =小提琴我= = >

If you do not want all the cases to be evaluated then add a break after the case.

如果您不希望所有的案例都被评估,那么在案例之后添加一个break。

does switch accept multiple expression

交换机接受多重表达式吗

Sure it does as shown below. The expression moveOneType === "rock" && moveTwoType === "paper" will be evaluated and the value will be true and if at least one case is also true then execution will go inside the block:

当然,如下所示。表达式moveOneType === "rock" & moveTwoType === "paper"将被评估,该值将为true,如果至少有一个case为true,那么执行将进入block中:

var moveOneType = "rock";
var moveTwoType = "paper"; 
var moveThreeType = "scissors";

switch(moveOneType === "rock" && moveTwoType === "paper")
{
    case moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors":
        alert('case 1');
    case moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors":
        alert('case 2');
    case moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors":
        alert('case 3');
        break;
}

<== Fiddle Me ==>

< = =小提琴我= = >

#2


3  

Your code is fine, although it is probably not the most common use of switch.

您的代码很好,尽管它可能不是最常见的switch用法。

The reason why it works is that the expression in the case statement is evaluated and the result is used, so a case statement with an expression evaluating to true would match in switch(true).

它起作用的原因是对case语句中的表达式进行了计算并使用了结果,所以表达式为true的case语句在switch中会匹配(true)。

Here is a simple example

这里有一个简单的例子

function getValue() {
  console.log('get value');
  return 2;
}


switch (true) {
case getValue() === 1 || getValue() === 2:
  console.log('ok');
  break;
case false:
  console.log('not ok');
  break;
default:
  break;
}

You can see that it outputs get value twice, one for the first getValue() and one for the second (i.e. after the ||). The result evaluates to true, as getValue() === 2 returns 2. As we are using switch(true), as long as the expression in the case statement returns true, the case statement will be executed, so in this example, ok will be outputted.

您可以看到,它输出get值两次,第一次getValue()一次,第二次(即||之后)。结果计算为true, getValue() === 2返回2。当我们使用switch(true)时,只要case语句中的表达式返回true,则case语句将被执行,因此在本例中,ok将被输出。

Now, I am not exactly sure what your switch statement is supposed to do in the first place, so I cannot tell you if this is a good way to handle your issue or not, but it is by no mean incorrect to use an expression returning a boolean and match it with true.

现在,我不确定你的switch语句是什么应该做的,所以我不能告诉你这是一个很好的方式来处理你的问题,但这并不意味着不正确使用一个表达式返回一个布尔匹配和真实的。

#3


2  

You have to use if/else instead. Your switch statement can be an expression. But the case conditions are the value you are the possible value you are expecting from the output of your switch statement. Here is the Documentation

你必须用if/else代替。您的switch语句可以是一个表达式。但情况是你的值是你期望从switch语句输出的值。这是文档

    var moveOneType;

    if(moveOneType === "rock" || moveOneType === "paper" || moveOneType === "scissors"){}
    else if(moveTwoType === "rock" || moveTwoType === "paper" || moveTwoType === "scissors"){}
    else if(moveThreeType === "rock" || moveThreeType === "paper" || moveThreeType === "scissors"){}