-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.js
More file actions
465 lines (373 loc) · 12.3 KB
/
oop.js
File metadata and controls
465 lines (373 loc) · 12.3 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// // constructor functions only start with capital, it simulates the class
// const Person = function (firstName, birthYear) {
// this.firstName = firstName;
// this.birthYear = birthYear;
// // this is very bad practice, never use this, by performance
// // this.calcAge = function () {
// // console.log(2024 - 2000)
// // }
// }
// const jonas = new Person('Jonas', 1998);
// console.log(jonas);
// const tshewang = new Person('tshewang', 1998);
// const gyaltshen = new Person('gyaltshen', 2000);
// // 1. new {empty object is being created} created
// // 2. function is called, this ={}
// // 3. {} linked to prototype;
// // 4. function automatically return {}
// // console.log(jonas instanceof Person);
// //protypes, each and every functions is javascript contains the property called prototype, including the constructor functions
// // also contains for the objects
// Person.prototype.calcAge = function () {
// console.log(2024, -this.birthYear);
// }
// // without the prototype property it wont be able to access it
// jonas.calcAge();
// tshewang.calcAge();
// // very important in debugging while developing the frontend
// // console.log(jonas.hasOwnProperty('firstName'));
// // when the methods is envoked, first it will take a look inside the functions and if it cannot find there, it will look inside the function prototype
// // console.log(jonas.__proto__);
// // console.dir(Person.prototype.constructor);
// const arr = [2, 3, 4, 56];
// console.log(arr.__proto__);
// // extending the prototype of arrays, do not it in the partical
// Array.prototype.unique = function () {
// return [...new Set(this)]
// }
// console.log(arr.unique());
// // ES6 class, does not work like traditional classes in language like java and c++
// // class expression
// const PersonClq = class {
// }
// // class delcaration
// class PersonCl {
// // add the constructor methods, its just like the constructor functions that we have seen
// // all are the inherited methods
// constructor(firstName, birthYear) {
// this.firstName = firstName;
// this.birthYear = birthYear;
// }
// // this is a prototype property, methods will added to .prototype prototype
// calcAge() {
// console.log(2024 - this.birthYear);
// }
// // static methods,
// static hey() {
// }
// }
// const jessica = new PersonCl('jessica', 1900);
// console.log(jessica);
// // important things about the class
// // 1. classes are not hoisted
// // 2. class are first-class citizes,
// // 3. class are executed in strict mode
// // every objects will have getter and setter property
// const account = {
// ower: "tshewang",
// movements: [200, 520, 120, 202],
// // setting the setters and getters
// get lastest() {
// return this.movements.slice(-1).pop();
// },
// set lastest(mov) {
// this.movements.push(mov);
// },
// }
// console.log(account.lastest);
// account.set = 50;
// // implementing the prototyple inheritance
// // using the Object.create(), manually set the prototype of the object
// const PersonProto = {
// calcAge() {
// console.log(2023 - this.birthYear);
// },
// init(firstName, birthYear) {
// this.firstName = firstName;
// this.birthYear = birthYear
// }
// }
// const tshewang2 = Object.create(PersonProto);
// tshewang2.name = 'tshewang gyaltshen';
// tshewang2.birthYear = 1990;
// tshewang2.calcAge();
// tshewang2.init('sara', 1990);
// tshewang2.calcAge();
// // inheritance-> inheriting the methods and property from the parent class
// const Person = function (firsName, birthYear) {
// this.firsName = firsName;
// this.birthYear = birthYear;
// }
// Person.prototype.calcAge = function () {
// console.log(2024 - this.birthYear);
// }
// // child class of the Person
// const Student = function (firstName, birthYear, course) {
// // this.firsName = firstName; // this needs to inherit from Person
// // this.birthYear = birthYear; // this needs to inherit from Person
// Person.call(this, firstName, birthYear);
// this.course = course;
// }
// Person.prototype.calcAge = function () {
// console.log(2024 - this.birthYear);
// }
// // linking the prototype
// Student.prototype = Object.create(Person.prototype);
// Student.prototype.introduce = function () {
// console.log(`My name is ${this.firsName} and i study ${this.course}`)
// }
// const mike = new Student('Mike', 2020, 'computer science');
// mike.introduce();
// mike.calcAge();
// console.log(mike.__proto__);
// class PersonCl {
// // add the constructor methods, its just like the constructor functions that we have seen
// // all are the inherited methods
// constructor(firstName, birthYear) {
// this.firstName = firstName;
// this.birthYear = birthYear;
// }
// // this is a prototype property, methods will added to .prototype prototype
// calcAge() {
// console.log(2024 - this.birthYear);
// }
// // static methods,
// static hey() {
// }
// }
// // inheritance, needs extends and super
// class Student extends PersonCl {
// constructor(firstName, birthYear, course) {
// // always need to happen first
// super(firstName, birthYear);
// this.course = course;
// }
// introduce() {
// console.log(`My name is ${this.firstName} and i study ${this.course}`)
// }
// }
// const phurpha = new Student('phuba', 2012, 'computer science');
// console.log(phurpha);
// phurpha.introduce();
// phurpha.calcAge();
// inheritance between classes and Object.create
// const PersonProto = {
// calcAge() {
// console.log(2017 - this.birthYear);
// },
// init(firsName, birthYear) {
// this.firsName = firsName;
// this.birthYear = birthYear;
// }
// }
// const steven = Object.create(PersonProto);
// const StudentProto = Object.create(PersonProto);
// const jay = Object.create(StudentProto);
// const student = StudentProto.init();
// StudentProto.init = function () {
// PersonProto.init.call(this, firsName, birthYear);
// this.course = course
// }
// console.log(student);
class account {
constructor(owner, currency, pin) {
this.owner = owner;
this.currency = currency;
this.pin = pin
// protected property
this._movements = []
console.log('thanks for opening an account')
}
//public interface of our objects
deposit(val) {
this._movements.push(val);
}
withdraw(val) {
this.deposit(-val)
}
withdraw(val) {
this.deposit(-val)
}
approveLoan(val) {
return true;
}
requestLoan(val) {
if (this.approveLoan(val)) {
this.deposit(val);
console.log('loan approved');
}
}
// public interface, for the _movements
getMovements() {
return this._movements;
}
}
const account1 = new account('tshewang', 'EUR', 111);
account1.deposit(250);
account1.withdraw(140);
account1.requestLoan(1000);
account1.approveLoan(1000);
console.log(account1);
console.log(account1.getMovements());
// encapsulation, ->data privacy
// private class fields and methods ->encapsulation
// somehow the things that i have learned does not get connected while i am working on the project.
const Person3 = function (firstName, birthYear) {
this.firstName = firstName;
this.birthYear = birthYear;
}
// this is the server side code
const Student = function (firstName, birthYear, course) {
// inherit the properties from the Person3
Person3.call(this, firstName, birthYear);
this.course = course
}
Student.prototype.introduce = function () {
console.log(`My name is ${this.firstName} and i study ${this.course}`)
}
// this is the client side code
const mike = new Student('mike', 1990, 'computer science');
console.log(mike);
//using the es6 class
class PersonCl {
constructor(firstName, birthYear) {
this.firstName = firstName;
this.birthYear = birthYear;
}
calcAge() {
console.log(2024 - this.birthYear);
}
}
// extends keyword will setup the prototype chain for us
class StudentCl extends PersonCl {
constructor(firstName, birthYear, course) {
// inherit the properties from the Person3
// super => PersonCl.call(this, firstName, birthYear);
super(firstName, birthYear);
this.course = course
}
introduce() {
console.log(`My name is ${this.firstName} and i study ${this.course}`)
}
}
const tshewang = new StudentCl('tshewang', 1990, 'computer science');
const PersonProto = {
calcAge() {
console.log(2037 - this.birthYear);
},
init(firstName, birthYear) {
this.firstName = firstName;
this.birthYear = birthYear;
}
}
// creating the prototype chain
const steven = Object.create(PersonProto);
const StudentProto = Object.create(PersonProto);
StudentProto.init = function (firsName, birthYear) {
this.course = this.course;
}
// Object.create linking the object togather
const jay = Object.create(StudentProto);
jay.init('tshewang', 2020, 'computer science')
// this is the server side code
class Account {
#movements = [] // this is the private property
locale = navigator.nivations // this public property
// init the class object
constructor(owner, currency, pin) {
this.owner = owner;
this.currency = currency;
this.pin = pin;
this.movements = [];
this.locale = navigator.language;
console.log(`thank for opening an account, ${owner}`);
}
deposit(val) {
this.movements.push(val);
return this;
}
withdraw(val) {
this.deposit(-val);
return this;
}
approveLoan(val) {
return true;
}
requestLoan(val) {
if (this.approveLoan(val)) {
this.deposit(val);
console.log(`Approved loan`);
}
// the methods of chaining
return this;
}
}
// this is the client side code
const acct = new Account('Jonas', 'EUR', 111);
// arguments
acct.deposit(250);
acct.withdraw(140);
//output
console.log(acct);
// security risk
acct.requestLoan(); // this should not be avaiable to anyone
// thats why need need to encapsulation
// i get up every day to be best of what i am really capable of, that is the moto of my life
// push each and every inches of the life, because life fucks bad
// they may not understand the thing about it, but every thing need to be calculated with precision to min risk
// after all, everything is a strategy, you need too take risk, and do not play save, because you have nothing to lose
/** chaining methods **/ // some can fool you very easily, because they seems very convincing to you, just be careful
const chaining = acct.requestLoan().deposit(90).deposit(10).withdraw(90); // this is chaining methods, that we can commonly see in the object oriented programming
console.log(chaining);
class Car {
}
// challenging myself
class Carcl {
constructor(make, speed) {
this.make = make;
this.speed = speed;
}
accelerate() {
this.speed += 10;
console.log(`${this.make} is going at ${this.speed} km/h`);
}
brake() {
this.speed -= 5;
console.log(`${this.make} is going at ${this.speed} km/h`);
}
get speedUS() {
return this.speed / 1.6;
}
set speedUS(speed) {
this.speed = this.speed * 1.6;
};
}
class EVCL extends Carcl {
// making the property private
#charge
constructor(make, speed, charge) {
supper(make, speed);
this.charge = charge;
}
chargeBattery(chargeTo) {
this.charge = chargeTo
}
accelerate() {
this.speed += 20;
this.#charge--;
console.log(`${this.make} is going at ${this.speed} km/h with charge of ${this.#charge}`);
}
};
const EV = function (make, speed, charge) {
Car.call(this, make, speed);
this.charge = charge;
}
EV.prototype = Object.create(Car.prototype);
EV.prototype.chargeBattery = function (chargeTo) {
this.charge = chargeTo;
}
EV.prototype.accelerate = function () {
this.speed += 20;
this.charge--;
console.log(`${this.make} is going at ${this.speed} km/h, with a charge of ${this.charge}`);
};// lets see how the code have been written inside the node_modules file