Translating 1-js/05-data-types/09-destructuring-assignment#82
Merged
leviding merged 5 commits intojavascript-tutorial:zh-hansfrom May 27, 2018
kezhenxu94:1-js/05-data-types/09-destructuring-assignment
Merged
Translating 1-js/05-data-types/09-destructuring-assignment#82leviding merged 5 commits intojavascript-tutorial:zh-hansfrom kezhenxu94:1-js/05-data-types/09-destructuring-assignment
leviding merged 5 commits intojavascript-tutorial:zh-hansfrom
kezhenxu94:1-js/05-data-types/09-destructuring-assignment
Conversation
Contributor
|
@leviding 校对认领 |
Member
|
@lihanxiang ok |
Contributor
|
@leviding 校对认领 |
sunhaokk
reviewed
May 25, 2018
| ``` | ||
|
|
||
| If we want a "default" value to replace the missing one, we can provide it using `=`: | ||
| 如果我们想要提供一个“默认值”给缺失的变量,我们可以使用 `=` 来提供: |
| ``` | ||
|
|
||
| Default values can be more complex expressions or even function calls. They are evaluated only if the value is not provided. | ||
| 默认值可以是更加复杂的表达式甚至可以是函数调用,这些表达式或函数只会在这个值缺失的时候才会被计算。 |
| ``` | ||
|
|
||
| Properties `options.title`, `options.width` and `options.height` are assigned to the corresponding variables. The order does not matter. This works too: | ||
| 属性 `options.title`、`options.width` 和 `options.height` 的值被赋给了对应的变量。变量的顺序并不重要,以下的代码也奏效: |
| Just like with arrays or function parameters, default values can be any expressions or even function calls. They will be evaluated if the value is not provided. | ||
| 就像数组或函数参数一样,默认值可以是表达式甚至是函数调用。只会在这个值缺失的时候才会被计算/调用。 | ||
|
|
||
| The code below asks for width, but not the title. |
|
|
||
| *!* | ||
| let {width = prompt("width?"), title = prompt("title?")} = options; | ||
| let {width = prompt("宽度?"), title = prompt("标题?")} = options; |
Contributor
Author
There was a problem hiding this comment.
此处并非代码,而是在生成的网站上与用户交互的提示语,为了全站统一,这里也让用户看到中文
| ````smart header="Gotcha without `let`" | ||
| In the examples above variables were declared right before the assignment: `let {…} = {…}`. Of course, we could use existing variables too. But there's a catch. | ||
| ````smart header="不使用 `let` 的疑难病症" | ||
| 在以上的例子中,变量都是在等号前才被声明的:`let {…} = {…}`。当然,我们也可以使用已有的变量。但这里有一个问题需要注意。 |
| // ...and it immediately expands it to variables | ||
| // ...然后函数马上把对象展开成变量 | ||
| function showMenu(*!*{title = "Untitled", width = 200, height = 100, items = []}*/!*) { | ||
| // title, items – taken from options, |
lihanxiang
reviewed
May 26, 2018
| 对象让我们能够把许多信息打包到一个单一的实体中,数组则让我们能够存放有序的集合。所以我们可以创建一个对象或一个数组,把它当作一个实体进行处理,或者把它当作参数传递给一个函数调用。 | ||
|
|
||
| *Destructuring assignment* is a special syntax that allows us to "unpack" arrays or objects into a bunch of variables, as sometimes they are more convenient. Destructuring also works great with complex functions that have a lot of parameters, default values, and soon we'll see how these are handled too. | ||
| **解构赋值**是一种特殊的语法,它让我们可以将数组或对象进行“拆包”,存放到一系列的变量中,因为变量有时候更加方便。解构操作在那些具有很多参数和默认参数值的函数中也很奏效,很快我们就会看到这些都是怎么处理的。 |
Contributor
There was a problem hiding this comment.
“因为变量有时候更加方便” => “因为有时候使用变量更加方便”
|
|
||
| ```js | ||
| // we have an array with the name and surname | ||
| // 有一个存放了名字和姓氏数组 |
Contributor
There was a problem hiding this comment.
“有一个存放了名字和姓氏数组” => “有一个存放了名字和姓氏的数组”
| ### 默认值 | ||
|
|
||
| If there are fewer values in the array than variables in the assignment, there will be no error. Absent values are considered undefined: | ||
| 如果赋值语句中变量的数量多于数组中实际元素的数量,赋值不会报错。缺失的被当作 `undifined`: |
Member
|
@kezhenxu94 可以修改啦 |
Contributor
Author
|
@leviding 修改完毕 |
leviding
approved these changes
May 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.
@leviding 翻译完成,resolve #65