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

1-js/04-object-basics/05-object-toprimitive #111

Merged
merged 7 commits into from Jun 18, 2018
Merged

1-js/04-object-basics/05-object-toprimitive #111

merged 7 commits into from Jun 18, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jun 4, 2018

译文翻译完成,resolve #27

@leviding leviding changed the title 翻译完成 1-js/04-object-basics/05-object-toprimitive Jun 5, 2018
@athena0304
Copy link
Contributor

@leviding 校对认领

@leviding
Copy link
Member

leviding commented Jun 7, 2018

@athena0304 ok


In the chapter <info:type-conversions> we've seen the rules for numeric, string and boolean conversions of primitives. But we left a gap for objects. Now, as we know about methods and symbols it becomes possible to close it.
<info:type-conversions> 一章中,我们已经看到了数值,字符串和布尔转换的规则。但是我没给对象留了一个空隙。正如我们所知道的方法和符号一样,现在我们可以关闭它。
Copy link
Contributor

Choose a reason for hiding this comment

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

【我没】=> 【我们】

let total = car1 + car2;

// obj == string/number/symbol
if (user == 1) { ... };
```

The greater/less operator `<>` can work with both strings and numbers too. Still, it uses "number" hint, not "default". That's for historical reasons.
大于/小于运算符`<>`可以同时用于字符串和数字。不过,它使用“number”暗示,而不是“default”。这是历史原因。
Copy link
Contributor

Choose a reason for hiding this comment

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

可以同时用于 => 也可以同时用于

- try `obj.valueOf()` and `obj.toString()`, whatever exists.
1. 调用 `obj[Symbol.toPrimitive](hint)`如果这个方法存在的话,
2. 否则如果暗示是`"string"`
- 尝试 `obj.toString()` 和 `obj.valueOf()`, 无论哪个存在。
Copy link
Contributor

Choose a reason for hiding this comment

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

, 无论哪个存在 => ,无论哪个存在(去掉空格)

2. 否则如果暗示是`"string"`
- 尝试 `obj.toString()` `obj.valueOf()`, 无论哪个存在。
3. 否则,如果暗示`"number"` 或者 `"default"`
- 尝试 `obj.valueOf()` `obj.toString()`, 无论哪个存在。
Copy link
Contributor

Choose a reason for hiding this comment

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

, 无论哪个存在 => ,无论哪个存在(去掉空格)


```js
obj[Symbol.toPrimitive] = function(hint) {
// return a primitive value
// 返回一个原始值
// hint = one of "string", "number", "default"
Copy link
Contributor

Choose a reason for hiding this comment

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

注释未翻译 hint = "string", "number", "default" 中的一个

```js run
let obj = {
toString() {
return true;
}
};

alert(obj + 2); // 3 (ToPrimitive returned boolean, not string => ToNumber)
alert(obj + 2); // 3 (ToPrimitive 返回布尔值,非字符串=>ToNumber)
```

```smart header="Historical notes"
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
Author

Choose a reason for hiding this comment

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

此处标题不知道有什么作用,为了不破坏其他地方可能有的引用,所以没有翻译。

Copy link
Contributor

Choose a reason for hiding this comment

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

就翻译引号里面的Historical notes


In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise, there will be an error.
相反,Symbol.toPrimitive必须返回一个原始值,否则会出现错误。
Copy link
Contributor

Choose a reason for hiding this comment

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

相反,Symbol.toPrimitive必须返回一个原始值,否则会出现错误。 => 相反,Symbol.toPrimitive 必须返回一个原始值,否则会出现错误。


The object-to-primitive conversion is called automatically by many built-in functions and operators that expect a primitive as a value.
对象到原始值的转换是由把一个原始值作为返回值的许多内置函数和操作符自动调用的。
Copy link
Contributor

Choose a reason for hiding this comment

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

对象到原始值的转换是由把一个原始值作为返回值的许多内置函数和操作符自动调用的 => 对象到原始值的转换会被很多内置函数自动调用,还有将原始值作为返回值的操作器也会调用这种转换。
(这句话太长了,最好分解一下,不然读起来比较困难)

- try `obj.valueOf()` and `obj.toString()`, whatever exists.
1. 调用 `obj[Symbol.toPrimitive](hint)`如果这个方法存在的话,
2. 否则如果暗示是`"string"`
- 尝试 `obj.toString()` 和 `obj.valueOf()`, 无论哪个存在。
Copy link
Contributor

Choose a reason for hiding this comment

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

同样存在空格问题,【无论】前面的空格应该去掉


In practice, it's often enough to implement only `obj.toString()` as a "catch-all" method for all conversions that return a "human-readable" representation of an object, for logging or debugging purposes.
在实践中,为了记录或调试目的,仅实现 `obj.toString()` 作为“全捕获"方法,对于一个对象的所有转换返回一个“人类可读”的形式,就足够了。
Copy link
Contributor

Choose a reason for hiding this comment

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

在实践中,为了记录或调试目的,仅实现 obj.toString() 作为“全捕获"方法,对于一个对象的所有转换返回一个“人类可读”的形式,就足够了。=> 在实践中,为了记录或调试目的,仅实现 obj.toString() 作为“全捕获"方法通常就够了,这样所有转换都能返回一种“人类可读”的对象表达形式。

@athena0304
Copy link
Contributor

校对完毕 @leviding @elliott-zhao
有的句子有点长,可以适当拆解一下,不然读起来费劲。。

@leviding leviding added the WIP Work in process label Jun 12, 2018
@leviding
Copy link
Member

@elliott-zhao 自己再找下问题吧,我从后向前看,随意改了几个典型问题。一定要按照译者教程进行修改。

@leviding leviding added the enhancement New feature or request label Jun 12, 2018
@@ -208,15 +208,15 @@ alert(user + 500); // toString -> John500
alert(obj + 2); // 3 (ToPrimitive 返回布尔值,非字符串=>ToNumber)
```

```smart header="Historical notes"
```smart header="历史笔记"
Copy link
Contributor

Choose a reason for hiding this comment

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

这里notes肯定不能翻译成笔记,翻译成【历史原因】,【历史问题】之类的都行

由于历史原因,`toString` 或 `valueOf` 方法*应该*返回一个原始值:如果它们中的任何一个返回了一个对象,虽然不会报错,但是该对象被忽略(就像该方法不存在一样)。

相反,Symbol.toPrimitive必须返回一个原始值,否则会出现错误。
相反,`Symbol.toPrimitive` *必须*返回一个原始值,否则会出现错误。
Copy link
Contributor

Choose a reason for hiding this comment

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

这里【必须】应该是加粗的

@leviding leviding removed the enhancement New feature or request label Jun 13, 2018
@leviding
Copy link
Member

@elliott-zhao 看下我的几个 commit,译文存在不少格式问题,根据 从 commit 和译者教程认真看下,避免下此类问题。

@leviding
Copy link
Member

@athena0304 格式也需要校对哈

@leviding leviding merged commit 252ce14 into javascript-tutorial:zh-hans Jun 18, 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

2 participants