|
1 |
| -## Type casting |
| 1 | +## 类型转换 |
2 | 2 |
|
3 |
| -JavaScript is a *weakly typed* language, so it will apply *type coercion* |
4 |
| -**wherever** possible. |
| 3 | +JavaScript 是*弱类型*语言,所以会在**任何**可能的情况下应用*强制类型转换*。 |
5 | 4 |
|
6 |
| - // These are true |
7 |
| - new Number(10) == 10; // Number.toString() is converted |
8 |
| - // back to a number |
| 5 | + // 下面的比较结果是:true |
| 6 | + new Number(10) == 10; // Number.toString() 返回的字符串被再次转换为数字 |
9 | 7 |
|
10 |
| - 10 == '10'; // Strings gets converted to Number |
11 |
| - 10 == '+10 '; // More string madness |
12 |
| - 10 == '010'; // And more |
13 |
| - isNaN(null) == false; // null converts to 0 |
14 |
| - // which of course is not NaN |
| 8 | + 10 == '10'; // 字符串被转换为数字 |
| 9 | + 10 == '+10 '; // 同上 |
| 10 | + 10 == '010'; // 同上 |
| 11 | + isNaN(null) == false; // null 被转换为数字 0 |
| 12 | + // 0 当然不是一个 NaN(译者注:否定之否定) |
15 | 13 |
|
16 |
| - // These are false |
| 14 | + // 下面的比较结果是:false |
17 | 15 | 10 == 010;
|
18 | 16 | 10 == '-10';
|
19 | 17 |
|
20 |
| -> **ES5 Note:** Number literals that start with a `0` are interpreted as octal |
21 |
| -> (Base 8). Octal support for these has been **removed** in ECMAScript 5 strict |
22 |
| -> mode. |
| 18 | +> **ES5 提示:** 以 `0` 开头的数字字面值会被作为八进制数字解析。 |
| 19 | +> 而在 ECMAScript 5 严格模式下,这个特性被**移除**了。 |
23 | 20 |
|
24 |
| -In order to avoid the above, use of the [strict equal operator](#types.equality) |
25 |
| -is **highly** recommended. Although this avoids a lot of common pitfalls, there |
26 |
| -are still many further issues that arise from JavaScript's weak typing system. |
| 21 | +为了避免上面复杂的强制类型转换,**强烈**推荐使用[严格的等于操作符](#types.equality)。 |
| 22 | +虽然这可以避免大部分的问题,但 JavaScript 的弱类型系统仍然会导致一些其它问题。 |
27 | 23 |
|
28 |
| -### Constructors of built-in types |
29 | 24 |
|
30 |
| -The constructors of the built in types like `Number` and `String` behave |
31 |
| -differently when being used with the `new` keyword and without it. |
| 25 | +### 内置类型的构造函数(Constructors of built-in types) |
32 | 26 |
|
33 |
| - new Number(10) === 10; // False, Object and Number |
34 |
| - Number(10) === 10; // True, Number and Number |
35 |
| - new Number(10) + 0 === 10; // True, due to implicit conversion |
| 27 | +内置类型(比如 `Number` 和 `String`)的构造函数在被调用时,使用或者不使用 `new` 的结果完全不同。 |
36 | 28 |
|
37 |
| -Using a built-in type like `Number` as a constructor will create a new `Number` |
38 |
| -object, but leaving out the `new` keyword will make the `Number` function behave |
39 |
| -like a converter. |
| 29 | + new Number(10) === 10; // False, 对象与数字的比较 |
| 30 | + Number(10) === 10; // True, 数字与数字的比较 |
| 31 | + new Number(10) + 0 === 10; // True, 由于隐式的类型转换 |
40 | 32 |
|
41 |
| -In addition, having literals or non-object values in there will result in even |
42 |
| -more type coercion. |
| 33 | +使用内置类型 `Number` 作为构造函数将会创建一个新的 `Number` 对象, |
| 34 | +而在不使用 `new` 关键字的 `Number` 函数更像是一个数字转换器。 |
43 | 35 |
|
44 |
| -The best option is to cast to one of the three possible types **explicitly**. |
| 36 | +另外,在比较中引入对象的字面值将会导致更加复杂的强制类型转换。 |
45 | 37 |
|
46 |
| -### Casting to a string |
| 38 | +最好的选择是把要比较的值**显式**的转换为三种可能的类型之一。 |
| 39 | + |
| 40 | + |
| 41 | +### 转换为字符串(Casting to a string) |
47 | 42 |
|
48 | 43 | '' + 10 === '10'; // true
|
49 | 44 |
|
50 |
| -By prepending a empty string a value can easily be casted to a string. |
| 45 | +将一个值加上空字符串可以轻松转换为字符串类型。 |
| 46 | + |
51 | 47 |
|
52 |
| -### Casting to a number |
| 48 | +### 转换为数字(Casting to a number) |
53 | 49 |
|
54 | 50 | +'10' === 10; // true
|
55 | 51 |
|
56 |
| -Using the **unary** plus operator it is possible to cast to a number. |
| 52 | +使用**一元**的加号操作符,可以把字符串转换为数字。 |
| 53 | + |
| 54 | +[译者注][30]:字符串转换为数字的常用方法: |
| 55 | + |
| 56 | + +'010' === 10 |
| 57 | + Number('010') === 10 |
| 58 | + parseInt('010', 10) === 10 // 用来转换为整数 |
| 59 | + |
| 60 | + +'010.2' === 10.2 |
| 61 | + Number('010.2') === 10.2 |
| 62 | + parseInt('010.2', 10) === 10 |
57 | 63 |
|
58 |
| -### Casting to a boolean |
| 64 | + |
| 65 | +### 转换为布尔型(Casting to a boolean) |
59 | 66 |
|
60 |
| -By using the **not** operator twice, a value can be converted a boolean. |
| 67 | +通过使用 **否** 操作符两次,可以把一个值转换为布尔型。 |
61 | 68 |
|
62 | 69 | !!'foo'; // true
|
63 | 70 | !!''; // false
|
|
0 commit comments