1-js/02-first-steps/13-switch#43
Merged
leviding merged 31 commits intojavascript-tutorial:zh-hansfrom Apr 23, 2018
HydeSong:translation/13-switch
Merged
1-js/02-first-steps/13-switch#43leviding merged 31 commits intojavascript-tutorial:zh-hansfrom HydeSong:translation/13-switch
leviding merged 31 commits intojavascript-tutorial:zh-hansfrom
HydeSong:translation/13-switch
Conversation
Contributor
Author
|
翻译完成 resolve #13 |
|
校对认领 |
Member
PengyuanZhao
suggested changes
Apr 20, 2018
| @@ -1,8 +1,8 @@ | |||
| To precisely match the functionality of `switch`, the `if` must use a strict comparison `'==='`. | |||
| 为了精确实现 `switch` 的功能,`if` 必须使用严格比较 `'==='`。 | |||
| ``` | ||
|
|
||
| Please note: the `break` at the bottom is not required. But we put it to make the code future-proof. | ||
| 请注意:最后的 `break` 不是必须的。但是为了让代码支持新特性我们要把它加上。 |
| Please note: the `break` at the bottom is not required. But we put it to make the code future-proof. | ||
| 请注意:最后的 `break` 不是必须的。但是为了让代码支持新特性我们要把它加上。 | ||
|
|
||
| 将来,我们可能会再添加一个 `case`,例如 `case 4`。 如果我们忘记在它之前添加一个 break,那么在 case 3 结束时会出现错误。所以这是一种保险。 |
| - 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`). | ||
| - 比较 `x` 值与第一个 `case` (也就是 `value1`)是否严格相等,然后比较第二个 `case`(`value2`)以此类推。 | ||
| - 如果相等,`switch` 语句就执行相应 `case` 下的代码块,直到遇到最靠近的 `break` 语句(或者直到 `switch` 语句末尾)。 | ||
| - If no case is matched then the `default` code is executed (if it exists). |
| - 比较 `x` 值与第一个 `case` (也就是 `value1`)是否严格相等,然后比较第二个 `case`(`value2`)以此类推。 | ||
| - 如果相等,`switch` 语句就执行相应 `case` 下的代码块,直到遇到最靠近的 `break` 语句(或者直到 `switch` 语句末尾)。 | ||
| - If no case is matched then the `default` code is executed (if it exists). | ||
| - 如果没有符合的 case,`default` 代码块就会被执行(如果代码中有 `default`)。 |
| ``` | ||
|
|
||
| In the example above we'll see sequential execution of three `alert`s: | ||
| 在上面的例子我们会看到连续执行的三个 `alert`: |
| ## “case” 分组 | ||
|
|
||
| For example, if we want the same code to run for `case 3` and `case 5`: | ||
| 共享同一段代码的 `case` 分支会被分在一组: |
| 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. | ||
| 1. 在 prompt 对话框输入 `0`、`1`,第一个 `alert` 弹出。 | ||
| 2. 输入 `2`,第二个 `alert` 弹出。 | ||
| 3. 但是输入 `3`,因为 `prompt` 的结果是字符串类型的 `"3"`,不是严格相等于数字类型的 `3`,所以 `case 3` 不会执行!最后`default` 分支也会被执行。 |
There was a problem hiding this comment.
最后default 分支也会被执行 -> 最后的default 分支会执行
Contributor
Author
|
@PengyuanZhao @leviding 已根据校对者意见修改 |
Contributor
|
@leviding 校对认领 |
Starriers
reviewed
Apr 23, 2018
| 对于给定的字符串,一个简单的`'=='`也可以。 | ||
|
|
||
| ```js no-beautify | ||
| ```js |
| --- | ||
|
|
||
| # Rewrite the "switch" into an "if" | ||
| # 把 "switch" 重构为 "if" 结构 |
| Please note: the `break` at the bottom is not required. But we put it to make the code future-proof. | ||
| 请注意:最后的 `break` 不是必须的。但是为了让代码可扩展我们要把它加上。 | ||
|
|
||
| 有可能之后我们想要再添加一个 `case`,例如 `case 4`。 如果我们忘记在它之前添加一个 break,那么在 case 3 结束时会出现错误。所以这是一种保险。 |
| - 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`). | ||
| - 比较 `x` 值与第一个 `case` (也就是 `value1`)是否严格相等,然后比较第二个 `case`(`value2`)以此类推。 | ||
| - 如果相等,`switch` 语句就执行相应 `case` 下的代码块,直到遇到最靠近的 `break` 语句(或者直到 `switch` 语句末尾)。 | ||
| - If no case is matched then the `default` code is executed (if it exists). |
| 现在 `3` 和 `5` 都显示相同的信息。 | ||
|
|
||
| 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`. | ||
| “分组” case 的能力是 `switch / case` 在没有 `break` 的情况下的副作用。 因为没有 `break`,`case 3` 会从 `(*)` 行执行到 `case 5`。 |
Contributor
There was a problem hiding this comment.
两个英文单词不用翻译。 在没有break的情况下switchcase如何工作的(我这手机,就简写了
| ## 举个例子 | ||
|
|
||
| An example of `switch` (the executed code is highlighted): | ||
| `switch` 例子( *!* 和 */!* 之间的代码会被执行): |
|
|
||
| - 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. | ||
| - 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`). | ||
| - 比较 `x` 值与第一个 `case` (也就是 `value1`)是否严格相等,然后比较第二个 `case`(`value2`)以此类推。 |
| # Rewrite "if" into "switch" | ||
|
|
||
| Rewrite the code below using a single `switch` statement: | ||
| # 把 "if" 重构为 "switch" 结构 |
| Please note: the `break` at the bottom is not required. But we put it to make the code future-proof. | ||
| 请注意:最后的 `break` 不是必须的。但是为了让代码可扩展我们要把它加上。 | ||
|
|
||
| 有可能之后我们想要再添加一个 `case`,例如 `case 4`。 如果我们忘记在它之前添加一个 break,那么在 case 3 结束时会出现错误。所以这是一种保险。 |
Contributor
Author
|
@leviding @Starriers 已根据校对者意见修改 |
leviding
reviewed
Apr 23, 2018
| 为了精确实现 `switch` 的功能,`if` 必须使用严格相等 `'==='`。 | ||
|
|
||
| For given strings though, a simple `'=='` works too. | ||
| 对于给定的字符串,一个简单的`'=='`也可以。 |
| @@ -1,10 +1,10 @@ | |||
| importance: 5 | |||
| 重要:5 | |||
| @@ -1,11 +1,10 @@ | |||
| importance: 4 | |||
| 重要:4 | |||
| Rewrite the code below using a single `switch` statement: | ||
| # "if" 重构为 "switch" 结构 | ||
|
|
||
| 用 `switch` 重写以下代码: |
| @@ -1,16 +1,16 @@ | |||
| # The "switch" statement | |||
| # “switch” 语句 | |||
| For example, if we want the same code to run for `case 3` and `case 5`: | ||
| 共享同一段代码的几个 `case` 分支会被分在一组: | ||
|
|
||
| ```js run no-beautify |
| `+a` 被赋值为字符串类型的 `1`,和 `b + 1` 的 `case` 比较,相应的代码被执行。 | ||
|
|
||
| Several variants of `case` which share the same code can be grouped. | ||
| ## “case” 分组 |
| alert( "I don't know such values" ); | ||
| ``` | ||
|
|
||
| ````smart header="Any expression can be a `switch/case` argument" |
| For example: | ||
| `switch` 和 `case` 都允许任意表达式。 | ||
|
|
||
| ```js run |
| } | ||
| ``` | ||
| Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed. | ||
| ```` |
Contributor
Author
|
@leviding 已修改 |
leviding
approved these changes
Apr 23, 2018
Member
|
@HydeSong 看看我最新的七个 commit,这些问题要注意,不要修改原文的那些 flag |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
02-first-steps的13-switch目录下的文件翻译完成