|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>JS Reference VS Copy</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | + <script> |
| 10 | + // start with strings, numbers and booleans |
| 11 | + let age = 100; |
| 12 | + let age2 = age; |
| 13 | + console.log(age, age2); // 100 100 |
| 14 | + age = 200; |
| 15 | + console.log(age, age2); // 200 100 |
| 16 | + |
| 17 | + // Let's say we have an array |
| 18 | + const players = ['Wes', 'Sarah', 'Ryan', 'Poppy']; |
| 19 | + |
| 20 | + // and we want to make a copy of it. |
| 21 | + const team = players; // team is just a reference to the players array |
| 22 | + |
| 23 | + console.log(players, team); |
| 24 | + // You might think we can just do something like this: |
| 25 | + team[3] = 'Lux'; // so this will update the original array: players |
| 26 | + |
| 27 | + // however what happens when we update that array? |
| 28 | + |
| 29 | + // now here is the problem! |
| 30 | + |
| 31 | + // oh no - we have edited the original array too! |
| 32 | + |
| 33 | + // Why? It's because that is an array reference, not an array copy. They both point to the same array! |
| 34 | + |
| 35 | + // So, how do we fix this? We take a copy instead! |
| 36 | + const team2 = players.slice(); |
| 37 | + |
| 38 | + // one way |
| 39 | + |
| 40 | + // or create a new array and concat the old one in |
| 41 | + const team3 = [].concat(players); |
| 42 | + |
| 43 | + // or use the new ES6 Spread |
| 44 | + const team4 = [...players]; |
| 45 | + team4[3] = 'heeeee haaa'; |
| 46 | + console.log(team4); |
| 47 | + |
| 48 | + // another good way to copy an array |
| 49 | + const team5 = Array.from(players); |
| 50 | + // now when we update it, the original one isn't changed |
| 51 | + |
| 52 | + // The same thing goes for objects, let's say we have a person object |
| 53 | + |
| 54 | + // with Objects |
| 55 | + const person = { |
| 56 | + name: 'Wes Bos', |
| 57 | + age: 80 |
| 58 | + }; |
| 59 | + |
| 60 | + // and think we make a copy: |
| 61 | + const captain = person; |
| 62 | + captain.number = 99; |
| 63 | + |
| 64 | + // how do we take a copy instead? |
| 65 | + const cap2 = Object.assign({}, person, { number: 99, age: 12 }); |
| 66 | + // we take a blank object {}, overwrite the property with person, then use the thrid argument to fold in our own ones. |
| 67 | + console.log(cap2); |
| 68 | + |
| 69 | + // We will hopefully soon see the object ...spread |
| 70 | + // const cap3 = {...person}; |
| 71 | + // use an object spread, that works exactly the same way as we spread into an array. |
| 72 | + |
| 73 | + |
| 74 | + // 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. |
| 75 | + const sch = { |
| 76 | + name: 'Steve', |
| 77 | + age: 100, |
| 78 | + social: { |
| 79 | + twitter: '@steve', |
| 80 | + facebook: 'steve.chang' |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + console.clear(); |
| 85 | + console.log(sch) |
| 86 | + |
| 87 | + const dev = Object.assign({}, sch) |
| 88 | + |
| 89 | + // not |
| 90 | + dev.social.twitter = 'color'; |
| 91 | + |
| 92 | + const dev2 = JSON.parse(JSON.stringify(sch)); |
| 93 | + </script> |
| 94 | + |
| 95 | +</body> |
| 96 | +</html> |
0 commit comments