-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_and_spread_operator.js
126 lines (99 loc) · 2.72 KB
/
rest_and_spread_operator.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
//////////////////////////////////
// ES6 - Rest and Spread Operator
//////////////////////////////////
/*
function addNumbers(...numbers) {
return numbers.reduce((sum, number) => {
return sum + number;
}, 0);
}
console.log(addNumbers(1,2,3,4,5,6,7,8,8,9,55));
*/
// =======================================
/*
const defaultColors = ['red', 'green'];
const userFavoriteColors = ['orange', 'yellow'];
const fallColors = ['fire red', 'fall orange' ];
// ES5
console.log(defaultColors.concat(userFavoriteColors));
// => [ 'red', 'green', 'orange', 'yellow' ]
// ES6 - spread operator
console.log([ ...defaultColors, ...userFavoriteColors ]);
// => [ 'red', 'green', 'orange', 'yellow' ]
console.log([ ...defaultColors, userFavoriteColors ]);
// => [ 'red', 'green', [ 'orange', 'yellow' ] ]
console.log([ defaultColors, userFavoriteColors ]);
// => [ [ 'red', 'green' ], [ 'orange', 'yellow' ] ]
console.log([ ...fallColors, ...defaultColors, ...userFavoriteColors ]);
console.log([ 'green', 'blue', ...fallColors, ...defaultColors, ...userFavoriteColors ]);
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];
console.log(lyrics);
// => [ 'head', 'shoulders', 'knees', 'and', 'toes' ]
*/
// ===============================================
/*
function validateShoppingList(...items) {
if (items.indexOf('milk') < 0) {
return [ 'milk', ...items ];
}
return items;
}
console.log(validateShoppingList('oranges', 'bread', 'eggs'));
*/
// ===============================================
/*
// ES5
const MathLibrary2 = {
calculateProduct(a, b) {
return a * b;
},
multiply(a, b) {
return a * b;
}
};
// ES6
const MathLibrary1 = {
calculateProduct(...rest) {
console.log('Please use the multiply method instead');
return this.multiply(...rest);
},
multiply(a, b) {
return a * b;
}
};
*/
// ===============================================
// Many, Many Arguments
// ES5
function product(a, b, c, d, e) {
var numbers = [a,b,c,d,e];
return numbers.reduce(function(acc, number) {
return acc * number;
}, 1)
}
// ES6
function product(...numbers) {
return numbers.reduce((acc, number) => acc * number, 1)
}
console.log(product(1,2,3,4,5));
// ==================================
// ES5
function join(array1, array2) {
return array1.concat(array2);
}
// ES6
function join(array1, array2) {
return [ ...array1, ...array2 ];
}
// ===================================
// ES5
function unshift(array, a, b, c, d, e) {
return [a, b, c, d, e].concat(array);
}
// ES6
//pass array and pass rest of the numbers
function unshift(array, ...numbers) {
//spread numbers and spread the array
return [...numbers,...array];
}