Skip to content

Commit 19aae28

Browse files
committed
JS30 wesbos#14 complete
1 parent 5315d4e commit 19aae28

1 file changed

Lines changed: 75 additions & 32 deletions

File tree

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,99 @@
11
<!DOCTYPE html>
22
<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
810

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);
1116

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);
1422

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'];
1625

17-
// You might think we can just do something like this:
26+
// and we want to make a copy of it.
1827

19-
// however what happens when we update that array?
28+
const team = players;
29+
console.log(players, team);
2030

21-
// now here is the problem!
31+
// You might think we can just do something like this:
2232

23-
// oh no - we have edited the original array too!
33+
team[3] = 'Lux';
34+
console.log(team);
35+
console.log(players);
2436

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?
2638

27-
// So, how do we fix this? We take a copy instead!
39+
// now here is the problem!
2840

29-
// one way
41+
// oh no - we have edited the original array too!
3042

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!
3244

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);
3448

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);
3653

37-
// The same thing goes for objects, let's say we have a person object
54+
// one way
3855

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
4457

45-
// and think we make a copy:
58+
const team3 = [].concat(players);
59+
console.log(team3);
4660

47-
// how do we take a copy instead?
61+
// or use the new ES6 Spread
4862

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);
5067

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);
5271

53-
</script>
72+
// now when we update it, the original one isn't changed
5473

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>
5699
</html>

0 commit comments

Comments
 (0)