1-js/04-object-basics/05-object-toprimitive#111
Conversation
|
@leviding 校对认领 |
|
@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> 一章中,我们已经看到了数值,字符串和布尔转换的规则。但是我没给对象留了一个空隙。正如我们所知道的方法和符号一样,现在我们可以关闭它。 |
| ``` | ||
|
|
||
| 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”。这是历史原因。 |
| - try `obj.valueOf()` and `obj.toString()`, whatever exists. | ||
| 1. 调用 `obj[Symbol.toPrimitive](hint)`如果这个方法存在的话, | ||
| 2. 否则如果暗示是`"string"` | ||
| - 尝试 `obj.toString()` 和 `obj.valueOf()`, 无论哪个存在。 |
There was a problem hiding this comment.
, 无论哪个存在 => ,无论哪个存在(去掉空格)
| 2. 否则如果暗示是`"string"` | ||
| - 尝试 `obj.toString()` 和 `obj.valueOf()`, 无论哪个存在。 | ||
| 3. 否则,如果暗示`"number"` 或者 `"default"` | ||
| - 尝试 `obj.valueOf()` 和 `obj.toString()`, 无论哪个存在。 |
There was a problem hiding this comment.
, 无论哪个存在 => ,无论哪个存在(去掉空格)
| obj[Symbol.toPrimitive] = function(hint) { | ||
| // return a primitive value | ||
| // 返回一个原始值 | ||
| // hint = one of "string", "number", "default" |
There was a problem hiding this comment.
注释未翻译 hint = "string", "number", "default" 中的一个
| alert(obj + 2); // 3 (ToPrimitive 返回布尔值,非字符串=>ToNumber) | ||
| ``` | ||
|
|
||
| ```smart header="Historical notes" |
There was a problem hiding this comment.
此处标题不知道有什么作用,为了不破坏其他地方可能有的引用,所以没有翻译。
There was a problem hiding this comment.
就翻译引号里面的Historical notes
| 由于历史原因,`toString` 或 `valueOf` 方法*应该*返回一个原始值:如果它们中的任何一个返回了一个对象,虽然不会报错,但是该对象被忽略(就像该方法不存在一样)。 | ||
|
|
||
| In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise, there will be an error. | ||
| 相反,Symbol.toPrimitive必须返回一个原始值,否则会出现错误。 |
There was a problem hiding this comment.
相反,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. | ||
| 对象到原始值的转换是由把一个原始值作为返回值的许多内置函数和操作符自动调用的。 |
There was a problem hiding this comment.
对象到原始值的转换是由把一个原始值作为返回值的许多内置函数和操作符自动调用的 => 对象到原始值的转换会被很多内置函数自动调用,还有将原始值作为返回值的操作器也会调用这种转换。
(这句话太长了,最好分解一下,不然读起来比较困难)
| - try `obj.valueOf()` and `obj.toString()`, whatever exists. | ||
| 1. 调用 `obj[Symbol.toPrimitive](hint)`如果这个方法存在的话, | ||
| 2. 否则如果暗示是`"string"` | ||
| - 尝试 `obj.toString()` 和 `obj.valueOf()`, 无论哪个存在。 |
| - 尝试 `obj.valueOf()` 和 `obj.toString()`, 无论哪个存在。 | ||
|
|
||
| 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()` 作为“全捕获"方法,对于一个对象的所有转换返回一个“人类可读”的形式,就足够了。 |
There was a problem hiding this comment.
在实践中,为了记录或调试目的,仅实现 obj.toString() 作为“全捕获"方法,对于一个对象的所有转换返回一个“人类可读”的形式,就足够了。=> 在实践中,为了记录或调试目的,仅实现 obj.toString() 作为“全捕获"方法通常就够了,这样所有转换都能返回一种“人类可读”的对象表达形式。
|
校对完毕 @leviding @elliott-zhao |
|
@elliott-zhao 自己再找下问题吧,我从后向前看,随意改了几个典型问题。一定要按照译者教程进行修改。 |
| ``` | ||
|
|
||
| ```smart header="Historical notes" | ||
| ```smart header="历史笔记" |
There was a problem hiding this comment.
这里notes肯定不能翻译成笔记,翻译成【历史原因】,【历史问题】之类的都行
| 由于历史原因,`toString` 或 `valueOf` 方法*应该*返回一个原始值:如果它们中的任何一个返回了一个对象,虽然不会报错,但是该对象被忽略(就像该方法不存在一样)。 | ||
|
|
||
| 相反,Symbol.toPrimitive必须返回一个原始值,否则会出现错误。 | ||
| 相反,`Symbol.toPrimitive` *必须*返回一个原始值,否则会出现错误。 |
|
@elliott-zhao 看下我的几个 commit,译文存在不少格式问题,根据 从 commit 和译者教程认真看下,避免下此类问题。 |
|
@athena0304 格式也需要校对哈 |
译文翻译完成,resolve #27