Skip to content

Commit f1fb354

Browse files
HydeSongleviding
authored andcommitted
1-js/02-first-steps/13-switch (javascript-tutorial#43)
* 翻译完成 翻译完成 * 翻译完成 翻译完成 * 翻译完成 翻译完成 * 翻译完成 翻译完成 * 翻译完成 翻译完成 * Update solution.md * Update solution.md * Update solution.md * Update article.md * Update solution.md * Update solution.md * Update task.md * Update solution.md * Update task.md * Update article.md * Update solution.md * Update task.md * Update task.md * Update article.md * Update article.md * Update article.md * Update article.md * Update task.md * Update task.md * Update article.md * Update article.md * Update article.md * Update article.md * Update article.md * Update article.md * Update article.md
1 parent bb3665a commit f1fb354

File tree

5 files changed

+42
-41
lines changed

5 files changed

+42
-41
lines changed

1-js/02-first-steps/13-switch/1-rewrite-switch-if-else/solution.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
To precisely match the functionality of `switch`, the `if` must use a strict comparison `'==='`.
1+
为了精确实现 `switch` 的功能,`if` 必须使用严格相等 `'==='`
22

3-
For given strings though, a simple `'=='` works too.
3+
对于给定的字符串,一个简单的 `'=='` 也可以。
44

55
```js no-beautify
66
if(browser == 'Edge') {
@@ -15,6 +15,6 @@ if(browser == 'Edge') {
1515
}
1616
```
1717

18-
Please note: the construct `browser == 'Chrome' || browser == 'Firefox' …` is split into multiple lines for better readability.
18+
请注意:`browser == 'Chrome' || browser == 'Firefox' …` 结构分成多行更容易阅读。
1919

20-
But the `switch` construct is still cleaner and more descriptive.
20+
`switch` 结构更清晰明了。

1-js/02-first-steps/13-switch/1-rewrite-switch-if-else/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 5
22

33
---
44

5-
# Rewrite the "switch" into an "if"
5+
# "switch" 重构为 "if" 结构
66

7-
Write the code using `if..else` which would correspond to the following `switch`:
7+
对应以下 `switch`,使用 `if..else` 编写代码:
88

99
```js
1010
switch (browser) {

1-js/02-first-steps/13-switch/2-rewrite-if-switch/solution.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The first two checks turn into two `case`. The third check is split into two cases:
1+
前两个 `case` 分别执行各自的代码,第三个 `case` 会和下一个 `case` 一起执行:
22

33
```js run
44
let a = +prompt('a?', '');
@@ -21,6 +21,7 @@ switch (a) {
2121
}
2222
```
2323

24-
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 结束时会出现错误。所以这是一种保险。
2527

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.

1-js/02-first-steps/13-switch/2-rewrite-if-switch/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 4
22

33
---
44

5-
# Rewrite "if" into "switch"
5+
# "if" 重构为 "switch" 结构
66

7-
Rewrite the code below using a single `switch` statement:
7+
`switch` 重写以下代码:
88

99
```js run
1010
let a = +prompt('a?', '');

1-js/02-first-steps/13-switch/article.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# The "switch" statement
1+
# "switch" 语句
22

3-
A `switch` statement can replace multiple `if` checks.
3+
`switch` 语句可以被多个 `if` 语句替代。
44

5-
It gives a more descriptive way to compare a value with multiple variants.
5+
`switch` 语句为多分支选择的情况提供了一个更具描述性的方式。
66

7-
## The syntax
7+
## 语法
88

9-
The `switch` has one or more `case` blocks and an optional default.
9+
`switch` 语句有至少一个 `case` 代码块和一个可选的 `default` 代码块。
1010

11-
It looks like this:
11+
就像这样:
1212

1313
```js no-beautify
1414
switch(x) {
@@ -26,13 +26,13 @@ switch(x) {
2626
}
2727
```
2828

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).
29+
- 比较 `x` 值与第一个 `case`(也就是 `value1`)是否严格相等,然后比较第二个 `case``value2`)以此类推。
30+
- 如果相等,`switch` 语句就执行相应 `case` 下的代码块,直到遇到最靠近的 `break` 语句(或者直到 `switch` 语句末尾)。
31+
- 如果没有符合的 case`default` 代码块就会被执行(如果 `default` 存在)。
3232

33-
## An example
33+
## 举个例子
3434

35-
An example of `switch` (the executed code is highlighted):
35+
`switch` 例子(被执行的代码高亮):
3636

3737
```js run
3838
let a = 2 + 2;
@@ -54,13 +54,13 @@ switch (a) {
5454
}
5555
```
5656

57-
Here the `switch` starts to compare `a` from the first `case` variant that is `3`. The match fails.
57+
这里的 `switch` 从第一个 `case` 分支比较 `a` 的值,值为 `3` 匹配失败。
5858

59-
Then `4`. That's a match, so the execution starts from `case 4` until the nearest `break`.
59+
然后比较 `4`。匹配,所以从 `case 4` 开始执行直到遇到最近的 `break`
6060

61-
**If there is no `break` then the execution continues with the next `case` without any checks.**
61+
**如果没有 `break`,不经过任何检查就会继续执行下一个 `case`**
6262

63-
An example without `break`:
63+
`break` 的例子:
6464

6565
```js run
6666
let a = 2 + 2;
@@ -79,7 +79,7 @@ switch (a) {
7979
}
8080
```
8181

82-
In the example above we'll see sequential execution of three `alert`s:
82+
在上面的例子中我们会看到连续执行的三个 `alert`
8383

8484
```js
8585
alert( 'Exactly!' );
@@ -88,9 +88,9 @@ alert( "I don't know such values" );
8888
```
8989

9090
````smart header="Any expression can be a `switch/case` argument"
91-
Both `switch` and `case` allow arbitrary expressions.
91+
`switch` `case` 都允许任意表达式。
9292

93-
For example:
93+
比如:
9494

9595
```js run
9696
let a = "1";
@@ -110,11 +110,11 @@ switch (+a) {
110110
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
111111
````
112112
113-
## Grouping of "case"
113+
## "case" 分组
114114
115-
Several variants of `case` which share the same code can be grouped.
115+
共享同一段代码的几个 `case` 分支会被分在一组:
116116
117-
For example, if we want the same code to run for `case 3` and `case 5`:
117+
比如,如果我们想让 `case 3` `case 5` 执行同样的代码:
118118
119119
```js run no-beautify
120120
let a = 2 + 2;
@@ -125,7 +125,7 @@ switch (a) {
125125
break;
126126
127127
*!*
128-
case 3: // (*) grouped two cases
128+
case 3: // (*) 下面这两个 case 被分在一组
129129
case 5:
130130
alert('Wrong!');
131131
alert("Why don't you take a math class?");
@@ -137,15 +137,15 @@ switch (a) {
137137
}
138138
```
139139
140-
Now both `3` and `5` show the same message.
140+
现在 `3` `5` 都显示相同的信息。
141141
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`.
142+
在没有 `break` 的情况下,`switch/case` 会“分组” case。 因为没有 `break``case 3` 会从 `(*)` 行执行到 `case 5`
143143
144-
## Type matters
144+
## 值类型
145145
146-
Let's emphasize that the equality check is always strict. The values must be of the same type to match.
146+
强调一下,这里的相等是严格相等。被比较的值必须是相同类型的才能匹配。
147147
148-
For example, let's consider the code:
148+
比如,我们来看下面的代码:
149149
150150
```js run
151151
let arg = prompt("Enter a value?")
@@ -167,6 +167,6 @@ switch (arg) {
167167
}
168168
```
169169
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.
170+
1. 在 prompt 对话框输入 `0``1`,第一个 `alert` 弹出。
171+
2. 输入 `2`,第二个 `alert` 弹出。
172+
3. 但是输入 `3`,因为 `prompt` 的结果是字符串类型的 `"3"`,不是严格相等于数字类型的 `3`,所以 `case 3` 不会执行!最后`default` 分支会执行。

0 commit comments

Comments
 (0)