-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallApplyBindMethods.js
More file actions
69 lines (60 loc) · 1.53 KB
/
Copy pathcallApplyBindMethods.js
File metadata and controls
69 lines (60 loc) · 1.53 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
/**
* All three of these JavaScript methods allow you to change the value of this for a given function.
*
* The practical use of these methods in the scenarios like function borrowing and function currying.
*
*/
/**
* Example 1:- Syntax and the usage of the methods
*
* In this example we will show you the synatx and the usage of these 3 functions
* The object (person) and its method can be attached to a particular function.
* After attaching we can use them in the function given.This will help us to reuse the code.
*
*/
var person = {
firstName : 'john',
lastName : 'Doe',
getFullName : function(){
var fullName = this.firstName + " " + this.lastName;
return fullName;
}
}
var logName = function(lang1, lang2){
console.log('Logged:' + this.getFullName());
console.log('Arguments:' + lang1 + ' ' + lang2);
console.log('-------------');
}
// Bind
var logPersonName = logName.bind(person);
logPersonName('en', 'es');
// Call
logName.call(person, 'en', 'es');
// Apply
logName.apply(person, ['en', 'es']);
/**
* Example 2 - Function borrowing
*/
var user = {
firstName : 'Bob',
lastName : 'Marley',
getFullName : function(){
var fullName = this.firstName + " " + this.lastName;
return fullName;
}
};
var user2 = {
firstName : 'Tarun',
lastName : 'Nagpal',
}
console.log(user.getFullName());
// borrowing
console.log(user.getFullName.apply(user2));
/**
* Example 3 - Function Currying
*/
function multiply(x, y){
return x * y;
}
var multiplyByTwo = multiply.bind(this, 2);
console.log(multiplyByTwo(6));