-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathClasses.js
367 lines (287 loc) · 8.47 KB
/
Classes.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* 💡"JavaScript-with-JC"
👉 Classes and Objects
The ES6 JavaScript supports the Object-Oriented programming components,
Classes are a template ( a blueprint ) for creating objects.
💡 9 Things you should know about Classes and Objects.
1) Ways to define a class ( Class Declaration, Class Expression).
2) Do Classes get Hoisted ?
3) Types of Constructor ( default constructor, Parameterized constructor ).
4) Class Inheritance.
5) Method Overriding.
6) Class Static Methods and Properties.
7) Private Properties in Class using "#".
8) Classes getters and setters.
9) 8 Ways to create objects in javascript.
*/
// 💡 Two ways to define class in javascript
// 1) Class Declaration
// 2) Class Expression
// 👉 1) Let's take an example Class Declaration
class Person {
// special method for creating and initializing an object
constructor(name, age) {
// instance members ( created for each object separately )
this.name = name;
this.age = age;
}
// prototype members ( created in Person.prototype common sharable with all object )
getInfo() {
console.log("name", this.name, "age", this.age);
}
}
console.log(typeof Person); // function, ( Person.prototype => Object.prototype => null )
// Everything in javascript is an object
const jayesh = new Person("jayesh", 24);
jayesh.getInfo(); // name jayesh age 24
// 👉 2) Let's take an example Class Expression
// unnamed class expression
const Person1 = class {
constructor(name, age) {
// instance members ( created for each object separately )
this.name = name;
this.age = age;
}
};
const sam = new Person1("sam", 24);
sam.getInfo(); // name sam age 24
// named class expression
const Person2 = class PersonClass {
constructor(name, age) {
// instance members ( created for each object separately )
this.name = name;
this.age = age;
}
};
const john = new Person("john", 24);
john.getInfo(); // name john age 24
////////////////////////////////////////////////////////////////
// 💡 Class Hoisting
// Class Declaration hoisting ? => TDZ
const Ford = new Car("black"); // Uncaught ReferenceError: Cannot access 'Car' before initialization
class Car {
constructor(color) {
this.color = color;
}
}
// Class Expression hoisting ? => TDZ (let, const) or undefined (var)
const Ferrari = new Car("red"); // Uncaught ReferenceError: Cannot access 'Car' before initialization
const Car = class {
constructor(color) {
this.color = color;
}
};
////////////////////////////////////////////////////////////////
// 💡 Two types of Constructor
// 1) default constructor
// 2) Parameterized constructor
// 1) Let's take an example default constructor in Class Declaration
class Animal1 {
// If we do not specify any constructor default constructor will be used
// prototype member
display() {
console.log("prototype member");
}
}
const cat = new Animal1();
cat.display();
// 2) Let's take an example parameterized constructor in Class Declaration
class Animal2 {
// parameterized constructor overriding default constructor
constructor(color) {
// instance member
this.color = color;
}
// prototype members
display() {
console.log("color", this.color);
}
}
const dog = new Animal2("black");
dog.display(); // color black
///////////////////////////////////////////////////////////////////
// 💡 Class Inheritance
class Parent {
constructor(name, age) {
// instance members
this.name = name;
this.age = age;
}
// prototype member
getInfo() {
console.log("name", this.name, "age", this.age);
}
}
const sachin = new Parent("sachin", "60");
sachin.getInfo(); // name sachin age 60
class Child extends Parent {
// case 1 :- if we don't specify constructor then default constrouctor calls super constructor automatically
// case 2 :- if we specify parameterized constructor then need to call super constructor at the starting
constructor(name, age) {
super(name, age); // similar to Parent.call(this, name, age);
}
}
const sachinBaby = new Child("sachinBaby", "28");
sachinBaby.getInfo(); // name sachinBaby age 28 ( Child inheriting the properties of Parent )
///////////////////////////////////////////////////////////////////
// 💡 Class Inheritance Method Overriding
class Father {
constructor(name, age) {
this.name = name;
this.age = age;
}
getInfo() {
console.log("father Info", this.name, this.age);
}
}
const daddy = new Father("daddy", 70);
daddy.getInfo(); // father Info daddy 70
class Son extends Father {
constructor(name, age, sport) {
super(name, age);
this.sport = sport;
}
// overriding the method getInfo() of Parent class
getInfo() {
console.log("Son Info", this.name, this.age, "plays", this.sport);
}
}
const babby = new Son("babby", 10, "cricket");
babby.getInfo(); // Son Info babby 10 plays cricket
///////////////////////////////////////////////////////////////////
// 💡 Class Static Methods and Properties ( created once and only access by class )
class Student {
static school = "little flower";
constructor(name, age) {
this.name = name;
this.age = age;
}
// can be used as utility functions
static ageDifference(a, b) {
return a - b;
}
// object or intance method
displaySchool() {
// can't access static members
console.log(this.school);
}
// class method
static displaySchoolName() {
console.log(this.school);
}
}
const stud1 = new Student("jonh", 15);
const stud2 = new Student("sam", 13);
// static method or class method
const ageDiff = Student.ageDifference(stud1.age, stud2.age);
console.log(ageDiff); // 2
Student.displaySchoolName(); // little flower
stud1.displaySchool(); // undefined
stud2.displaySchool(); // undefined
///////////////////////////////////////////////////////////////////
// 💡 Private Properties in Class using "#"
class BankAccount {
// private member
#amount;
constructor(name, amount) {
this.name = name; // this is public
this.#amount = amount; // this is private
}
// private method
#withdrawAmount(amount) {
this.#amount -= amount;
console.log(this.#amount);
}
displayAmount() {
// here you can access private members
console.log(this.#amount);
}
enterPinAndWithdraw(amount) {
this.#withdrawAmount(amount);
}
}
const account = new BankAccount("rakesh", 5000);
console.log(account.name); // rakesh
// console.log(account.#amount); // SyntaxError
account.displayAmount(); // 5000
// account.#withdrawAmount(); // SyntaxError
account.enterPinAndWithdraw(2000); // 3000
///////////////////////////////////////////////////////////////////
// 💡 Classes getters and setters
class User {
#name;
#age;
constructor(name, age) {
this.#name = name;
this.#age = age;
}
get name() {
return this.#name;
}
set name(name) {
this.#name = name;
}
get age() {
return this.#age;
}
set age(age) {
this.#age = age;
}
}
const mukesh = new User("mukesh", 44);
console.log(mukesh.name); // mukesh
console.log(mukesh.age); // 44
mukesh.name = "new mukesh";
console.log(mukesh.name); // new mukesh
mukesh.age = 45;
console.log(mukesh.age); // 45
///////////////////////////////////////////////////////////////////
// 💡 8 Ways to create object in javascript
// 1) using new keyword ( object constructor )
const obj1 = new Object();
obj1.name = "Jc";
obj1.age = 24;
console.log(obj1); // { name: 'Jc', age: 24 }
// 2) using object literals
const obj2 = {
name: "Jc",
age: 24,
};
console.log(obj2); // { name: 'Jc', age: 24 }
// 3) using function constructor
function Person3(name, age) {
this.name = name;
this.age = age;
}
const obj3 = new Person3("Jc", 24);
console.log(obj3); // Person3 { name: 'Jc', age: 24 }
// 4) using class and constructor
class Person4 {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const obj4 = new Person4("Jc", 24);
console.log(obj4); // Person4 { name: 'Jc', age: 24 }
// 5) using Object.create()
const obj5 = Object.create({ name: "Jc", age: 24 });
console.log(obj5.name); // Jc
console.log(obj5.age); // 24
// 6) using Object.assign()
const obj6 = Object.assign({}, { name: "Jc", age: 24 });
console.log(obj6); // { name: 'Jc', age: 24 }
// 7) using singleton pattern
const obj7 = new (function (name, age) {
this.name = name;
this.age = age;
})("Jc", 24);
console.log(obj7); // { name: 'Jc', age: 24 }
// 8) using factory function
function Person8(name, age) {
return {
name: name,
age: age,
};
}
const obj8 = Person8("Jc", 24);
console.log(obj8); // { name: 'Jc', age: 24 }