-
Notifications
You must be signed in to change notification settings - Fork 1
/
spread-operator.js
65 lines (50 loc) · 1.2 KB
/
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
/** Spread Operator **/
// Function Definition
// ES5
function foo () {
// arguments is an Array-like object variable
// available within all functions that contains
// the parameters passed to the function.
console.log(arguments)
}
foo(1,2,3) // { '0': 1, '1': 2, '2': 3 }
foo([1,2,3],4) // { '0': [1,2,3], '1': 4 }
// ES6
function foo (...parameters) {
// parameters is an Array
// with all parameters.
console.log(parameters)
}
foo(1,2,3) // [1,2,3]
foo([1,2,3],4) // [[1,2,3],4]
// We can name some parameters
function foo (bar, ...parameters) {
// bar is the first parameter.
// parameters is an Array containing
// the remaining parameters.
console.log(bar, parameters)
}
foo() // undefined, []
foo(1) // 1, []
foo(1,2,3) // 1, [2,3]
// Function Invocation
function foo (a,b,c) {
console.log(a,b,c)
}
// ES5
foo.apply(this, [1,2,3]) // 1, 2, 3
// ES6
foo(...[1,2,3]) // 1, 2, 3
// Concatenation
// ES5
[1].concat([2,3],4) // [1,2,3,4]
// ES6
[1, ...[2,3], 4] // [1,2,3,4]
// Destructuring
// ES5
var foo = [1,2,3,4,5][0] // 1
var rest = [1,2,3,4,5].slice(2,5) // [3,4,5]
// ES6
[foo, , ...rest] = [1,2,3,4,5]
console.log(foo) // 1
console.log(rest) // [3,4,5]