|
98
|
1 Objects
|
|
|
2 =======
|
|
|
3
|
|
|
4 JavaScript has objects and is object-oriented, but in an unusual way
|
|
|
5 (more on that later).
|
|
100
|
6
|
|
|
7
|
|
|
8 Simple objects
|
|
|
9 --------------
|
|
|
10
|
|
|
11 Simple objects are like dictionaries or hashes in other languages.
|
|
|
12 They support key-value storage and access to attributes. Here's how
|
|
|
13 to create a simple object.
|
|
|
14
|
|
|
15 .. code-block:: javascript
|
|
|
16
|
|
|
17 js> obj = {}
|
|
|
18 [object Object]
|
|
|
19
|
|
|
20 js> typeof obj
|
|
|
21 object
|
|
|
22
|
|
|
23 We can store attribute values in a simple object (or any object in the
|
|
|
24 doc:`object-tree`).
|
|
|
25
|
|
|
26 .. code-block:: javascript
|
|
|
27
|
|
|
28 js> obj.s = 'hi'
|
|
|
29 hi
|
|
|
30 js> obj.i = 10
|
|
|
31 10
|
|
|
32
|
|
|
33 We can get these values back again.
|
|
|
34
|
|
|
35 .. code-block:: javascript
|
|
|
36
|
|
|
37 js> obj.s === 'hi'
|
|
|
38 true
|
|
|
39 js> obj.i == 10
|
|
|
40 true
|
|
|
41
|
|
|
42
|
|
|
43 Missing attributes
|
|
|
44 ------------------
|
|
|
45
|
|
|
46 It's not an error to ask for something that's not there. We get
|
|
|
47 undefined.
|
|
|
48
|
|
|
49 .. code-block:: javascript
|
|
|
50
|
|
|
51 js> obj.dne === undefined
|
|
|
52 true
|
|
|
53
|
|
|
54 We still get undefined if we set the value to undefined.
|
|
|
55
|
|
|
56 .. code-block:: javascript
|
|
|
57
|
|
|
58 js> obj.undef = undefined
|
|
|
59 undefined
|
|
|
60 js> obj.dne === undefined
|
|
|
61 true
|
|
|
62
|
|
|
63 You can use null to signal that there is a value, but that it is None.
|
|
|
64
|
|
|
65 .. code-block:: javascript
|
|
|
66
|
|
|
67 js> obj.none = null
|
|
|
68 null
|
|
|
69 js> obj.none === null
|
|
|
70 true
|
|
|
71
|
|
|
72
|
|
|
73 hasOwnProperty
|
|
|
74 --------------
|
|
|
75
|
|
|
76 You can use hasOwnProperty to help figure out why an object has an
|
|
|
77 undefined attribute. It's also useful when inspecting the
|
|
|
78 doc::`object-tree`. Often, however, it's better to write your code so
|
|
|
79 you don't need to do this (for example by using null).
|
|
|
80
|
|
|
81 .. code-block:: javascript
|
|
|
82
|
|
|
83 js> obj.hasOwnProperty('dne')
|
|
|
84 false
|
|
|
85 js> obj.hasOwnProperty('undef')
|
|
|
86 true
|
|
|
87 js> obj.hasOwnProperty('none')
|
|
|
88 true
|
|
|
89
|
|
|
90
|
|
|
91 Object literals
|
|
|
92 ---------------
|
|
|
93
|
|
|
94 You can create a simple object by placing key-value pairs in the curly
|
|
|
95 braces.
|
|
|
96
|
|
|
97 .. code-block:: javascript
|
|
|
98
|
|
|
99 js> someone = {
|
|
|
100 > 'name': 'Joe Doe',
|
|
|
101 > 'age': 43
|
|
|
102 > }
|
|
|
103 [object Object]
|
|
|
104
|
|
|
105 js> someone.age
|
|
|
106 43
|
|
|
107
|
|
|
108 Take care *not* to put a trailing semicolon in the object literal. It
|
|
|
109 will work in Firefox but not in Internet Explorer.
|
|
|
110
|
|
|
111
|
|
|
112 Arrays
|
|
|
113 ------
|
|
|
114
|
|
|
115 An array is a list of items. You can put anything is as a value for
|
|
|
116 the list. Use square brackets to create an array.
|
|
|
117
|
|
|
118 .. code-block:: javascript
|
|
|
119
|
|
|
120 js> array = []
|
|
|
121
|
|
|
122 Arrays expand to accomodate the data you store in them. You can even
|
|
|
123 leave gaps.
|
|
|
124
|
|
|
125 .. code-block:: javascript
|
|
|
126
|
|
|
127 js> array[0] = 'zero'
|
|
|
128 zero
|
|
|
129 js> array[3] = 'three'
|
|
|
130 three
|
|
|
131
|
|
|
132 An array turned into a string consists of the string on its entries
|
|
|
133 joined by commas.
|
|
|
134
|
|
|
135 .. code-block:: javascript
|
|
|
136
|
|
|
137 js> array
|
|
|
138 zero,,,three
|
|
|
139 js> array[1] === undefined
|
|
|
140 true
|
|
|
141
|
|
|
142
|
|
|
143 Array literals
|
|
|
144 --------------
|
|
|
145
|
|
|
146 As with simple objects, simply place the values between the square
|
|
|
147 brackets, separated by commas.
|
|
|
148
|
|
|
149 .. code-block:: javascript
|
|
|
150
|
|
|
151 js> seasons = ['spring', 'summer', 'autumn', 'winter']
|
|
|
152 spring,summer,autumn,winter
|
|
|
153
|
|
|
154
|
|
|
155 As with simple objects, beware of trailing commas and missing entries.
|
|
|
156 This will work in some browsers and not others.
|
|
|
157
|
|
|
158
|
|
|
159 JSON
|
|
|
160 ----
|
|
|
161
|
|
|
162 `JSON <http://www.json.org/>`_,stands for JavaScript Simple Object
|
|
|
163 Notation. It is very much one of the best parts of JavaScript. It is
|
|
|
164 `the fat-free alternative to XML <http://www.json.org/xml.html>`_, and
|
|
|
165 is widely used in AJAX (instead of XML). JSON objects are object and
|
|
|
166 array literals constructed using only simple objects, arrays, strings,
|
|
|
167 true, false and null.
|
|
|
168
|
|
|
169 If your code accepts JSON objects then it will be a lot easier to use
|
|
|
170 it with AJAX or otherwise integrate it with other systems.
|
|
|
171
|
|
|
172 Many programming languages have JSON libraries. You don't have to use
|
|
|
173 JavaScript to use JSON.
|
|
|
174
|
|
|
175 To provide standards, there are rules on how to write a JSON objects.
|
|
|
176 Many applications and programming languages will generate valid JSON
|
|
|
177 for you. I find YAML a convenient way of authoring JSON data.
|