-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrow_functions.js
143 lines (116 loc) · 2.51 KB
/
arrow_functions.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//////////////////////////
// ES6 - Arrow Functions
//////////////////////////
/*
// ES5
const add1 = function(a, b) {
return a + b;
}
console.log(add1(1, 2));
// ES6 Option 1
const add2 = (a, b) => {
return a + b;
}
console.log(add2(1, 2));
// ES6 Option 2
const add3 = (a, b) => a + b;
console.log(add3(1, 2));
*/
// ===============================================================
/*
// ES5 - with normal function syntax
const double1 = function(number) {
return 2 * number;
};
console.log(double1(8));
// ES6 with fat arrow function syntax
const double2 = (number) => {
return 2 * number;
};
console.log(double2(8))
//////////////////////
const double3 = (number) => {
return 2 * number;
};
console.log(double3(8))
//////////////////////
const double4 = number => 2 * number;
console.log(double4(8));
//////////////////////
const double5 = (number1, number2) => {
return 2 * number1 + 2 * number2;
}
console.log(double5(8,8));
//////////////////////
const double6 = () => {
return 2;
}
console.log(double6());
*/
//////////////////////
/*
const numbers = [1,2,3];
// ES5
numbers.map(function(number) {
return 2 * number;
})
// ES6
console.log(numbers.map(number => 2 * number));
*/
///////////////////////////
// ES5 using bind function
/*
const team1 = {
members: ['Jane', 'Bill'],
teamName: 'Super Squad',
teamSummary: function() {
return this.members.map(function(member) {
return `${member} is on team ${this.teamName}`
}.bind(this));
}
};
console.log(team1.teamSummary());
// ES6 arrow function (using lexical this)
const team2 = {
members: ['Jane', 'Bill'],
teamName: 'Super Squad',
teamSummary: function() {
return this.members.map(member => {
return `${member} is on team ${this.teamName}`
});
}
};
console.log(team2.teamSummary());
*/
//////////////////////////////////////
// Refactoring Keyword Functions
/*
// ES5
const fibonacci1 = function(n) {
if (n < 3) return 1;
return fibonacci1(n - 1) + fibonacci1(n - 2);
}
// ES6 with fat arrow function
const fibonacci2 = n => {
if (n < 3) return 1;
return fibonacci2(n - 1) + fibonacci2(n - 2);
}
*/
////////////////////////////////////
// Arrow Functions Aren't Always a Solution
// ES5
const profile1 = {
name: 'Alex',
getName: function() {
return this.name;
}.bind(this)
};
console.log(profile1.getName());
// ES6
const profile2 = {
name: 'Alex',
getName() {
return this.name;
}
};
console.log(profile2.getName());