Skip to content

Commit 04c8e4e

Browse files
committed
Call
1 parent 32c8d5a commit 04c8e4e

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

app.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
1-
/*
2-
ES6 Classes - Syntactic Sugar
3-
Prototypal Inheritance
1+
// call - runs instantly, arguments - list of items
42

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
8-
*/
3+
const john = {
4+
name: "john",
5+
age: 25,
6+
greet: function () {
7+
console.log(this);
8+
console.log(`Hi, my name is ${this.name} and ${this.age}`);
9+
},
10+
};
911

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+
const susan = {
13+
name: "susan",
14+
age: 21,
15+
};
16+
17+
function greet() {
18+
console.log(this);
19+
console.log(`Hi, my name ${this.name} and I am ${this.age} years old`);
2020
}
2121

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";
22+
// this will fail
23+
// susan.greet();
24+
// greet();
25+
const secondGreet = john.greet;
26+
// secondGreet();
2427

25-
const john = new Account("john", 200);
26-
console.log(john);
27-
console.log(john.name);
28-
console.log(john.bank);
29-
john.deposit(200);
28+
greet.call(john);
29+
greet.call(susan);
30+
greet.call({ name: "peter", age: 30 });
3031

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);
32+
// Making "this" point to susan object
33+
john.greet.call(susan);

0 commit comments

Comments
 (0)