Skip to content

Commit f9c599b

Browse files
committed
Added Reference Vs Copy (wesbos#14)
1 parent 1a7a705 commit f9c599b

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed
Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,82 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
4-
<meta charset="UTF-8">
5-
<title>JS Reference VS Copy</title>
5+
<meta charset="UTF-8">
6+
<title>JS Reference VS Copy</title>
67
</head>
8+
79
<body>
10+
<script>
11+
// start with strings, numbers and booleans
12+
let = age = 100;
13+
let age2 = age;
14+
console.log(age, age2);
815

9-
<script>
10-
// start with strings, numbers and booleans
16+
age = 200;
17+
console.log(age, age2);
1118

12-
// Let's say we have an array
13-
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
1419

15-
// and we want to make a copy of it.
20+
// Let's say we have an array
21+
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
1622

17-
// You might think we can just do something like this:
23+
// and we want to make a copy of it.
24+
const team = players;
25+
console.log(team, players)
1826

19-
// however what happens when we update that array?
27+
// You might think we can just do something like this:
28+
team[3] = 'Lux';
2029

21-
// now here is the problem!
30+
// however what happens when we update that array?
31+
// now here is the problem!
32+
console.log(team, players)
2233

23-
// oh no - we have edited the original array too!
34+
// oh no - we have edited the original array too!
2435

25-
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
36+
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
2637

27-
// So, how do we fix this? We take a copy instead!
38+
// So, how do we fix this? We take a copy instead!
2839

29-
// one way
40+
// one way
41+
const team2 = players.slice();
3042

31-
// or create a new array and concat the old one in
43+
// or create a new array and concat the old one in
44+
const team3 = [].concat(players);
3245

33-
// or use the new ES6 Spread
46+
// or use the new ES6 Spread
47+
const team4 = [...players];
3448

35-
// now when we update it, the original one isn't changed
49+
const team5 = Array.from(players);
3650

37-
// The same thing goes for objects, let's say we have a person object
51+
// now when we update it, the original one isn't changed
3852

39-
// with Objects
53+
// The same thing goes for objects, let's say we have a person object
4054

41-
// and think we make a copy:
55+
// with Objects
56+
const person = {
57+
name: 'Jeff Dess',
58+
age: 35,
59+
social: {
60+
twitter: '@jeffdess',
61+
github: '@jeffdess'
62+
}
63+
}
4264

43-
// how do we take a copy instead?
65+
// and think we make a copy:
66+
const captain = person;
67+
captain.number = 99;
4468

45-
// We will hopefully soon see the object ...spread
69+
// how do we take a copy instead?
70+
Object.assign({}, person, { number: 99 });
4671

47-
// 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.
72+
// We will hopefully soon see the object ...spread
73+
//const cap3 = {...person};
4874

49-
</script>
75+
// 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.
5076

77+
// Poorman's cloneDeep
78+
const cloneDeep = JSON.parse(JSON.stringify(person));
79+
</script>
5180
</body>
52-
</html>
81+
82+
</html>

0 commit comments

Comments
 (0)