-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch03-objects.js
169 lines (134 loc) · 4.84 KB
/
ch03-objects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
console.log("=== Object literals ===");
var empty_object = {};
var stooge = {
"first-name": "Joe", // quotes are required, first-name is not legal JavaScript name
"last-name": "Howard" // quotes are required, last-name is not legal JavaScript name
};
var flight = {
airline: "Oceanic",
number: 815,
departure: {
IATA: "SYD",
time: "2008-09-22 14:55",
city: "Sydney"
},
arrival: {
IATA: "LAX",
time: "2008-09-23 10:42",
city: "Los Angeles"
}
};
console.log("=== Retrieval ===");
console.log(flight.departure.IATA); // "SYD"
console.log(stooge["first-name"]); // "Joe"
// attempt to retrieve a non existent member
console.log(stooge["middle-name"]); // undefined
console.log(flight.status); // undefined
console.log(stooge["FIRST-NAME"]); // undefined
// the || operator can be used to fill in default values
console.log(stooge["middle-name"] || "(none)");
console.log(flight.status || "unknown");
// retrieving values from undefined will throw a TypeError exception,
// the && operator guards
console.log(flight.equipment); // undefined
//flight.equipment.model; // throw "TypeError"
console.log(flight.equipment && flight.equipment.model); // undefined
console.log("=== Update ===");
// updating
stooge['first-name'] = 'Jerome';
console.log(stooge['first-name']);
// adding new properties
stooge['middle-name'] = 'Lester';
flight.equipment = {
model: 'Boeing 777'
};
flight.status = 'overdue';
console.log(flight.equipment.model);
console.log("=== Reference ===");
var x = stooge;
x.nickname = 'Curly';
var nick = stooge.nickname;
// nick is 'Curly', because x and stooge are reference to the same objects
console.log(nick);
var a = {}, b = {}, c = {}; // a, b and c each refer to a different empty object
a = b = c = {}; // a, b and c all refer to the same empty object
console.log("=== Prototype ===");
// Every object is linked to a prototype object from which it can inherit properties. All
// objects created from object literals are linked to Object.prototype, an object that
// comes standard with JavaScript.
if (typeof Object.beget !== 'function') {
// creates a new object that uses an old object o as its prototype
Object.beget = function (o) {
var F = function () {};
F.prototype = o;
return new F();
};
}
var another_stooge = Object.beget(stooge);
// the object’s prototype is not touched
another_stooge['first-name'] = 'Harry';
another_stooge['middle-name'] = 'Moses';
another_stooge.nickname = 'Moe';
console.log(another_stooge['first-name'] + " " + another_stooge['middle-name'] + " " + another_stooge.nickname);
console.log(stooge['first-name'] + " " + stooge['middle-name'] + " " + stooge.nickname);
// the prototype relationship is a dynamic
stooge.profession = 'actor';
console.log(another_stooge.profession); // 'actor'
console.log("=== Reflection ===");
console.log(typeof flight.number); // 'number'
console.log(typeof flight.status); // 'string'
console.log(typeof flight.arrival); // 'object'
console.log(typeof flight.manifest); // 'undefined'
// any property on the prototype chain can produce a value
console.log(typeof flight.toString); // 'function'
console.log(typeof flight.constructor); // 'function'
// hasOwnProperty method returns true if the object has a particular property
// does not look at the prototype chain
console.log(flight.hasOwnProperty('number')); // true
console.log(flight.hasOwnProperty('constructor')); // false
console.log("=== Enumeration ===");
// the order of the names is not guarantee
var name;
for (name in another_stooge) { //all of the properties including functions and prototype properties
if (typeof another_stooge[name] !== 'function') {
console.log(name + ': ' + another_stooge[name]);
}
}
console.log("");
// the properties appear in a particular order
var i;
var properties = [
'first-name',
'middle-name',
'last-name',
'profession'
];
for (i = 0; i < properties.length; i += 1) {
console.log(properties[i] + ': ' + another_stooge[properties[i]]);
}
console.log("=== Delete ===");
console.log(another_stooge.nickname); // 'Moe'
// Remove nickname from another_stooge, revealing the nickname of the prototype.
delete another_stooge.nickname;
console.log(another_stooge.nickname); // 'Curly'
console.log("=== Global Abatement ===");
// single global variable becomes the container for application
var MYAPP = {};
MYAPP.stooge = {
"first-name": "Joe",
"last-name": "Howard"
};
MYAPP.flight = {
airline: "Oceanic",
number: 815,
departure: {
IATA: "SYD",
time: "2008-09-22 14:55",
city: "Sydney"
},
arrival: {
IATA: "LAX",
time: "2008-09-23 10:42",
city: "Los Angeles"
}
};