Skip to content

1-js/02-first-steps/13-switch#43

Merged
leviding merged 31 commits intojavascript-tutorial:zh-hansfrom
HydeSong:translation/13-switch
Apr 23, 2018
Merged

1-js/02-first-steps/13-switch#43
leviding merged 31 commits intojavascript-tutorial:zh-hansfrom
HydeSong:translation/13-switch

Conversation

@HydeSong
Copy link
Copy Markdown
Contributor

02-first-steps的13-switch目录下的文件翻译完成

翻译完成
翻译完成
翻译完成
翻译完成
翻译完成
@HydeSong HydeSong changed the title 翻译完成 1-js/02-first-steps/13-switch Apr 17, 2018
@HydeSong
Copy link
Copy Markdown
Contributor Author

翻译完成 resolve #13

@PengyuanZhao
Copy link
Copy Markdown

校对认领

@leviding
Copy link
Copy Markdown
Member

@PengyuanZhao ok

@@ -1,8 +1,8 @@
To precisely match the functionality of `switch`, the `if` must use a strict comparison `'==='`.
为了精确实现 `switch` 的功能,`if` 必须使用严格比较 `'==='`
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

严格比较 -> 严格相等

```

Please note: the `break` at the bottom is not required. But we put it to make the code future-proof.
请注意:最后的 `break` 不是必须的。但是为了让代码支持新特性我们要把它加上。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

支持新特性 -> 可扩展

根据上下文,我觉得这样翻译更准确。

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

将来,我们可能会 -> 有可能之后我们想要

- 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).
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句原文删掉?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该是没有翻译 要翻译的

- 比较 `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`)。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果代码中有 default -> 如果 default 存在

```

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在上面的例子 -> 在上面的例子中

## “case” 分组

For example, if we want the same code to run for `case 3` and `case 5`:
共享同一段代码的 `case` 分支会被分在一组:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case 分支 -> 几个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` 分支也会被执行。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最后default 分支也会被执行 -> 最后的default 分支会执行

@PengyuanZhao
Copy link
Copy Markdown

@HydeSong @leviding 校对完成

@HydeSong
Copy link
Copy Markdown
Contributor Author

@PengyuanZhao @leviding 已根据校对者意见修改

@Starriers
Copy link
Copy Markdown
Contributor

@leviding 校对认领

Copy link
Copy Markdown
Contributor

@Starriers Starriers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HydeSong @leviding 校对完成

对于给定的字符串,一个简单的`'=='`也可以。

```js no-beautify
```js
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

并不优雅

---

# Rewrite the "switch" into an "if"
# "switch" 重构为 "if" 结构
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把 去掉感觉更好

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果前面多了空格

- 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).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该是没有翻译 要翻译的

现在 `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`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两个英文单词不用翻译。 在没有break的情况下switchcase如何工作的(我这手机,就简写了

## 举个例子

An example of `switch` (the executed code is highlighted):
`switch` 例子( *!* 和 */!* 之间的代码会被执行):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的翻译翻到不是。执行代码高亮


- 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`)以此类推。
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第一个case够多了空格

# Rewrite "if" into "switch"

Rewrite the code below using a single `switch` statement:
# 把 "if" 重构为 "switch" 结构
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把去掉更好

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果前多了空格

@HydeSong
Copy link
Copy Markdown
Contributor Author

@leviding @Starriers 已根据校对者意见修改

Copy link
Copy Markdown
Member

@leviding leviding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有问题

为了精确实现 `switch` 的功能,`if` 必须使用严格相等 `'==='`

For given strings though, a simple `'=='` works too.
对于给定的字符串,一个简单的`'=='`也可以。
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺两个空格

@@ -1,10 +1,10 @@
importance: 5
重要:5
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重要程度

@@ -1,11 +1,10 @@
importance: 4
重要:4
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重要程度

Rewrite the code below using a single `switch` statement:
# "if" 重构为 "switch" 结构

用 `switch` 重写以下代码:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

和下一行之间空一行

@@ -1,16 +1,16 @@
# The "switch" statement
# switch” 语句
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# "switch" 语句

引号换成英文的吧

For example, if we want the same code to run for `case 3` and `case 5`:
共享同一段代码的几个 `case` 分支会被分在一组:

```js run no-beautify
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一行不要改

`+a` 被赋值为字符串类型的 `1`,和 `b + 1` 的 `case` 比较,相应的代码被执行。

Several variants of `case` which share the same code can be grouped.
## “case” 分组
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“case” => "case"

alert( "I don't know such values" );
```

````smart header="Any expression can be a `switch/case` argument"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要改,这是原文固定的解析格式

For example:
`switch` 和 `case` 都允许任意表达式。

```js run
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要改这里

}
```
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
````
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要改这里,这是原文规定的解析格式

@HydeSong
Copy link
Copy Markdown
Contributor Author

@leviding 已修改

@leviding
Copy link
Copy Markdown
Member

@HydeSong 看看我最新的七个 commit,这些问题要注意,不要修改原文的那些 flag

@leviding leviding merged commit f1fb354 into javascript-tutorial:zh-hans Apr 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants