对象的键、值、项#140
Conversation
|
@leviding 翻译完成,不好意思,拖了这么久 |
|
@allenlongbaobao get |
|
校对认领 @leviding |
|
@trexguo ok |
| # 属性求和 Sum the properties | ||
|
|
||
| There is a `salaries` object with arbitrary number of salaries. | ||
| 有一个 `salaries` 对象,包含了薪水的值。 |
There was a problem hiding this comment.
『有一个 salaries 对象,包含了薪水的值。』 => 『有一个 salaries 对象,包含了任意数量的薪水。』
| 有一个 `salaries` 对象,包含了薪水的值。 | ||
|
|
||
| Write the function `sumSalaries(salaries)` that returns the sum of all salaries using `Object.values` and the `for..of` loop. | ||
| 写一个 `sumSalaries(salaries)` 函数,使用 `Object.values` 和 `for..of` 循环语句返回所有薪水的和。 |
There was a problem hiding this comment.
『写一个 sumSalaries(salaries) 函数,使用 Object.values 和 for..of 循环语句返回所有薪水的和。』 => 『使用 Object.values 和 for..of 循环语句写一个可以返回所有薪水的和的函数sumSalaries(salaries)』
| # 计算属性数量 | ||
|
|
||
| Write a function `count(obj)` that returns the number of properties in the object: | ||
| 写一个 `count(obj)` 函数返回一个对象的属性数量: |
There was a problem hiding this comment.
『写一个 count(obj) 函数返回一个对象的属性数量:』 => 『写一个可以返回对象的属性数量的函数 count(obj) :』
| - `Array` (除了 `arr.values()`) | ||
|
|
||
| Plain objects also support similar methods, but the syntax is a bit different. | ||
| 纯对象也支持这些方法,但是语法上有一些不同 |
There was a problem hiding this comment.
『纯对象也支持这些方法,但是语法上有一些不同』 => 『纯对象也支持类似的方法,但是语法有一些不同。』
注意标点和形容词similar
| - [Object.keys(obj)](mdn:js/Object/keys) -- returns an array of keys. | ||
| - [Object.values(obj)](mdn:js/Object/values) -- returns an array of values. | ||
| - [Object.entries(obj)](mdn:js/Object/entries) -- returns an array of `[key, value]` pairs. | ||
| - [Object.keys(obj)](mdn:js/Object/keys) —— 返回包含所有键的数组。 |
There was a problem hiding this comment.
『返回包含所有键的数组。』 => 『返回一个包含该对象全部的键的数组。』
“返回包含所有键的数组”这句话中的“所有”一词容易引发歧义。一个解释是“归XXX所拥有”,一个解释是“全部的”。
为避免歧义,建议改为“全部”
| 为什么会这样?主要原因是弹性化。请记住,在 JavaScript 中对象是所有复杂数据结构的基础。因此我们可能自己创建一个对象,比如 `order`,然后实现它自己的方法 `order.values()`。那么,我们依然可以对它调用 `Object.values(order)` 方法。 | ||
|
|
||
| The second difference is that `Object.*` methods return "real" array objects, not just an iterable. That's mainly for historical reasons. | ||
| 第二个区别是 `Object.*` 方法返回的是「真正的」数组对象,而不是可迭代项。这主要是历史原因。 |
There was a problem hiding this comment.
『第二个区别是 Object.* 方法返回的是「真正的」数组对象,而不是可迭代项。这主要是历史原因。』 => 『第二个区别是 Object.* 方法返回的是「真正的」数组对象,而不仅仅是一个可迭代项。这主要由于是历史原因。』
| - `Object.entries(user) = [ ["name","John"], ["age",30] ]` | ||
|
|
||
| Here's an example of using `Object.values` to loop over property values: | ||
| 下面这个例子中,使用了 `Object.values` 在属性值上做循环操作: |
There was a problem hiding this comment.
『下面这个例子中,使用了 Object.values 在属性值上做循环操作:』 => 『这里有一个使用 Object.values 来遍历属性值的例子:』
| }; | ||
|
|
||
| // loop over values | ||
| // 循环所有的值 |
| 就像 `for..in` 循环,这些方法会忽略使用 `Symbol(...)` 作为键的属性。 | ||
|
|
||
| Usually that's convenient. But if we want symbolic keys too, then there's a separate method [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) that returns an array of only symbolic keys. Also, the method [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) returns *all* keys. | ||
| 通常这很方便。但是如果我们也想要获得 Symbol 类型的键,那么有另外不同的方法 [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) 它会返回一个只包含 Symbol 类型的键的数组。同样,[Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) 返回「所有」键。 |
There was a problem hiding this comment.
『同样,Reflect.ownKeys(obj) 返回「所有」键。』 => 『此外,Reflect.ownKeys(obj) 方法会返回「所有的」键。』
| Just like a `for..in` loop, these methods ignore properties that use `Symbol(...)` as keys. | ||
| 就像 `for..in` 循环,这些方法会忽略使用 `Symbol(...)` 作为键的属性。 | ||
|
|
||
| Usually that's convenient. But if we want symbolic keys too, then there's a separate method [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) that returns an array of only symbolic keys. Also, the method [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) returns *all* keys. |
There was a problem hiding this comment.
『它会返回一个只包含 Symbol 类型的键的数组。』
建议在最前面加一个逗号。
| - `Object.entries(user) = [ ["name","John"], ["age",30] ]` | ||
|
|
||
| 下面这个例子中,使用了 `Object.values` 在属性值上做循环操作: | ||
| 这里有一个使用 `Object.valuesa` 来遍历属性值的例子: |
|
@leviding 校对认领 |
| | 返回值 | 可迭代项 |「真正的」数组 | ||
|
|
||
| The first difference is that we have to call `Object.keys(obj)`, and not `obj.keys()`. | ||
| 第一个区别是在对象中我们的调用语法是 `Object.key(obj)`,而不是 `obj.key()`。 |
There was a problem hiding this comment.
『Object.key(obj),而不是 obj.key()。』=>『Object.keys(obj),而不是 obj.keys()。』
|
@allenlongbaobao 可以修改啦 |
|
修改完毕,感谢校对。 |
|
很赞,整体没什么问题。 |
翻译完成 resolve #64