-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
80 lines (67 loc) · 1.53 KB
/
index.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
(function(context) {
function basicSpread() {
const name = {
name: {
title: 'Dr.',
firstName: 'Strange',
lastName: '',
}
};
const address = {
address: {
street: '890',
locality: 'Fifth Avenue',
city: 'Manhattan',
state: 'New York',
}
};
// This is object spread
const employee2 = {
...name,
...address,
id: 1234,
};
console.log(employee2);
// This is object literal and not object spread
const employee = {
name,
address,
id: 1234,
};
console.log(employee);
const arr1 = [1, 2, 3, 4];
const arr2 = [10, 20, 30, 40];
// spread
const arrMerged = [...arr1, ...arr2];
console.log(arrMerged);
}
function functionSpread() {
// regular stuff
print('John', 'Doe');
// function spread
const arrName = ['John', 'Doe'];
print(...arrName);
const a = 'John';
const b = 'Doe';
// now this is interesting, guess the output
print(...[...a,...b]);
}
function print(firstName, lastName) {
console.log(`Hey ${firstName} ${lastName}`);
}
function rest() {
const colors = ['red', 'green', 'blue', 'yellow', 'brown'];
// rest
const [ first, second, ...restOfColors ] = colors;
console.log(first);
console.log(second);
console.log(restOfColors);
}
function demo() {
console.log('\n\nSPREAD');
basicSpread();
functionSpread();
rest();
};
(context || this).demoLibs['spread'] = demo;
})(window);