|
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); //100, 100 |
| 14 | + age = 200; |
| 15 | + console.log(age, age2); //200, 100 |
11 | 16 |
|
12 | 17 | // Let's say we have an array |
13 | 18 | const players = ['Wes', 'Sarah', 'Ryan', 'Poppy']; |
14 | 19 |
|
15 | 20 | // and we want to make a copy of it. |
16 | | - |
| 21 | + const playersCopy1 = [...players]; |
| 22 | + const playersCopy2 = ([]).concat(players); |
| 23 | + const playersCopy3 = players.slice(); |
| 24 | + const playersCopy4 = Array.from(players); |
| 25 | + console.log(players); |
| 26 | + console.log(playersCopy1); |
| 27 | + console.log(playersCopy2); |
| 28 | + console.log(playersCopy3); |
| 29 | + console.log(playersCopy4); |
17 | 30 | // You might think we can just do something like this: |
18 | | - |
| 31 | + |
19 | 32 | // however what happens when we update that array? |
20 | 33 |
|
21 | 34 | // now here is the problem! |
|
43 | 56 | }; |
44 | 57 |
|
45 | 58 | // and think we make a copy: |
46 | | - |
| 59 | + const personCopy1 = {...person} |
| 60 | + const personCopy2 = Object.assign({}, person) |
| 61 | + console.log(person); |
| 62 | + console.log(personCopy1) |
| 63 | + console.log(personCopy2) |
47 | 64 | // how do we take a copy instead? |
48 | 65 |
|
49 | 66 | // We will hopefully soon see the object ...spread |
|
0 commit comments