Switch

Javascript Switch (Javascript切换)

The switch statement is used to represent various actions based on different conditions. It can replace multiple if checks and gives a more descriptive way for comparing a value with multiple variant. (Switch语句用于表示基于不同条件的各种动作。它可以替换多个if检查,并提供了一种更具描述性的方法来比较具有多个变量的值。)

To perform a multiway branch, you can use multiple else…if statements, as in the previous chapter, but it is not the best way. It is better to use a switch statement which is more efficiently than repeated else if statements. (要执行多路分支,您可以使用多个else… if语句,如前一章所述,但这不是最佳方式。最好使用switch语句,它比重复else if语句更有效。)

Syntax

Syntax (语法)

The main objective of a switch is to give an expression for evaluating and many various statements for executing. Both objectives are based on the value of the expression. The interpreter is checking each case against the value of the expression until it founds a match. If nothing matches, there will be used a default condition. (交换机的主要目的是给出一个用于评估的表达式和许多用于执行的各种语句。这两个目标是基于表达式的值。解释器根据表达式的值检查每种情况,直到找到匹配项。如果没有匹配,将使用默认条件。)

The switch statement has one or more case blocks and an optional default, which looks like this:

switch (x) {
 case 'value1': // if (x === 'value1')
   ...[
     break
(休息)
   ]
 case 'value2': // if (x === 'value2')
   ...[
     break
(休息)
   ]
 default:
   ...[
     break
(休息)
   ]
}
  • The value of x is checked for an equality of a strict to the value from the first case, it is value2, then to the second value2 and so on. (-检查x的值是否与第一种情况下的值严格相等,它是value2 ,然后是第二个value2 ,依此类推。)

  • If the equality is found, switch executes the code starting from the analogous case, until the end of switch. (-如果发现相等,交换机将从类似情况开始执行代码,直到交换机结束。)

  • If nothing matched, the default code will be used. (-如果没有匹配,将使用默认代码。)

Example of the switch:

let a = 2 * 3;
switch (a) {
 case 3:
   console.log('Too small');
   break;
 case 6:
   console.log('Exactly!');
   break;
 case 8:
   console.log('Too large');
   break;
 default:
   console.log("I don't know such values");
}

The switch compares a from the 3 which is the first case variant, the match fails. (交换机比较来自3的,这是第一个案例变体,匹配失败。)

Then 6 that is a match, it means that the execution starts from case 6 until the nearest break. (那么6是一个匹配,这意味着执行从案例6开始,直到最近的中断。)

If there is no break the execution will continue with the next case without any checks. (如果没有中断,执行将继续下一个案例,而无需任何检查。)

Example of the switch without break:

let a = 2 * 3;
switch (a) {
 case 3:
   console.log('Too small');
 case 6:
   console.log('Exactly!');
 case 8:
   console.log('Too big');
 default:
   console.log("I don't know such values");
}

There are sequential execution of three console.logs in the example above:

console.log('Exactly!');
console.log('Too big');
console.log("I don't know such values");

When JavaScript meets a break keyword, it breaks out of the switch block. That stops the execution of inside the block. (当JavaScript遇到break关键字时,它会突破switch块。这阻止了块内部的执行。)

It is not important to break the last case in a switch block, the block breaks there anyway. (打破开关块中的最后一个外壳并不重要,该块无论如何都会打破。)

If you forget a break the program continues execution at the next statement in the switch statement, even if the evaluation does not match the case. (>如果忘记中断,程序将在switch语句中的下一个语句继续执行,即使计算结果与大小写不匹配。)

The default keyword defines the code to run if there is no case match. (Default关键字定义了在没有大小写匹配时要运行的代码。)

Any expression can be a switch/case argument. Both switch and case allow random expressions. (>任何表达式都可以是switch/case参数。switch和case都允许随机表达式。)

let a = "4";
let b = 2;
switch (+a) {
 case b * 2:
   console.log("this runs, because +a is 4, exactly equals b*2");
   break;
 default:
   console.log("this doesn't run");
}

The examples shows that +a gives 4, that’s compared with b * 2 in case, and the corresponding code is implemented. (示例显示+ a给出4 ,在情况下与b * 2进行比较,并实现相应的代码。)

Grouping of “case”

Grouping of “case” (“病例”的分组)

If several variants of case share the same code they can be grouped. So if we want the same code to run for case 2 and case 3 or case 5, case 6 and case 8 we do this:

let val;
let answer = "";
switch (val) {
 case 1:
   answer = "Low";
   break;
 case 2: // (**) 
 case 5:
   answer = "Mid";
   break;
 case 3:
 case 6:
 case 8:
   answer = "High";
   break;
 default:
   answer = "Massive?";
}

At the result both 2 and 5 cases and 3, 6 , 8 cases show the same message. (在结果中, 2和5个案例以及3、6、8个案例都显示相同的信息。)

The ability to “group” cases is an aftereffect of how switch/case works without break. The execution of case 2 starts from the line (**), then it goes through case 5, because there’s no break. (“分组”个案的能力是切换/个案工作方式不间断的后遗症。案例2的执行从行(* *)开始,然后通过案例5 ,因为没有中断。)

Type matters

Type matters (类型很重要)

Let’s focus attention on the equality check that is always strict. The values here must be of the same type to match. (让我们把注意力集中在始终严格的平等检查上。此处的值必须具有相同的类型才能匹配。)

Example of the type matters:

let arg = prompt("Guess the car?");
switch (arg) {
 case 'bmw':
   console.log('BMW');
   break;
 case 'mercedes':
   console.log('Mercedes');
   break;
 default:
   console.log('An unknown value');
}

As you see the first alert runs for bmw, the second alert runs for mercedes. (如您所见,第一个警报针对宝马,第二个警报针对奔驰。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部