-
Notifications
You must be signed in to change notification settings - Fork 5
/
11-prototypes.js
172 lines (127 loc) · 3.01 KB
/
11-prototypes.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
/**
*
* Prototypes
*
*/
// A "constructor call" makes an object its own prototype
/**
*
* Prototypal class
*
*/
function Workshop(teacher) {
this.teacher = teacher;
}
Workshop.prototype.ask = function (question) {
console.log(this.teacher, question);
};
var deepJS = new Workshop("Kyle");
var reactJS = new Workshop("Suzy");
deepJS.ask("Is 'prototype' a class?");
// 'Kyle' 'Is \'prototype\' a class?'
reactJS.ask("Isn't 'prototype' ugly?");
// 'Suzy' 'Isn\'t \'prototype\' ugly?'
/**
*
* Dunder prototypes
*
*/
function Workshop(teacher) {
this.teacher = teacher;
}
Workshop.prototype.ask = function (question) {
console.log(this.teacher, queueMicrotask);
};
var deepJS = new Workshop("Kyle");
deepJS.constructor === Workshop; // true
deepJS.__proto__ === Workshop.prototype; // true
Object.getPrototypeOf(deepJS) === Workshop.prototype; // true
/**
*
* Shadowing prototypes
*
*/
function Workshop(teacher) {
this.teacher = teacher;
}
Workshop.prototype.ask = function (question) {
console.log(this.teacher, queueMicrotask);
};
var deepJS = new Workshop("Kyle");
deepJS.ask = function (question) {
this.ask(question.toUpperCase());
};
deepJS.ask("Oops, is this infinite recursion?");
// RangeError: Maximum call stack size exceeded
function Workshop(teacher) {
this.teacher = teacher;
}
Workshop.prototype.ask = function (question) {
console.log(this.teacher, question);
};
var deepJS = new Workshop("Kyle");
deepJS.ask = function (question) {
this.__proto__.ask.call(this, question.toUpperCase());
};
deepJS.ask("Is this fake polymorphism?");
// 'Kyle' 'IS THIS FAKE POLYMORPHISM?'
/**
*
* Prototypal inheritance
*
*/
function Workshop(teacher) {
this.teacher = teacher;
}
Workshop.prototype.ask = function (question) {
console.log(this.teacher, question);
};
function AnotherWorkshop(teacher) {
Workshop.call(this, teacher);
}
AnotherWorkshop.prototype = Object.create(Workshop.prototype);
AnotherWorkshop.prototype.speakUp = function (msg) {
this.ask(msg.toUpperCase());
};
var JSRecentParts = new AnotherWorkshop("Kyle");
JSRecentParts.speakUp("Is this actually inheritance?");
// 'Kyle' 'IS THIS ACTUALLY INHERITANCE?'
/**
*
* OLOO (Objects Linked to Other Objects) pattern
*
*/
// ES6 class pattern
class Workshop {
constructor(teacher) {
this.teacher = teacher;
}
ask(question) {
console.log(this.teacher, question);
}
}
class AnotherWorkshop extends Workshop {
speakUp(msg) {
this.ask(msg);
}
}
var JSRecentParts = new AnotherWorkshop("Kyle");
JSRecentParts.speakUp("Are classes getting better?");
// OLOO pattern
var Workshop = {
setTeacher(teacher) {
this.teacher = teacher;
},
ask(question) {
console.log(this.teacher, question);
},
};
var AnotherWorkshop = Object.assign(Object.create(Workshop), {
speakUp(msg) {
this.ask(msg.toUpperCase());
},
});
var JSRecentParts = Object.create(AnotherWorkshop);
JSRecentParts.setTeacher("Kyle");
JSRecentParts.speakUp("But isn't this cleaner?");
//'Kyle' 'BUT ISN\'T THIS CLEANER?'