/
1-the-constructor-pattern.es2015.js
179 lines (137 loc) · 5.25 KB
/
1-the-constructor-pattern.es2015.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
170
171
172
173
174
175
176
177
178
179
//*******************************************************//
// Object Creation
//*******************************************************//
//********************** Snippet 1 **********************//
// [ES2015+] We used new keyword const for immutable constant declaration
// Each of the following options will create a new empty object:
const newObject = {};
// or
const newObject = Object.create(Object.prototype);
// or
const newObject = new Object();
//********************** Snippet 2 **********************//
// ECMAScript 3 compatible approaches
// 1. Dot syntax
// Set properties
newObject.someKey = 'Hello World';
// Get properties
// [ES2015+] We used new keyword const for immutable constant declaration
const value = newObject.someKey;
// 2. Square bracket syntax
// Set properties
newObject['Some Key'] = 'Hello World';
// Get properties
// [ES2015+] We used new keyword const for immutable constant declaration
const value = newObject['Some Key'];
// ECMAScript 5 only compatible approaches
// For more information see: http://kangax.github.com/es5-compat-table/
// 3. Object.defineProperty
// Set properties
Object.defineProperty(newObject, 'someKey', {
value: "for more control of the property's behavior",
writable: true,
enumerable: true,
configurable: true,
});
// If the above feels a little difficult to read, a short-hand could
// be written as follows:
// [ES2015+] We used new keyword const for immutable constant declaration
// [ES2015+] We used new arrow function expression syntax
const defineProp = (obj, key, value) => {
const config = {
value: value,
writable: true,
enumerable: true,
configurable: true,
};
Object.defineProperty(obj, key, config);
};
// To use, we then create a new empty "person" object
// [ES2015+] We used new keyword const for immutable constant declaration
const person = Object.create(Object.prototype);
// Populate the object with properties
defineProp(person, 'car', 'Delorean');
defineProp(person, 'dateOfBirth', '1981');
defineProp(person, 'hasBeard', false);
console.log(person);
// Outputs: Object {car: "Delorean", dateOfBirth: "1981", hasBeard: false}
// 4. Object.defineProperties
// Set properties
Object.defineProperties(newObject, {
someKey: {
value: 'Hello World',
writable: true,
},
anotherKey: {
value: 'Foo bar',
writable: false,
},
});
// Getting properties for 3. and 4. can be done using any of the
// options in 1. and 2.
//********************** Snippet 3 **********************//
// Usage:
// Create a race car driver that inherits from the person object
// [ES2015+] We used new keyword const for immutable constant declaration
const driver = Object.create(person);
// Set some properties for the driver
defineProp(driver, 'topSpeed', '100mph');
// Get an inherited property (1981)
console.log(driver.dateOfBirth);
// Get the property we set (100mph)
console.log(driver.topSpeed);
//*******************************************************//
// Basic Constructors
//*******************************************************//
//********************** Snippet 1 **********************//
// [ES2015+] Below we used new class declaration, using keyword class
// [ES2015+] We used new constructor method and method declaration
// [ES2015+] Classes are syntactic sugar over JavaScript's prototype-based inheritance
// [ES2015+] We used new template literals for string interpolation
class Car {
constructor(model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
}
toString() {
return `${this.model} has done ${this.miles} miles`;
}
}
// Usage:
// We can create new instances of the car
// [ES2015+] We used new keyword const for immutable constant declaration
const civic = new Car('Honda Civic', 2009, 20000);
const mondeo = new Car('Ford Mondeo', 2010, 5000);
// and then open our browser console to view the
// output of the toString() method being called on
// these objects
console.log(civic.toString());
console.log(mondeo.toString());
//*******************************************************//
// Constructors With Prototypes
//*******************************************************//
//********************** Snippet 1 **********************//
// [ES2015+] Classes are syntactic sugar over JavaScript's prototype-based inheritance
// [ES2015+] We used new constructor method and method declaration
// [ES2015+] All of it new syntax sugar above old function structures
class Car {
constructor(model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
}
}
// Note here that we are using Object.prototype.newMethod rather than
// Object.prototype so as to avoid redefining the prototype object
// [ES2015+] We still could use Object.prototype for adding new methods, because internally we use the same structure
// [ES2015+] We used new template literals for string interpolation
Car.prototype.toString = function() {
return `${this.model} has done ${this.miles} miles`;
};
// Usage:
// [ES2015+] We used new keyword const for immutable constant declaration
const civic = new Car('Honda Civic', 2009, 20000);
const mondeo = new Car('Ford Mondeo', 2010, 5000);
console.log(civic.toString());
console.log(mondeo.toString());