|
8 | 8 |
|
9 | 9 | <script> |
10 | 10 | // start with strings, numbers and booleans |
| 11 | + let name = 'Sean'; |
| 12 | + let name2 = 'Wesley'; |
| 13 | + |
| 14 | + console.group('Names'); |
| 15 | + console.log(name, '&', name2); |
| 16 | + name = 'Mike'; |
| 17 | + console.log(name, '&', name2); |
| 18 | + console.groupEnd(); |
11 | 19 |
|
12 | 20 | // Let's say we have an array |
13 | | - const players = ['Wes', 'Sarah', 'Ryan', 'Poppy']; |
| 21 | + const players = ['Poppy', 'Ryan', 'Sarah', 'Sean']; |
14 | 22 |
|
15 | 23 | // and we want to make a copy of it. |
| 24 | + const players2 = players; |
16 | 25 |
|
17 | 26 | // You might think we can just do something like this: |
18 | 27 |
|
19 | 28 | // however what happens when we update that array? |
| 29 | + players[3] = 'Wesley'; |
| 30 | + players2[3] = 'Wesley'; |
| 31 | + console.group('Players 1 & 2'); |
| 32 | + console.table(players); |
| 33 | + console.table(players2); |
| 34 | + console.groupEnd(); |
20 | 35 |
|
21 | 36 | // now here is the problem! |
22 | 37 |
|
|
27 | 42 | // So, how do we fix this? We take a copy instead! |
28 | 43 |
|
29 | 44 | // one way |
| 45 | + const team = players.slice(); |
| 46 | + const team2 = [].concat(players); |
| 47 | + |
| 48 | + console.group('Team 1 & 2'); |
| 49 | + console.table(team); |
| 50 | + console.table(team2); |
| 51 | + console.groupEnd(); |
| 52 | + |
| 53 | + players[3] = 'Xavier'; |
| 54 | + |
| 55 | + console.group('Players & Team'); |
| 56 | + console.table(players); |
| 57 | + console.table(team); |
| 58 | + console.groupEnd(); |
30 | 59 |
|
31 | 60 | // or create a new array and concat the old one in |
32 | 61 |
|
33 | 62 | // or use the new ES6 Spread |
| 63 | + const team3 = [...players]; |
| 64 | + console.log(team3); |
| 65 | + |
| 66 | + const team4 = Array.from(players); |
| 67 | + console.log(team4); |
34 | 68 |
|
35 | 69 | // now when we update it, the original one isn't changed |
36 | 70 |
|
37 | 71 | // The same thing goes for objects, let's say we have a person object |
38 | 72 |
|
39 | 73 | // with Objects |
40 | 74 | const person = { |
41 | | - name: 'Wes Bos', |
42 | | - age: 80 |
| 75 | + name: 'Sean McHenry', |
| 76 | + age: 33 |
43 | 77 | }; |
44 | 78 |
|
45 | 79 | // and think we make a copy: |
| 80 | + // const captain = person; |
| 81 | + // captain.number = 99; |
46 | 82 |
|
47 | 83 | // how do we take a copy instead? |
| 84 | + const cap2 = Object.assign({}, person, { number: 99, age: 12 }); |
| 85 | + console.log(cap2); |
| 86 | + console.log(person); |
48 | 87 |
|
49 | 88 | // We will hopefully soon see the object ...spread |
| 89 | + const cap3 = {...person}; |
50 | 90 |
|
51 | 91 | // Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it. |
| 92 | + const person2 = { |
| 93 | + name: 'Amahl Majack', |
| 94 | + age: 34, |
| 95 | + cats: { |
| 96 | + first_male: 'Sly', |
| 97 | + second_male: 'Louie' |
| 98 | + } |
| 99 | + } |
| 100 | + console.log(person2); |
| 101 | + const human = Object.assign({}, person); |
| 102 | + console.log(human); |
| 103 | + |
| 104 | + // poor man's deep clone: |
| 105 | + const human2 = JSON.parse(JSON.stringify(person2)); |
| 106 | + console.log(human2); |
52 | 107 |
|
53 | 108 | </script> |
54 | 109 |
|
|
0 commit comments