-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
66 lines (56 loc) · 1.65 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
(function(context) {
function printAddress(address) {
const {
street,
locality,
city,
state } = address;
console.log(`${street}, ${locality}, ${city}, ${state}`);
}
function printName(name) {
const { title, firstName, lastName } = name;
console.log(`${title} ${firstName} ${lastName}`);
}
// destruct received object param
function printNameDestructParams({ title, firstName, lastName }) {
console.log(`${title} ${firstName} ${lastName}`);
}
function demo() {
console.log('\n\nDESTRUCTURING');
const employee = {
id: '1234',
name: {
title: 'Dr.',
firstName: 'Strange',
lastName: ''
},
address: {
street: '890',
locality: 'Fifth Avenue',
city: 'Manhattan',
state: 'New York',
}
};
const { id, name, address: addr } = employee;
console.log(id);
printName(name);
printNameDestructParams(name);
printAddress(addr);
// array destructuring
const arr = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
// destructs arr[0], arr[1], arr[2] and rest of items into
// mon tue wed and restOfNumbers respectively
const [mon, tue, wed, ...restOfWeek] = arr;
console.log(mon);
console.log(tue);
console.log(wed);
console.log(restOfWeek);
// destructs [0], [1] and rest of items into
// first second and restOfNumbers respectively
const [first, second, ...restOfNumbers] = [1, 2, 3, 4, 5];
console.log(first);
console.log(second);
console.log(restOfNumbers);
};
(context || this).demoLibs['destruct'] = demo;
})(window);