Skip to content

Commit 86fff5e

Browse files
committed
Finished wesbos#14
1 parent b69e175 commit 86fff5e

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

14 - JavaScript References VS Copying/index-START.html

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,30 @@
88

99
<script>
1010
// start with strings, numbers and booleans
11+
let name = 'Sean';
12+
let name2 = 'Wesley';
13+
14+
console.group('Names');
15+
console.log(name, '&', name2);
16+
name = 'Mike';
17+
console.log(name, '&', name2);
18+
console.groupEnd();
1119

1220
// Let's say we have an array
13-
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
21+
const players = ['Poppy', 'Ryan', 'Sarah', 'Sean'];
1422

1523
// and we want to make a copy of it.
24+
const players2 = players;
1625

1726
// You might think we can just do something like this:
1827

1928
// however what happens when we update that array?
29+
players[3] = 'Wesley';
30+
players2[3] = 'Wesley';
31+
console.group('Players 1 & 2');
32+
console.table(players);
33+
console.table(players2);
34+
console.groupEnd();
2035

2136
// now here is the problem!
2237

@@ -27,28 +42,68 @@
2742
// So, how do we fix this? We take a copy instead!
2843

2944
// one way
45+
const team = players.slice();
46+
const team2 = [].concat(players);
47+
48+
console.group('Team 1 & 2');
49+
console.table(team);
50+
console.table(team2);
51+
console.groupEnd();
52+
53+
players[3] = 'Xavier';
54+
55+
console.group('Players & Team');
56+
console.table(players);
57+
console.table(team);
58+
console.groupEnd();
3059

3160
// or create a new array and concat the old one in
3261

3362
// or use the new ES6 Spread
63+
const team3 = [...players];
64+
console.log(team3);
65+
66+
const team4 = Array.from(players);
67+
console.log(team4);
3468

3569
// now when we update it, the original one isn't changed
3670

3771
// The same thing goes for objects, let's say we have a person object
3872

3973
// with Objects
4074
const person = {
41-
name: 'Wes Bos',
42-
age: 80
75+
name: 'Sean McHenry',
76+
age: 33
4377
};
4478

4579
// and think we make a copy:
80+
// const captain = person;
81+
// captain.number = 99;
4682

4783
// how do we take a copy instead?
84+
const cap2 = Object.assign({}, person, { number: 99, age: 12 });
85+
console.log(cap2);
86+
console.log(person);
4887

4988
// We will hopefully soon see the object ...spread
89+
const cap3 = {...person};
5090

5191
// 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.
92+
const person2 = {
93+
name: 'Amahl Majack',
94+
age: 34,
95+
cats: {
96+
first_male: 'Sly',
97+
second_male: 'Louie'
98+
}
99+
}
100+
console.log(person2);
101+
const human = Object.assign({}, person);
102+
console.log(human);
103+
104+
// poor man's deep clone:
105+
const human2 = JSON.parse(JSON.stringify(person2));
106+
console.log(human2);
52107

53108
</script>
54109

0 commit comments

Comments
 (0)