-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind-method.js
51 lines (42 loc) · 1.04 KB
/
bind-method.js
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
43
44
45
46
47
48
49
50
51
let user = {
userName: "nagato",
specialty: "rinngegan",
clan: "uzumaki",
info: {
userName: "nagato",
specialty: "rinngegan",
jutsu: "shinra tensei",
msg: function () {
console.log(this.userName + "'s specialty is " + this.specialty);
},
},
};
user.info.msg();
user.info.msg.bind(user); //! will return function definition
const userFn = user.info.msg.bind(user);
userFn();
console.log("---------------------");
let zakir = {
fullName: "Zakir Hossian",
dob: 1994,
age: function (currentYear, msg) {
console.log(
msg + this.fullName + " is " + (currentYear - this.dob) + " years old"
);
},
};
zakir.age(2021, "Congratulations! ");
let ohi = {
fullName: "Ohi Miya",
dob: 1999,
};
// zakir.age.call(ohi, 2021, "Move on! ");
// zakir.age.apply(ohi, [2021, "Yeayy "]);
const ohiInfo = zakir.age.bind(ohi, 2020);
ohiInfo();
ohiInfo("Wow! ");
console.log("---------------------");
const ohiInfo2 = zakir.age.bind(ohi);
ohiInfo2(2021, "Hello Boss! ");
ohiInfo2(2005, "Nice! ");
ohiInfo2(2000, "Damn! ");