|
1 | 1 | /*
|
2 |
| -Property Lookup |
3 |
| -If child does not have ask parent |
4 |
| -Everything in JS is an Object |
| 2 | +ES6 Classes - Syntactic Sugar |
| 3 | +Prototypal Inheritance |
| 4 | +
|
| 5 | +No keywords inside the class (bank, deposit(){}) apart from constructor |
| 6 | +Property(bank) are created on each instance |
| 7 | +Methods(deposit(){}) are on prototype |
5 | 8 | */
|
6 | 9 |
|
7 |
| -function Account(name, initialBalance) { |
8 |
| - this.name = name; |
9 |
| - this.balance = initialBalance; |
10 |
| - this.bank = "Bank Of America"; |
11 |
| - //This takes precedence over proto, if not present then it will look property and methods in proto |
| 10 | +class Account { |
| 11 | + constructor(name, initialBalance) { |
| 12 | + this.name = name; |
| 13 | + this.balance = initialBalance; |
| 14 | + } |
| 15 | + bank = "Chase"; //This gets created on each instance |
| 16 | + deposit(amount) { |
| 17 | + this.balance = this.balance + amount; |
| 18 | + console.log(`Hello ${this.name}, your balance is ${this.balance}`); |
| 19 | + } |
12 | 20 | }
|
13 | 21 |
|
14 |
| -Account.prototype.bank = "CHASE"; |
15 |
| -Account.prototype.deposit = function (amount) { |
16 |
| - this.balance = this.balance + amount; |
17 |
| - console.log(`Hello ${this.name}, your balance is ${this.balance}`); |
18 |
| -}; |
| 22 | +// Again, if want the a property on prototype to prevent memory issues, copy of same in each instance |
| 23 | +// Account.prototype.bank = "Bank of America"; |
19 | 24 |
|
20 | 25 | const john = new Account("john", 200);
|
21 |
| -const bob = new Account("bob", 400); |
22 |
| - |
23 |
| -// john.deposit(300); |
24 |
| -// bob.deposit(1000); |
25 |
| - |
26 | 26 | console.log(john);
|
27 |
| -console.log(bob.bank); |
| 27 | +console.log(john.name); |
| 28 | +console.log(john.bank); |
| 29 | +john.deposit(200); |
28 | 30 |
|
29 |
| -console.log({}); |
30 |
| -console.log([]); |
| 31 | +const bob = new Account("bob", 0); |
| 32 | +console.log(bob); |
| 33 | +console.log(bob.name); |
| 34 | +console.log(john.bank); |
| 35 | +bob.deposit(1000); |
0 commit comments