@@ -16,49 +16,44 @@ JavaScript 是一個 *弱類型* 的程式語言,所以在 **任何** 情況
16
16
10 == 010;
17
17
10 == '-10';
18
18
19
- > ** ES5 Note:** Number literals that start with a ` 0 ` are interpreted as octal
20
- > (Base 8). Octal support for these has been ** removed** in ECMAScript 5 strict
21
- > mode.
22
- >
19
+ > ** ES5 注意:** 如果數字字面值的開頭是 ` 0 ` 它會強制轉為八進位數字解析。
20
+ > 而在 ES5 嚴格模式下,它已經被刪除了。
23
21
24
22
25
23
To avoid the issues above, use of the [ strict equal operator] ( #types.equality )
26
24
is ** highly** recommended. Although this avoids a lot of common pitfalls, there
27
25
are still many further issues that arise from JavaScript's weak typing system.
26
+ 為了去避免上驗的事件發生,我們會用 [ 嚴格等於操作符] ( #types.equality ) 這是強烈建議。
27
+ 因為它可以避免很多常見的問題,但 JavaScript 的弱類型系同仍然會導致一些其他問題。
28
28
29
- ### Constructors of Built-In Types
29
+ ### 內置類型的建構函式
30
30
31
- The constructors of the built in types like ` Number ` and ` String ` behave
32
- differently when being used with the ` new ` keyword and without it.
31
+ 內置類型(比如 ` Number ` 和 ` String ` )在被調用時,使用或不使用 ` new ` 的結果完全不同。
33
32
34
33
new Number(10) === 10; // False, Object and Number
35
34
Number(10) === 10; // True, Number and Number
36
35
new Number(10) + 0 === 10; // True, due to implicit conversion
37
36
38
- Using a built-in type like ` Number ` as a constructor will create a new ` Number `
39
- object, but leaving out the ` new ` keyword will make the ` Number ` function behave
40
- like a converter.
37
+ 使用內置類型 ` Number ` 作為建構函式會建造一個新的 ` Number ` 物件,而在不使用 ` new ` 關鍵字的 ` Number ` 函式更像是一個數字轉換器。
41
38
42
- In addition, passing literals or non-object values will result in even more
43
- type coercion.
39
+ 另外,在比較中引入物件的字面值會導致更加複雜的強制類型轉換。
44
40
45
- The best option is to cast to one of the three possible types ** explicitly ** .
41
+ 最好的方式是比較值的 ** 顯示 ** 的轉換成最有可能的三種形態
46
42
47
- ### Casting to a String
43
+ ### 轉換成字符串
48
44
49
45
'' + 10 === '10'; // true
50
46
51
- By prepending an empty string, a value can easily be cast to a string.
47
+ 將一個值加上空字符串可以輕鬆轉為字符串類型。
52
48
53
- ### Casting to a Number
49
+ ### 轉換成一個數字
54
50
55
51
+'10' === 10; // true
56
52
57
- Using the ** unary ** plus operator, it is possible to cast to a number.
53
+ 使用 ** 一元 ** 的加號操作符,可以把字符串轉為數字。
58
54
59
- ### Casting to a Boolean
60
-
61
- By using the ** not** operator twice, a value can be converted a boolean.
55
+ ### 轉換成一個 Bool
56
+ 通過使用 ** 否** 操作符兩字,可以把一個值轉換為 Bool。
62
57
63
58
!!'foo'; // true
64
59
!!''; // false
0 commit comments