1-02-11-逻辑运算符#55
Merged
leviding merged 41 commits intojavascript-tutorial:zh-hansfrom Apr 27, 2018
EmilyQiRabbit:translation/1-02-11
Merged
1-02-11-逻辑运算符#55leviding merged 41 commits intojavascript-tutorial:zh-hansfrom EmilyQiRabbit:translation/1-02-11
leviding merged 41 commits intojavascript-tutorial:zh-hansfrom
EmilyQiRabbit:translation/1-02-11
Conversation
Contributor
Author
|
@leviding 翻译完成 |
|
@leviding 校对认领 |
Member
|
@wavezhang ok |
wavezhang
reviewed
Apr 23, 2018
| @@ -1,13 +1,13 @@ | |||
| The answer: first `1`, then `2`. | |||
| 答案:第一个是 `1`,然后是 `2`。 | |||
There was a problem hiding this comment.
这里 first... then... 翻译成 首先……然后…… 是不是更合理一点?
| 1. The first OR `||` evaluates it's left operand `alert(1)`. That shows the first message with `1`. | ||
| 2. The `alert` returns `undefined`, so OR goes on to the second operand searching for a truthy value. | ||
| 3. The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert. | ||
| 1. 第一个或运算 `||` 对它左边的 `alert(1)` 进行了计算。这就显示了第一条 `1` 的信息。 |
| 2. The `alert` returns `undefined`, so OR goes on to the second operand searching for a truthy value. | ||
| 3. The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert. | ||
| 1. 第一个或运算 `||` 对它左边的 `alert(1)` 进行了计算。这就显示了第一条 `1` 的信息。 | ||
| 2. 函数 `alert` 返回了 `undefined`,所以或运算继续检查第二个参数,寻找真值。 |
| --- | ||
|
|
||
| # What is the result of AND'ed alerts? | ||
| # 与运算和 alerts 的结果是什么? |
| 在传统的编程中,逻辑或仅能够操作布尔值。如果参与运算的一个参数为 `true`,结果将返回 `true`,否则返回 `false`。 | ||
|
|
||
| In JavaScript the operator is a little bit more tricky and powerful. But first let's see what happens with boolean values. | ||
| 在 JavaScript 中,逻辑运算符更加灵活强大。但是首先我们来看一下参与运算的布尔值发生了什么。 |
| 返回的值是操作数的初始形式,不会做布尔转换。 | ||
|
|
||
| In other words, a chain of OR `"||"` returns the first truthy value or the last one if no such value is found. | ||
| 也就是,一个或 `"||"` 运算的链将返回第一个真(布尔转化后为 true 的,译者注)值,如果这样的值不存在就返回该链的最后一个值。 |
|
|
||
| If both `currentUser` and `defaultUser` were falsy then `"unnamed"` would be the result. | ||
| 2. **Short-circuit evaluation.** | ||
| 如果 `currentUser` 和 `defaultUser` 都是无效值,那么结果就是 `"unnamed"`。 |
| 操作数不仅仅可能是值,还可能是任意表达式。或运算会从左到右计算并测试每个操作数。当找到第一个真值,计算就会停止,并返回这个值。这个过程就叫做“短路取值”,因为它将从左到右的计算尽可能的少。 | ||
|
|
||
| This is clearly seen when the expression given as the second argument has a side effect. Like a variable assignment. | ||
| 当表达式作为第二个参数并且有一定副作用 - 比如变量赋值 - 的时候,这就清楚可见了。 |
| ## &&(与) | ||
|
|
||
| The AND operator is represented with two ampersands `&&`: | ||
| 两个 & 符号表示 `&&` 与操作: |
| - 如果所有的操作数都被计算过(也就是,转换结果都是 `true`),返回最后一个操作数。 | ||
|
|
||
| In other words, AND returns the first falsy value or the last value if none were found. | ||
| 换句话说,与操作符返回第一个假值,如果没有假值就返回最后一个值。(假值:将该变量进行布尔转换后为 `false`。译者注) |
|
@EmilyQiRabbit @leviding 校对完成 |
leviding
reviewed
Apr 23, 2018
| @@ -1,10 +1,10 @@ | |||
| importance: 3 | |||
| 重要性:3 | |||
| @@ -1,10 +1,10 @@ | |||
| importance: 5 | |||
| 重要性:5 | |||
| @@ -1,10 +1,10 @@ | |||
| importance: 5 | |||
| 重要性:5 | |||
| 调用 `alert` 返回了 `undefined`(它只展示消息,所以没有有意义的返回值)。 | ||
|
|
||
| Because of that, `&&` evaluates the left operand (outputs `1`), and immediately stops, because `undefined` is a falsy value. And `&&` looks for a falsy value and returns it, so it's done. | ||
| 因此,`&&` 计算了它左边的操作数(显示 `1`),然后立即停止了,因为 `undefined` 是一个假值。并且 `&&` 就是寻找假值然后返回它,所以运算结束。 |
| @@ -1,10 +1,10 @@ | |||
| importance: 3 | |||
| 重要性:3 | |||
| @@ -1,9 +1,9 @@ | |||
| importance: 3 | |||
| 重要性:3 | |||
| // Doesn't run | ||
| // -1 && 0 = 0, falsy | ||
| // 不执行。 | ||
| // -1 && 0 = 0, 假值 |
| @@ -1,12 +1,12 @@ | |||
| importance: 5 | |||
| 重要性:5 | |||
| --- | ||
|
|
||
| # A question about "if" | ||
| # 一个关于“if”的问题 |
| - Evaluate operands from left to right. | ||
| - For each operand, convert it to a boolean. If the result is `false`, stop and return the original value of that operand. | ||
| - If all other operands have been assessed (i.e. all were truthy), return the last operand. | ||
| =- 从左到右依次计算操作数。 |
Contributor
Author
|
@leviding 校对完成 |
leviding
approved these changes
Apr 23, 2018
leviding
approved these changes
Apr 23, 2018
leviding
approved these changes
Apr 27, 2018
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.
翻译完成 #11