Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translating 1-js/05-data-types/09-destructuring-assignment #82

Merged
merged 5 commits into from May 27, 2018
Merged

Translating 1-js/05-data-types/09-destructuring-assignment #82

merged 5 commits into from May 27, 2018

Conversation

kezhenxu94
Copy link
Contributor

@kezhenxu94 kezhenxu94 commented May 6, 2018

@leviding 翻译完成,resolve #65

@lihanxiang
Copy link
Contributor

@leviding 校对认领

@leviding
Copy link
Member

@lihanxiang ok

@sunhaokk
Copy link
Contributor

@leviding 校对认领

Copy link
Contributor

@sunhaokk sunhaokk left a comment

Choose a reason for hiding this comment

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

还行

@@ -148,45 +148,45 @@ let [firstName, surname] = [];
alert(firstName); // undefined
```

If we want a "default" value to replace the missing one, we can provide it using `=`:
如果我们想要提供一个“默认值”给缺失的变量,我们可以使用 `=` 来提供:
Copy link
Contributor

Choose a reason for hiding this comment

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

缺失的变量 未赋值的变量

```

Default values can be more complex expressions or even function calls. They are evaluated only if the value is not provided.
默认值可以是更加复杂的表达式甚至可以是函数调用,这些表达式或函数只会在这个值缺失的时候才会被计算。
Copy link
Contributor

Choose a reason for hiding this comment

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

缺失 未赋值

@@ -204,16 +204,16 @@ alert(width); // 100
alert(height); // 200
```

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` 的值被赋给了对应的变量。变量的顺序并不重要,以下的代码也奏效:
Copy link
Contributor

Choose a reason for hiding this comment

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

,号

Copy link
Contributor Author

Choose a reason for hiding this comment

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

中文列举用顿号

@@ -254,24 +254,25 @@ alert(width); // 100
alert(height); // 200
```

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

Choose a reason for hiding this comment

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

没删掉


```js run
let options = {
title: "Menu"
};

*!*
let {width = prompt("width?"), title = prompt("title?")} = options;
let {width = prompt("宽度?"), title = prompt("标题?")} = options;
Copy link
Contributor

Choose a reason for hiding this comment

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

代码不建议翻译

Copy link
Contributor Author

Choose a reason for hiding this comment

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

此处并非代码,而是在生成的网站上与用户交互的提示语,为了全站统一,这里也让用户看到中文

````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 {…} = {…}`。当然,我们也可以使用已有的变量。但这里有一个问题需要注意。
Copy link
Contributor

Choose a reason for hiding this comment

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

等号 =》赋值

let options = {
title: "My menu",
items: ["Item1", "Item2"]
};

// ...and it immediately expands it to variables
// ...然后函数马上把对象展开成变量
function showMenu(*!*{title = "Untitled", width = 200, height = 100, items = []}*/!*) {
// title, items – taken from options,
Copy link
Contributor

Choose a reason for hiding this comment

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

注释建议翻译

Copy link
Contributor

@lihanxiang lihanxiang left a comment

Choose a reason for hiding this comment

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


*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.
**解构赋值**是一种特殊的语法,它让我们可以将数组或对象进行“拆包”,存放到一系列的变量中,因为变量有时候更加方便。解构操作在那些具有很多参数和默认参数值的函数中也很奏效,很快我们就会看到这些都是怎么处理的。
Copy link
Contributor

Choose a reason for hiding this comment

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

“因为变量有时候更加方便” => “因为有时候使用变量更加方便”


```js
// we have an array with the name and surname
// 有一个存放了名字和姓氏数组
Copy link
Contributor

Choose a reason for hiding this comment

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

“有一个存放了名字和姓氏数组” => “有一个存放了名字和姓氏的数组”


If there are fewer values in the array than variables in the assignment, there will be no error. Absent values are considered undefined:
如果赋值语句中变量的数量多于数组中实际元素的数量,赋值不会报错。缺失的被当作 `undifined`:
Copy link
Contributor

Choose a reason for hiding this comment

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

“缺失的被当作” => “缺失的变量被当作”

@leviding
Copy link
Member

@kezhenxu94 可以修改啦

@kezhenxu94
Copy link
Contributor Author

@leviding 修改完毕

@leviding leviding merged commit 6f1cbb8 into javascript-tutorial:zh-hans May 27, 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.

None yet

4 participants