|
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 = 'Wes'; |
18 | | - // let name2 = name; |
19 | | - // console.log(name, name2); |
20 | | - // name = 'wesley'; |
21 | | - // console.log(name, name2); |
| 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 | 22 |
|
23 | 23 | // Let's say we have an array |
24 | 24 | const players = ['Wes', 'Sarah', 'Ryan', 'Poppy']; |
25 | 25 |
|
26 | 26 | // and we want to make a copy of it. |
27 | 27 | const team = players; |
28 | | - |
29 | 28 | console.log(players, team); |
| 29 | + |
30 | 30 | // You might think we can just do something like this: |
31 | | - // team[3] = 'Lux'; |
| 31 | +// team[3] = 'Lux'; |
32 | 32 |
|
33 | 33 | // however what happens when we update that array? |
34 | 34 |
|
|
48 | 48 |
|
49 | 49 | // or use the new ES6 Spread |
50 | 50 | const team4 = [...players]; |
51 | | - team4[3] = 'heeee hawww'; |
52 | | - console.log(team4); |
53 | 51 |
|
54 | 52 | const team5 = Array.from(players); |
55 | 53 |
|
|
59 | 57 |
|
60 | 58 | // with Objects |
61 | 59 | const person = { |
62 | | - name: 'Wes Bos', |
63 | | - age: 80 |
| 60 | + name: 'Wes Bos', |
| 61 | + age: 80 |
64 | 62 | }; |
65 | 63 |
|
66 | 64 | // and think we make a copy: |
67 | | - // const captain = person; |
68 | | - // captain.number = 99; |
| 65 | + const captain = person; |
| 66 | +// captain.number = 99; |
69 | 67 |
|
70 | 68 | // how do we take a copy instead? |
71 | | - const cap2 = Object.assign({}, person, { number: 99, age: 12 }); |
72 | | - console.log(cap2); |
| 69 | + const captain2 = Object.assign({}, person, {number: 99}); |
73 | 70 |
|
74 | 71 | // We will hopefully soon see the object ...spread |
75 | | - // const cap3 = {...person}; |
| 72 | +// const captain3 = {...captain}; |
76 | 73 |
|
77 | 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. |
78 | | - |
79 | 75 | const wes = { |
80 | | - name: 'Wes', |
81 | | - age: 100, |
82 | | - social: { |
83 | | - twitter: '@wesbos', |
84 | | - facebook: 'wesbos.developer' |
85 | | - } |
| 76 | + name: 'Wes', |
| 77 | + age: 100, |
| 78 | + social: { |
| 79 | + twitter: '', |
| 80 | + facebook: '' |
| 81 | + } |
86 | 82 | }; |
87 | | - |
88 | 83 | console.clear(); |
89 | 84 | console.log(wes); |
90 | | - |
91 | 85 | const dev = Object.assign({}, wes); |
92 | | - |
93 | | - const dev2 = JSON.parse(JSON.stringify(wes)); |
94 | | - |
95 | | - |
96 | 86 | </script> |
97 | 87 |
|
98 | 88 | </body> |
|
0 commit comments