1
- ## Object Usage and Properties
1
+ ## 物件的使用和屬性
2
2
3
- Everything in JavaScript acts like an object, with the only two exceptions being
4
- [ ` null ` ] ( #core.undefined ) and [ ` undefined ` ] ( #core.undefined ) .
3
+ 每個變數可以表現像 JavaScript 物件,除了 [ ` null ` ] ( #core.undefined ) 和 [ ` undefined ` ] ( #core.undefined ) 。
5
4
6
5
false.toString(); // 'false'
7
6
[1, 2, 3].toString(); // '1,2,3'
@@ -10,37 +9,30 @@ Everything in JavaScript acts like an object, with the only two exceptions being
10
9
Foo.bar = 1;
11
10
Foo.bar; // 1
12
11
13
- A common misconception is that number literals cannot be used as
14
- objects. That is because a flaw in JavaScript's parser tries to parse the * dot
15
- notation* on a number as a floating point literal.
12
+ 一個常見的誤解就是字面值(literal)不是物件。這是因為 JavaScript 編譯器的一個錯誤,它試圖把 * 點操作符* 解析為浮點數的字面值的一部分。
16
13
17
- 2.toString(); // raises SyntaxError
14
+ 2.toString(); // 出錯: SyntaxError
18
15
19
- There are a couple of workarounds that can be used to make number literals act
20
- as objects too.
16
+ 有很多變通方法可以讓數字的字面值看起來像物件。
21
17
22
- 2..toString(); // the second point is correctly recognized
23
- 2 .toString(); // note the space left to the dot
24
- (2).toString(); // 2 is evaluated first
18
+ 2..toString(); // 第二個點號可以正常解析
19
+ 2 .toString(); // 注意點號前面的空格
20
+ (2).toString(); // 2 先被計算
25
21
26
- ### Objects as a Data Type
22
+ ### 物件做為數據類型
27
23
28
- Objects in JavaScript can also be used as [ * Hashmaps* ] [ 1 ] ; they mainly consist
29
- of named properties mapping to values.
24
+ JavaScript 的物件可以作為 [ * Hashmaps* ] [ 1 ] 使用,主要用來保存命名的建與值的對應關係。
30
25
31
- Using an object literal - ` {} ` notation - it is possible to create a
32
- plain object. This new object [ inherits] ( #object.prototype ) from ` Object.prototype ` and
33
- does not have [ own properties] ( #object.hasownproperty ) defined.
26
+ 使用物件的字面語法 - ` {} ` - 可以創建一個簡單的物件。 這個新創建的物件從 ` Object.prototype ` [ 繼承] ( #object.prototype ) ,下面,沒有任何 [ 字定義屬性] ( #object.hasownproperty ) 。
34
27
35
- var foo = {}; // a new empty object
28
+ var foo = {}; // 一個空的物件
36
29
37
- // a new object with a 'test' property with value 12
30
+ // 一個新的物件,有值為 12 的自定義屬性 'test'
38
31
var bar = {test: 12};
39
32
40
- ### Accessing Properties
33
+ ### 訪問屬性
41
34
42
- The properties of an object can be accessed in two ways, via either the dot
43
- notation or the square bracket notation.
35
+ 有兩種訪問物件的屬性,點操作或是中括號操作。
44
36
45
37
var foo = {name: 'kitten'}
46
38
foo.name; // kitten
@@ -52,15 +44,14 @@ notation or the square bracket notation.
52
44
foo.1234; // SyntaxError
53
45
foo[ '1234'] ; // works
54
46
55
- The notations work almost identically, with the only difference being that the
56
- square bracket notation allows for dynamic setting of properties and
57
- the use of property names that would otherwise lead to a syntax error.
47
+ 兩種語法是相等的,但是中括號在下面兩個情況依然有效
58
48
59
- ### Deleting Properties
49
+ - 動態設定屬性
50
+ - 屬性不是一個有較的變數名
60
51
61
- The only way to remove a property from an object is to use the ` delete `
62
- operator; setting the property to ` undefined ` or ` null ` only removes the
63
- * value * associated with the property, but not the * key * .
52
+ ### 刪除屬性
53
+
54
+ 唯一刪除屬性的方式就是用 ` delete ` 操作符。設置屬性為 ` undefined ` 或是 ` null ` 只有刪除的屬性和值的關聯,沒有真的刪掉屬性
64
55
65
56
var obj = {
66
57
bar: 1,
@@ -77,23 +68,21 @@ operator; setting the property to `undefined` or `null` only removes the
77
68
}
78
69
}
79
70
80
- The above outputs both ` bar undefined ` and ` foo null ` - only ` baz ` was
81
- removed and is therefore missing from the output.
71
+ 上面的輸出結果有 ` bar undefined ` 和 ` foo null `
72
+ 只有 ` baz ` 真正被刪除而已,所以從輸出結果中消失。
73
+
82
74
83
- ### Notation of Keys
75
+ ### 屬姓名的語法
84
76
85
77
var test = {
86
78
'case': 'I am a keyword, so I must be notated as a string',
87
79
delete: 'I am a keyword, so me too' // raises SyntaxError
88
80
};
89
81
90
- Object properties can be both notated as plain characters and as strings. Due to
91
- another mis-design in JavaScript's parser, the above will throw
92
- a ` SyntaxError ` prior to ECMAScript 5.
82
+ 物件的屬性名可以使用字符串或是普通的宣告。但是由於 JavaScript 編譯器有個另外一個錯誤設計。
83
+ 上面的兩種方式在 ECMAScript 5之前都會拋出 ` SyntaxError ` 的錯誤。
93
84
94
- This error arises from the fact that ` delete ` is a * keyword* ; therefore, it must be
95
- notated as a * string literal* to ensure that it will be correctly interpreted by
96
- older JavaScript engines.
85
+ 這個錯誤的原因是 ` delete ` 是 JavaScript 語言的一個 * 關鍵字* 因此為了在更低的版本能執行最好用 * string literal*
97
86
98
87
[ 1 ] : http://en.wikipedia.org/wiki/Hashmap
99
88
0 commit comments