You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please note: the `break` at the bottom is not required. But we put it to make the code future-proof.
24
+
请注意:最后的 `break` 不是必须的。但是为了让代码可扩展我们要把它加上。
25
+
26
+
有可能之后我们想要再添加一个 `case`,例如 `case 4`。如果我们忘记在它之前添加一个 break,那么在 case 3 结束时会出现错误。所以这是一种保险。
25
27
26
-
In the future, there is a chance that we'd want to add one more `case`, for example `case 4`. And if we forget to add a break before it, at the end of `case 3`, there will be an error. So that's a kind of self-insurance.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/13-switch/article.md
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
-
# The "switch" statement
1
+
# "switch" 语句
2
2
3
-
A `switch`statement can replace multiple `if`checks.
3
+
`switch`语句可以被多个 `if`语句替代。
4
4
5
-
It gives a more descriptive way to compare a value with multiple variants.
5
+
`switch` 语句为多分支选择的情况提供了一个更具描述性的方式。
6
6
7
-
## The syntax
7
+
## 语法
8
8
9
-
The `switch`has one or more `case`blocks and an optional default.
9
+
`switch`语句有至少一个 `case`代码块和一个可选的 `default` 代码块。
10
10
11
-
It looks like this:
11
+
就像这样:
12
12
13
13
```js no-beautify
14
14
switch(x) {
@@ -26,13 +26,13 @@ switch(x) {
26
26
}
27
27
```
28
28
29
-
-The value of `x`is checked for a strict equality to the value from the first `case` (that is, `value1`) then to the second (`value2`) and so on.
30
-
-If the equality is found, `switch`starts to execute the code starting from the corresponding `case`, until the nearest `break`(or until the end of `switch`).
31
-
-If no case is matched then the `default`code is executed (if it exists).
An example of `switch`(the executed code is highlighted):
35
+
`switch`例子(被执行的代码高亮):
36
36
37
37
```js run
38
38
let a =2+2;
@@ -54,13 +54,13 @@ switch (a) {
54
54
}
55
55
```
56
56
57
-
Here the `switch`starts to compare `a` from the first `case` variant that is `3`. The match fails.
57
+
这里的 `switch`从第一个 `case` 分支比较 `a` 的值,值为 `3` 匹配失败。
58
58
59
-
Then`4`. That's a match, so the execution starts from `case 4`until the nearest `break`.
59
+
然后比较`4`。匹配,所以从 `case 4`开始执行直到遇到最近的 `break`。
60
60
61
-
**If there is no `break` then the execution continues with the next `case` without any checks.**
61
+
**如果没有 `break`,不经过任何检查就会继续执行下一个 `case`**
62
62
63
-
An example without `break`:
63
+
无 `break` 的例子:
64
64
65
65
```js run
66
66
let a =2+2;
@@ -79,7 +79,7 @@ switch (a) {
79
79
}
80
80
```
81
81
82
-
In the example above we'll see sequential execution of three `alert`s:
82
+
在上面的例子中我们会看到连续执行的三个 `alert`:
83
83
84
84
```js
85
85
alert( 'Exactly!' );
@@ -88,9 +88,9 @@ alert( "I don't know such values" );
88
88
```
89
89
90
90
````smart header="Any expression can be a `switch/case` argument"
91
-
Both `switch`and`case`allow arbitrary expressions.
91
+
`switch`和`case`都允许任意表达式。
92
92
93
-
For example:
93
+
比如:
94
94
95
95
```js run
96
96
let a ="1";
@@ -110,11 +110,11 @@ switch (+a) {
110
110
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
111
111
````
112
112
113
-
## Grouping of "case"
113
+
## "case" 分组
114
114
115
-
Several variants of `case` which share the same code can be grouped.
115
+
共享同一段代码的几个 `case` 分支会被分在一组:
116
116
117
-
For example, if we want the same code to run for `case 3` and `case 5`:
117
+
比如,如果我们想让 `case 3` 和 `case 5` 执行同样的代码:
118
118
119
119
```js run no-beautify
120
120
let a = 2 + 2;
@@ -125,7 +125,7 @@ switch (a) {
125
125
break;
126
126
127
127
*!*
128
-
case 3: // (*) grouped two cases
128
+
case 3: // (*) 下面这两个 case 被分在一组
129
129
case 5:
130
130
alert('Wrong!');
131
131
alert("Why don't you take a math class?");
@@ -137,15 +137,15 @@ switch (a) {
137
137
}
138
138
```
139
139
140
-
Now both `3` and `5` show the same message.
140
+
现在 `3` 和 `5` 都显示相同的信息。
141
141
142
-
The ability to "group" cases is a side-effect of how `switch/case` works without `break`. Here the execution of `case 3` starts from the line `(*)` and goes through `case 5`, because there's no `break`.
Let's emphasize that the equality check is always strict. The values must be of the same type to match.
146
+
强调一下,这里的相等是严格相等。被比较的值必须是相同类型的才能匹配。
147
147
148
-
For example, let's consider the code:
148
+
比如,我们来看下面的代码:
149
149
150
150
```js run
151
151
let arg = prompt("Enter a value?")
@@ -167,6 +167,6 @@ switch (arg) {
167
167
}
168
168
```
169
169
170
-
1. For `0`, `1`, the first `alert` runs.
171
-
2. For `2` the second `alert` runs.
172
-
3. But for `3`, the result of the `prompt` is a string `"3"`, which is not strictly equal `===` to the number `3`. So we've got a dead code in `case 3`! The `default` variant will execute.
0 commit comments