|
| 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); |
| 14 | + // age = 200; |
| 15 | + // console.log(age, age2); |
| 16 | + |
| 17 | + // let name = 'Wes'; |
| 18 | + // let name2 = name; |
| 19 | + // console.log(name, name2); |
| 20 | + // name = 'wesley'; |
| 21 | + // console.log(name, name2); |
| 22 | + |
| 23 | + // Let's say we have an array |
| 24 | + const players = ['Wes', 'Sarah', 'Ryan', 'Poppy']; |
| 25 | + |
| 26 | + // and we want to make a copy of it. |
| 27 | + const team = players; |
| 28 | + |
| 29 | + console.log(players, team); |
| 30 | + // You might think we can just do something like this: |
| 31 | + // team[3] = 'Lux'; |
| 32 | + |
| 33 | + // however what happens when we update that array? |
| 34 | + |
| 35 | + // now here is the problem! |
| 36 | + |
| 37 | + // oh no - we have edited the original array too! |
| 38 | + |
| 39 | + // Why? It's because that is an array reference, not an array copy. They both point to the same array! |
| 40 | + |
| 41 | + // So, how do we fix this? We take a copy instead! |
| 42 | + const team2 = players.slice(); |
| 43 | + |
| 44 | + // one day |
| 45 | + |
| 46 | + // or create a new array and concat the old one in |
| 47 | + const team3 = [].concat(players); |
| 48 | + |
| 49 | + // or use the new ES6 Spread |
| 50 | + const team4 = [...players]; |
| 51 | + team4[3] = 'heeee hawww'; |
| 52 | + console.log(team4); |
| 53 | + |
| 54 | + const team5 = Array.from(players) |
| 55 | + |
| 56 | + // now when we update it, the original one isn't changed |
| 57 | + |
| 58 | + // The same thing goes for objects, let's say we have a person object |
| 59 | + |
| 60 | + // with Objects |
| 61 | + const person = { |
| 62 | + name: 'Wes Bos', |
| 63 | + age: 80 |
| 64 | + }; |
| 65 | + |
| 66 | + // and think we make a copy: |
| 67 | + // const captain = person; |
| 68 | + // captain.number = 99; |
| 69 | + |
| 70 | + // how do we take a copy instead? |
| 71 | + const cap2 = Object.assign({}, person, { number: 99, age: 12 }); |
| 72 | + console.log(cap2); |
| 73 | + |
| 74 | + // We will hopefully soon see the object ...spread |
| 75 | + // const cap3 = {...person}; |
| 76 | + |
| 77 | + // 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. |
| 78 | + |
| 79 | + const wes = { |
| 80 | + name: 'Wes', |
| 81 | + age: 100, |
| 82 | + social: { |
| 83 | + twitter: '@wesbos', |
| 84 | + facebook: 'wesbos.developer' |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + console.clear(); |
| 89 | + console.log(wes); |
| 90 | + |
| 91 | + const dev = Object.assign({}, wes); |
| 92 | + |
| 93 | + const dev2 = JSON.parse(JSON.stringify(wes)); |
| 94 | + |
| 95 | + |
| 96 | + </script> |
| 97 | + |
| 98 | +</body> |
| 99 | +</html> |
0 commit comments