-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallBindApply.js
More file actions
79 lines (56 loc) · 1.87 KB
/
callBindApply.js
File metadata and controls
79 lines (56 loc) · 1.87 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
/*
this: this refers to the calling context.
This is not completely true. (slippers on my face)
Call Apply and Bind can alter the this.
What is function in JS?
- Functions are nothing but an object.
- It has a lot of properties.
*/
function foo() {
console.log(this);
}
foo(); // window
console.log(foo);
console.log(foo.name); // functions are just objects it has properties
foo.gender = 'Male'; // we can add more properties.
console.log(foo.toString()); // functions which are just objects has properties which are functions as well.
console.log('----------------------------');
// Few importants properties on that functions are call apply and bind.
// Note: this need not be always an object it can be anything even a number 1.
function dragon() {
mother = this.mother;
console.log(mother);
}
dragon(); // undefined
dragon.call({mother: 'danny'}); // You can set the this using call.
dragon.apply({mother: 'Khaleesi'}); // Same as call
myDragon = dragon.bind({mother: 'Cersei'}); // Bind, binds the function with the this during function creation and returns a new function.
dragon(); // undefined
myDragon(); // Gotcha!
/*
Difference between call and apply.
call takes variable number of arguments but whereas apply takes an array.
*/
// Example 1
function sum(a, b, c, d) {
console.log(a + b + c + d);
}
sum(1, 2, 3, 4);
sum.call(null, 1, 2, 3, 4);
sum.apply(null, [1, 2, 3, 4]);
// Example 2
let numbers = [36, 24, 34];
function sum1() {
let sum = 0;
for(let i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
console.log(sum);
}
sum1(numbers); // Doesn't work.
/*
Pre ES6 before spread was available.
Assume you had an array and you would need to find the sum.
This can't be achieve through call because call expects the arguments be be exploaded.
*/
sum1.apply(null, numbers);