Repository navigation
Expand file tree
/
Copy pathbasicPerson.js
More file actions
295 lines (199 loc) · 5.77 KB
/
Copy pathbasicPerson.js
File metadata and controls
295 lines (199 loc) · 5.77 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//introduce yourself, ask sutdents to say their name
//it's a hard topic, understand the concepts that are the foundation of prototypes
//agenda (remember the concepts)
//zoom out to really big picutre, why are we learning this
//examples of prototypical synonyms and relationship
/***** function and object examples *****/
function sayHello () {
console.log('Hello!');
};
var person = {
name: 'Edwin',
age: 27
};
/**** make multiple objects of the same type ****/
var katie = {
name: 'Katie',
age: 24,
sayHello: sayHello
};
var erica = {
name: 'Erica',
age: 03,
sayHello: sayHello
};
// can anyone tell me what is inconvenient about this?
// - repetitive
// - you don't really know if they are of the same type
// - Katie could be a robot, Erica could be a person, a ghost, a
/********* we have a singular function **********/
var personMethods = {
sayHello: function () {
console.log('Hello!');
},
sayGoodbye: function () {
console.log('Goodbye!');
}
};
function Person (name, age) {
let person = {};
//property
person.name = name;
person.age = age;
//method
person.sayHello = personMethods.sayHello;
person.sayGoodbye = personMethods.sayGoodbye;
return person;
}
var katie = Person('Katie', 24);
var erica = Person('Erica', 30);
// what two lines of code can we write to make a katie and erica object like before? Pair up?
// what is still inconvenient about this?
// - adding another method means writing in two places
/****** using Object.create to delegate lookups to one personMethods object ********/
var personMethods = {
//method
sayHello: function () {
console.log('Hello!');
},
sayGoodbye: function() {
console.log('Goodbye!');
}
};
function Person (name, age) {
let person = Object.create(personMethods);
//let person = {};
person.name = name;
person.age = age;
//person.sayHello = personMethods.sayHello;
//person.sayGoodbye = personMethods.sayGoodbye;
return person;
}
var katie = Person('Katie', 24);
var erica = Person('Erica', 30);
//Inheritance for the first time with Object.Create
//It creates a new object and tells the Javascript engine where to lookup missing properties. In this case, personMethods object.
// - can anyone tell us how are code changes from explicit person naming
/**
Transition into what prototype literally is and why use it instead of personMethods.
**/
var parent = {
name: 'Edwin',
age: 27,
heritage: 'Korean'
};
var child = Object.create(parent);
child.name = 'Jimbo';
child.age = 2;
console.log(child.heritage);
function foo () { };
foo.prototype; //spits out an object
/***
STEP 1 in The Agenda
Key definition: prototype is just the property of a function, pointing to an object
So why don't we put all our personMethods into the prorotype of our Person function?
***/
/***** using Person.prototype instead of personMethods *****/
//this is called a Constructor function because it's constructing an object for us
function Person (name, age) {
var person = Object.create(Person.prototype);
//property
person.name = name;
person.age = age;
return person;
}
Person.prototype.sayHello = function() {
console.log('Hello!');
};
Person.prototype.sayGoodbye = function() {
console.log('Goodbye!');
};
//we are now creating instances of a Person prototype
// katie instanceof Person
// erica insatnceof Person
var katie = Person('Katie', 24);
var erica = Person('Erica', 30);
/***** using New contrsuctor and this *****/
function Person (name, age) {
//this = Object.create(this.prototype);
this.name = name;
this.age = age;
//return this
}
Person.prototype.sayHello = function() {
console.log('Hello!');
};
Person.prototype.sayGoodbye = function() {
console.log('Goodbye!');
};
var katie = new Person('Katie', 24);
var erica = new Person('Erica', 30);
/***
STEP 2 in The Agenda
we use prototypes as an efficient and expressive way to share properties to instances.
**/
/**** SUB CLASS PROTOTYPE ****/
//how protoypes chain together
function Person (name, age) {
//property
this.name = name;
this.age = age;
}
Person.prototype.sayHello = function() {
console.log('Hello!');
};
Person.prototype.sayGoodbye = function() {
console.log('Goodbye!');
};
function NewYorker(name, age, borough) {
//attach th Person attributes with *this* context
//inheritance
Person.call(this, name, age);
this.borough = borough;
this.isRude = true;
};
NewYorker.prototype = Object.create(Person.prototype);
//NewYorker.prototype.constructor = NewYorker;
NewYorker.prototype.sayThePhrase = function() {
console.log("I'm walkin' here!");
};
NewYorker.prototype.repTheirRegion = function() {
console.log(this.borough, 'is the place to be!');
};
var katie = new NewYorker('Katie', 24, 'Brooklyn');
var erica = new NewYorker('Erica', 30, 'Queens');
//katie instanceof NewYorker
//katie instanceof Person
//katie instanceof Object
/***
STEP 3 in The Agenda
How protoypes chain together and thus enable inheritance across multiple prototpyes
**/
/****** ES6 ******/
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log('Hello!');
}
sayGoodbye() {
console.log('Goodbye!');
}
}
class NewYorker extends Person {
constructor(name, age, borough) {
super(name, age);
this.borough = borough;
this.isRudeude = true;
}
sayThePhrase() {
console.log("I'm walkin' here");
}
repTheirRegion() {
console.log(this.borough, 'is the place to be!');
}
}
var katie = new NewYorker('Katie', 24, 'Brooklyn');
var erica = new NewYorker('Erica', 30, 'Queens');