-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.js
More file actions
42 lines (37 loc) · 1.01 KB
/
bind.js
File metadata and controls
42 lines (37 loc) · 1.01 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
// object use bind to borrow method from another object:
const normalPerson = {
firstName: 'Rahim',
lastName: 'Khan',
salary: 12000,
getFullName: function(){
console.log(this.firstName, this.lastName);
},
chargeBill: function(amount){
console.log(this);
this.salary = this.salary - amount;
return this.salary;
}
}
// normalPerson.chargeBill(500);
// console.log(normalPerson.salary);
// For Bind Structure
const heroPerson = {
firstName: 'balam',
lastName: 'khan',
salary: 500,
}
const friendlyPerson = {
firstName: 'kalam',
lastName: 'uddin',
salary: 300,
}
// heroPerson
const heroChargeBill = normalPerson.chargeBill.bind(heroPerson);
heroChargeBill(50);
console.log("HeroPerson = ", heroPerson.salary);
// friendlyPerson
const friendlyChargeBill = normalPerson.chargeBill.bind(friendlyPerson);
friendlyChargeBill(100);
console.log("FriendlyPerson = ", friendlyPerson.salary)
// normalPerson
console.log("NormalPerson = ", normalPerson.salary);