-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallBindApply.js
More file actions
89 lines (64 loc) · 2.27 KB
/
callBindApply.js
File metadata and controls
89 lines (64 loc) · 2.27 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//call function
//With the call() method, you can write a method that can be used on different objects.
//version 1
// let user1 = {
// firstName: 'Prashant',
// lastName: 'Maurya',
// printFullName: function () {
// console.log(this.firstName + ' ' + this.lastName);
// },
// };
// user1.printFullName();
// let user2 = {
// firstName: 'Sumit',
// lastName: 'Kumar',
// };
// //function borrowing
// user1.printFullName.call(user2);
//version 2
// let user1 = {
// firstName: 'Prashant',
// lastName: 'Maurya',
// };
// let printFullName = function () {
// console.log(this.firstName + ' ' + this.lastName);
// };
// printFullName.call(user1);
// let user2 = {
// firstName: 'Sumit',
// lastName: 'Kumar',
// };
// // function borrowing
// printFullName.call(user2);
//version 3
// let user1 = {
// firstName: 'Prashant',
// lastName: 'Maurya',
// };
// let printFullName = function(homeTown, city, state, country) {
// console.log(this.firstName + ' ' + this.lastName + ' lives in ' + homeTown + ', ' + city + ', ' + state + ', ' + country);
// };
// printFullName.call(user1, 'Jankipuram', 'Lucknow', 'Uttar Pradesh', 'India');
// let user2 = {
// firstName: 'Sumit',
// lastName: 'Kumar',
// };
// function borrowing
// printFullName.call(user2, 'Charbagh', 'Lucknow', 'Uttar Pradesh', 'India');
//-----------------------------------------------------------
//apply
//With the apply() method, you can write a method that can be used on different objects.
//it is different from call function is that how arguments are passed
// printFullName.apply(user1, ['Jankipuram', 'Lucknow', 'Uttar Pradesh', 'India']);
//------------------------------------------------------------
//The Difference Between call() and apply()
// The difference is:
// The call() method takes arguments separately.
// The apply() method takes arguments as an array.
// The apply() method is very handy if you want to use an array instead of an argument list.
//------------------------------------------------------------
//bind function
//With the bind() method, an object can borrow a method from another object.
// let myDetails = printFullName.bind(user1, 'Jankipuram', 'Lucknow', 'Uttar Pradesh', 'India');
// myDetails();
//------------------------------------------------------------