|
1 |
| -// All Objects in Javascript have access to constructor property that returns a constructor function that created it. |
2 |
| -// built in constructor functions |
3 |
| -// arrays and functions are objects in javascript |
| 1 | +/* |
| 2 | +Prototypal Inheritance Model |
| 3 | +Javascript uses prototypal inheritance model. That means that every constructor function/class has a prototype property that is shared by every instance of the constructor/class. So any properties and methods or prototype can be acessed by every instance. prototype property returns a object |
| 4 | +*/ |
4 | 5 |
|
5 |
| -/* Constructor Functions */ |
6 |
| -function Person(firstName, lastName) { |
7 |
| - this.firstName = firstName; |
8 |
| - this.lastName = lastName; |
9 |
| - this.fullName = function () { |
10 |
| - console.log( |
11 |
| - `Hello, my name is ${this.firstName} ${this.lastName} and I love React` |
12 |
| - ); |
13 |
| - }; |
| 6 | +function Account(name, initialBalance) { |
| 7 | + this.name = name; |
| 8 | + this.balance = initialBalance; |
14 | 9 | }
|
15 | 10 |
|
16 |
| -const john = new Person("john", "anderson"); |
17 |
| -console.log(john.constructor); |
| 11 | +Account.prototype.bank = "CHASE"; |
| 12 | +Account.prototype.deposit = function (amount) { |
| 13 | + this.balance = this.balance + amount; |
| 14 | + console.log(`Hello ${this.name}, your balance is ${this.balance}`); |
| 15 | +}; |
18 | 16 |
|
19 |
| -const bob = new Person("bob", "anderson"); |
20 |
| -console.log(bob.constructor); |
| 17 | +const john = new Account("john", 200); |
| 18 | +const bob = new Account("bob", 400); |
21 | 19 |
|
22 |
| -const object = {}; |
23 |
| -console.log(object.constructor); |
| 20 | +john.deposit(300); |
| 21 | +bob.deposit(1000); |
24 | 22 |
|
25 |
| -const array = []; |
26 |
| -console.log(array.constructor); |
27 |
| - |
28 |
| -const sayHi = function () {}; |
29 |
| -console.log(sayHi.constructor); |
30 |
| - |
31 |
| -const susy = new john.constructor("susy", "anderson"); |
32 |
| -susy.fullName(); |
| 23 | +console.log(john); |
| 24 | +console.log(bob.bank); |
0 commit comments