-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_object.js
95 lines (83 loc) · 2.84 KB
/
7_object.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
/**
* An object in JavaScript is essentially a group of key-value pairs, where a
* value can be either a property or a method.
*/
const umbrella = {
color: 'pink',
isOpen: false,
open: function() {
if (this.isOpen) {
return 'This umbrella is already open.';
} else {
this.isOpen = true;
return 'Successfully opened the umbrella!';
}
},
// If we do the following:
// open: () => {
// if (this.isOpen) { // "this" binds to the outer context, which is the global object in this case, so "this.isOpen" is undefined.
// return '...';
// } else {
// this.isOpen = true;
// return '...';
// }
// },
close: function() {
if (this.isOpen) {
this.isOpen = false;
return 'Successfully closed the umbrella!';
} else {
return 'This umbrella is already closed!';
}
},
// Define getter method
get matchColors() {
return ['blue', 'purple', 'yellow'];
}
};
console.log(umbrella);
console.log(`Type of umbrella: ${typeof umbrella}`); // object
// Check for property
console.log(umbrella.hasOwnProperty('open')); // true
console.log();
console.log(umbrella.open());
console.log(umbrella.close());
console.log();
console.log(`Matching colors: ${umbrella.matchColors}`); // Since matchColor() is defined as a getter method, access it like accessing a property
console.log();
// *** Keys & Values ***
console.log(Object.keys(umbrella)); // ['color', 'isOpen', 'open', 'close']
console.log(Object.values(umbrella)); // ['pink', true, [Function: open], [Function: close]]
console.log();
// *** Freezing ***
// Normally, an object declared with "const" can be freely add, update, or
// remove attributes, as long as that object still points to the same object.
// If we really want a frozen object that disallow modifications:
const obj = Object.freeze({ a: 1 });
// In order to totally freeze an object, use the following function:
const freeze = obj => {
Object.freeze(obj);
Object.entries(obj).forEach((key, val) => {
if (typeof key === 'object') {
// Recursively call freeze()
freeze(val);
}
});
};
// *** JSON Convertion ***
const converted = JSON.stringify(umbrella);
console.log(converted);
console.log(JSON.parse(converted));
console.log();
// *** "Inheritance" / Prototype Chaining ***
// We can define an object to be the "prototype" of another object.
// Essentially, this is like "copyping" or "extending" from the original object.
// e.g., a -> b
// If a field lookup fails on b, the field lookup is delegated to the
// upstream along the prototype chain, and the interpreter will look up
// the field in a.
const umbrellaChild = Object.create(umbrella);
// The field lookup of every JavaScript object, EVENTUALLY, delegates to
// "Object.prototype".
// Check for property along the prototype chain
console.log('open' in umbrellaChild); // true