-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrow.js
92 lines (69 loc) · 2.35 KB
/
arrow.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
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
90
91
92
let add = function(a, b) {
return a + b;
};
add(1, 2);
//ES6
add = (a, b) => {
return a + b;
};
//whenever we have a single JS expression in the function, the fat arrow can even become shorter
add = (a, b) => a + b;
let double = function(number) {
return 2 * number;
};
//refactor
double = (number) => 2 * number;
//when we have a single argument, the fat arrow function can get even more compact
double = number => 2 * number;
//when there are no argz, you still have to put the parentheses
double = () => {
return 2;
};
let numbers = [1, 2, 3];
numbers.map(function(number) {
return 2 * number;
});
//refactor
numbers.map(number => 2 * number);
let team = {
members: ['Jane', 'Bill'],
teamName: 'Super Squad',
teamSummary: function() {
return this.members.map(function(member) {
return `${member} is on team ${this.teamName}`; //whenever we have a function passed off to somewhere else in our code base, the value of 'this' gets lost. Hence this.teamName is not pointing to team.teamName
}.bind(this)); //to solve that lost 'this' issue, we use the bind helper. It binds the value to the current context
}
};
console.log(team.teamSummary());
team = {
members: ['Jane', 'Bill'],
teamName: 'Super Squad',
teamSummary: function() {
return this.members.map(function(member) {
return `${member} is on team ${this.teamName}`; //whenever we have a function passed off to somewhere else in our code base, the value of 'this' gets lost. Hence this.teamName is not pointing to team.teamName
}, this); // another workaround is by passing this as a second parameter of the map function.
}
};
team = {
members: ['Jane', 'Bill'],
teamName: 'Super Squad',
teamSummary: function() {
var self = this; //this is another workaround for the lost issue
return this.members.map(function(member) {
return `${member} is on team ${self.teamName}`;
});
}
};
console.log(team.teamSummary());
//refactor
team = {
members: ['Jane', 'Bill'],
teamName: 'Super Squad',
teamSummary: function() {
return this.members.map(member => {
return `${member} is on team ${this.teamName}`;
});
}
};
//so in essence, a fat arrow function has no need to bind this, and does not do it.
console.log(team.teamSummary());