|
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 = 'Josh'; |
| 18 | + // let name2 = name; |
| 19 | + // console.log(name, name2); |
| 20 | + // name = 'Joshua'; |
| 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.
|
16 |
| - |
17 | 27 | // You might think we can just do something like this:
|
18 |
| - |
| 28 | + const team = players; |
| 29 | + console.log(players, team); |
19 | 30 | // however what happens when we update that array?
|
20 |
| - |
| 31 | + team[3] = 'Lux'; |
21 | 32 | // now here is the problem!
|
22 |
| - |
| 33 | + console.log(players, team); |
23 | 34 | // oh no - we have edited the original array too!
|
24 |
| - |
25 | 35 | // Why? It's because that is an array reference, not an array copy. They both point to the same array!
|
26 |
| - |
27 | 36 | // So, how do we fix this? We take a copy instead!
|
28 |
| - |
29 | 37 | // one way
|
30 |
| - |
| 38 | + const team2 = players.slice(); |
31 | 39 | // or create a new array and concat the old one in
|
32 |
| - |
| 40 | + const team3 = [].concat(players); |
33 | 41 | // or use the new ES6 Spread
|
34 |
| - |
| 42 | + const team4 = [...players]; |
| 43 | + team4[3] = 'Amended'; |
| 44 | + //or use array from |
| 45 | + const team5 = Array.from(players) |
35 | 46 | // now when we update it, the original one isn't changed
|
36 |
| - |
| 47 | + console.log(players, team2, team3, team4); |
37 | 48 | // The same thing goes for objects, let's say we have a person object
|
38 | 49 |
|
39 | 50 | // with Objects
|
|
43 | 54 | };
|
44 | 55 |
|
45 | 56 | // and think we make a copy:
|
46 |
| - |
| 57 | + const captain = person; |
| 58 | + captain.number = 99; |
47 | 59 | // how do we take a copy instead?
|
48 |
| - |
| 60 | + const captain2 = Object.assign({}, person, {number: 99, age: 12}); |
| 61 | + console.log(captain2); |
49 | 62 | // We will hopefully soon see the object ...spread
|
50 |
| - |
| 63 | + //const captain3 = {...person}; |
51 | 64 | // 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.
|
52 |
| - |
| 65 | + const josh = { |
| 66 | + name: 'josh', |
| 67 | + age: '25', |
| 68 | + social: { |
| 69 | + twitter: '@developer_josh', |
| 70 | + facebook: 'watson6982' |
| 71 | + } |
| 72 | + } |
| 73 | + console.log(josh); |
| 74 | + |
| 75 | + const dev = Object.assign({}, josh, {name: 'joshua'}); |
| 76 | + dev.social.twitter = '@devhandle'; |
| 77 | + console.log(dev); |
| 78 | + // object will only assign top layer |
| 79 | + // function clone deep will close full object |
| 80 | + // or poor mans deep clone |
| 81 | + const dev2 = JSON.parse(JSON.stringify(josh)); |
| 82 | + dev2.social.twitter = '@handle'; |
| 83 | + |
| 84 | + console.log(dev2) |
53 | 85 | </script>
|
54 | 86 |
|
55 | 87 | </body>
|
|
0 commit comments