Skip to content

Commit 1cda28d

Browse files
committed
Prototype Property
1 parent 85edd8b commit 1cda28d

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

app.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
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+
*/
45

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;
149
}
1510

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+
};
1816

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);
2119

22-
const object = {};
23-
console.log(object.constructor);
20+
john.deposit(300);
21+
bob.deposit(1000);
2422

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

Comments
 (0)