-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis-in-array-methods.js
More file actions
43 lines (35 loc) · 1.27 KB
/
this-in-array-methods.js
File metadata and controls
43 lines (35 loc) · 1.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
// this argument in forEach, map, reduce etc
console.log("for Each example:");
[1, 2, 3, 4].forEach(
function () {
// if this was an array function, there is no this binding even if pass the second argument
console.log(this.name); // Samrood
// forEach, map,filter takes in a second argument which is the 'this' binding, but not in arrow functions
},
{ name: "Samrood" }
);
console.log("map example:");
[1, 2, 3, 4].map(
function () {
// if this was an array function, there is no this binding even if pass the second argument
console.log(this.name); // Samrood
// forEach, map ,filter takes in a second argument which is the 'this' binding, but not in arrow functions
},
{ name: "Samrood" }
);
// other apis that takes'this' argument
console.log(
"Array find,findIndex, filter, index all takes in a function and the 'thisArg' ('this' binding)"
);
console.log("This is different in Array.reduce");
console.log("reduce example");
[1, 2, 3, 4].reduce(
function (acc, curr) {
// if this was an array function, there is no this binding even if pass the second argument
console.log(this.name); // undefined
return acc;
// reduce doesn't have 'this' argument
},
// acc
{ name: "this is the accumulator and not the bound 'this'" }
);