|
8 | 8 |
|
9 | 9 | <script>
|
10 | 10 | // start with strings, numbers and booleans
|
| 11 | + let age = 100; |
| 12 | + let age2 = age; |
| 13 | + console.log(age, age2) |
| 14 | + age = 200; |
| 15 | + console.log(age, age2) |
| 16 | + |
| 17 | + let name = 'Wes'; |
| 18 | + let name2 = name; |
| 19 | + console.log(name, name); |
| 20 | + name = 'Wesley'; |
| 21 | + console.log(name, name2) |
11 | 22 |
|
12 | 23 | // Let's say we have an array
|
13 | 24 | const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
|
14 | 25 |
|
15 | 26 | // and we want to make a copy of it.
|
| 27 | + const team = players |
| 28 | + |
| 29 | + console.log(players, team); |
16 | 30 |
|
17 | 31 | // You might think we can just do something like this:
|
| 32 | + // team[3] = 'Lux'; |
18 | 33 |
|
19 | 34 | // however what happens when we update that array?
|
| 35 | + // team is just the reference to the original array. |
| 36 | + // So when you edit that original array it's aloways going to go back to the reference |
| 37 | + // If you update an array it will always reference back! |
20 | 38 |
|
21 | 39 | // now here is the problem!
|
22 | 40 |
|
|
26 | 44 |
|
27 | 45 | // So, how do we fix this? We take a copy instead!
|
28 | 46 |
|
| 47 | + const team2 = players.slice(); |
| 48 | + |
| 49 | + |
| 50 | + // If you pass slice nothing it will take a copy of the original |
| 51 | + |
29 | 52 | // one way
|
30 | 53 |
|
31 | 54 | // or create a new array and concat the old one in
|
| 55 | + const team3 = [].concat(players); |
32 | 56 |
|
33 | 57 | // or use the new ES6 Spread
|
| 58 | + const team4 = [...players]; |
| 59 | + team4[3] = 'hee haw' |
| 60 | + console.log(team4); |
| 61 | + |
| 62 | + const team5 = Array.from(players) |
34 | 63 |
|
35 | 64 | // now when we update it, the original one isn't changed
|
36 | 65 |
|
|
43 | 72 | };
|
44 | 73 |
|
45 | 74 | // and think we make a copy:
|
| 75 | + const captain = person; |
| 76 | + // captain.number = 99; |
46 | 77 |
|
47 | 78 | // how do we take a copy instead?
|
| 79 | + const capt2 = Object.assign({}, person, { number: 99, age: 12 }); |
| 80 | + console.log(capt2); |
| 81 | + // Here we take a blank object and fold in all the properties from the other object |
| 82 | + // And fold in our own properties |
48 | 83 |
|
49 | 84 | // We will hopefully soon see the object ...spread
|
| 85 | + // const cap3 = {...person}; |
50 | 86 |
|
51 | 87 | // 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.
|
| 88 | + const wes = { |
| 89 | + name: 'Wes', |
| 90 | + age: 100, |
| 91 | + social: { |
| 92 | + twitter: '@wesbos', |
| 93 | + facebook: 'wesbos.developer' |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + console.clear(); |
| 98 | + console.log(wes); |
| 99 | + |
| 100 | + const dev = Object.assign({}, wes); |
| 101 | + |
| 102 | + const dev2 = JSON.parse(JSON.stringify(wes)); |
| 103 | + // The poor mans deepclone |
| 104 | + |
52 | 105 |
|
53 | 106 | </script>
|
54 | 107 |
|
|
0 commit comments