-
Notifications
You must be signed in to change notification settings - Fork 146
/
oop.js
220 lines (142 loc) · 4.91 KB
/
oop.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
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
/****************************************************************
WORKING WITH OBJECT LITERALS
****************************************************************/
/*** CHALLENGE 1 of 1 ***/
function makePerson(name, age) {
const person = {};
person.name = name;
person.age = age;
return person;
}
const vicky = makePerson('Vicky', 24);
// /********* Uncomment these lines to test your work! *********/
// console.log(vicky.name); // -> Logs 'Vicky'
// console.log(vicky.age); // -> Logs 24
/****************************************************************
USING OBJECT.CREATE
****************************************************************/
/*** CHALLENGE 1 of 3 ***/
const personStore = {
greet: () => console.log('hello'),
};
// /********* Uncomment this line to test your work! *********/
// personStore.greet(); // -> Logs 'hello'
/*** CHALLENGE 2 of 3 ***/
function personFromPersonStore(name, age) {
const person = Object.create(personStore);
person.name = name;
person.age = age;
return person;
}
const sandra = personFromPersonStore('Sandra', 26);
// /********* Uncomment these lines to test your work! *********/
// console.log(sandra.name); // -> Logs 'Sandra'
// console.log(sandra.age); //-> Logs 26
// sandra.greet(); //-> Logs 'hello'
/*** CHALLENGE 3 of 3 ***/
personStore.introduce = function () {
console.log(`Hi, my name is ${this.name}`);
}
// sandra.introduce(); // -> Logs 'Hi, my name is Sandra'
/****************************************************************
USING THE 'NEW' KEYWORD
****************************************************************/
/*** CHALLENGE 1 of 3 ***/
function PersonConstructor() {
this.greet = () => console.log('hello');
}
// /********* Uncomment this line to test your work! *********/
const simon = new PersonConstructor;
// simon.greet(); // -> Logs 'hello'
/*** CHALLENGE 2 of 3 ***/
function personFromConstructor(name, age) {
const person = new PersonConstructor();
person.name = name;
person.age = age;
return person;
}
const mike = personFromConstructor('Mike', 30);
// /********* Uncomment these lines to test your work! *********/
// console.log(mike.name); // -> Logs 'Mike'
// console.log(mike.age); //-> Logs 30
// mike.greet(); //-> Logs 'hello'
/*** CHALLENGE 3 of 3 ***/
PersonConstructor.prototype.introduce = function() {
console.log(`Hi, my name is ${this.name}`);
}
// mike.introduce(); // -> Logs 'Hi, my name is Mike'
/****************************************************************
USING ES6 CLASSES
****************************************************************/
/*** CHALLENGE 1 of 3 ***/
class PersonClass {
constructor(name) {
this.name = name;
}
greet() {
console.log('hello');
}
}
// /********* Uncomment this line to test your work! *********/
const george = new PersonClass();
// george.greet(); // -> Logs 'hello'
/*** CHALLENGE 2 of 3 ***/
class DeveloperClass extends PersonClass {
constructor(name, age) {
super(name);
this.age = age;
}
introduce() {
console.log(`Hello World, my name is ${this.name}`)
}
}
// /********* Uncomment these lines to test your work! *********/
// const thai = new DeveloperClass('Thai', 32);
// console.log(thai.name); // -> Logs 'Thai'
// thai.introduce(); //-> Logs 'Hello World, my name is Thai'
/****************************************************************
EXTENSION: SUBCLASSING
****************************************************************/
const userFunctionStore = {
sayType: function () {
console.log(`I am a ${this.type}`);
}
}
function userFactory(name, score) {
let user = Object.create(userFunctionStore);
user.type = "User";
user.name = name;
user.score = score;
return user;
}
const adminFunctionStore = Object.create(userFunctionStore);
function adminFactory(name, score) {
const admin = new userFactory(name, score);
admin.type = 'Admin';
return admin;
}
/* Put code here for a method called sharePublicMessage*/
userFunctionStore.sharePublicMessage = () => console.log('Welcome users!');
const adminFromFactory = adminFactory("Eva", 5);
// /********* Uncomment these lines to test your work! *********/
adminFromFactory.sayType() // -> Logs "I am a Admin"
adminFromFactory.sharePublicMessage() // -> Logs "Welcome users!"
/****************************************************************
EXTENSION: MIXINS
****************************************************************/
class Dog {
constructor() {
this.legs = 4;
}
speak() {
console.log('Woof!');
}
}
const robotSkillsMixin = {
skin: 'metal',
speak: function () { console.log(`I have ${this.legs} legs and am made of ${this.skin}`) },
}
let robotFido = new Dog();
robotFido = Object.assign(robotFido, robotSkillsMixin);
// /********* Uncomment these lines to test your work! *********/
robotFido.speak() // -> Logs "I am made of metal"