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