Skip to content

Commit 2ce08f9

Browse files
committed
Update reading progress of Day 14
1 parent 6dc1d7f commit 2ce08f9

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Target - Day 14
2+
Introducing the reference of an object and ways to copy an Array.
3+
4+
## Key points
5+
- About reference:
6+
```js
7+
a1 = [1, 2, 3];
8+
a2 = a1.slice();
9+
a3 = a1;
10+
a4 = [].concat(a1);
11+
12+
a1[0] = 2;
13+
14+
console.log(a1); // [2, 2, 3]
15+
console.log(a2); // [1, 2, 3]
16+
17+
// been changed as well
18+
console.log(a3); // [2, 2, 3]
19+
console.log(a4); // [1, 2, 3]
20+
```
21+
22+
- An object can have property which reference to a function.
23+
```js
24+
var arr = ['a', 'b', 'c'];
25+
26+
arr.test = function() { return 'Hi'; };
27+
arr.test(); // 'Hi'
28+
29+
typeof(arr); // 'object'
30+
```
31+
32+
- Create a
33+
34+
35+
## Additional Information
36+
- To compare two arrays in JavaScript
37+
```js
38+
a1 = [1, 2, 3];
39+
a2 = [1, 2, 3];
40+
41+
JSON.stringify(a1) === JSON.stringify(a2); // true
42+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>JS Reference VS Copy</title>
6+
</head>
7+
<body>
8+
9+
<script>
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
16+
17+
// Let's say we have an array
18+
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
19+
20+
// and we want to make a copy of it.
21+
const team = players; // team is just a reference to the players array
22+
23+
console.log(players, team);
24+
// You might think we can just do something like this:
25+
team[3] = 'Lux'; // so this will update the original array: players
26+
27+
// however what happens when we update that array?
28+
29+
// now here is the problem!
30+
31+
// oh no - we have edited the original array too!
32+
33+
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
34+
35+
// So, how do we fix this? We take a copy instead!
36+
const team2 = players.slice();
37+
38+
// one way
39+
40+
// or create a new array and concat the old one in
41+
const team3 = [].concat(players);
42+
43+
// or use the new ES6 Spread
44+
const team4 = [...players];
45+
team4[3] = 'heeeee haaa';
46+
console.log(team4);
47+
48+
// another good way to copy an array
49+
const team5 = Array.from(players);
50+
// now when we update it, the original one isn't changed
51+
52+
// The same thing goes for objects, let's say we have a person object
53+
54+
// with Objects
55+
const person = {
56+
name: 'Wes Bos',
57+
age: 80
58+
};
59+
60+
// and think we make a copy:
61+
const captain = person;
62+
captain.number = 99;
63+
64+
// how do we take a copy instead?
65+
const cap2 = Object.assign({}, person, { number: 99, age: 12 });
66+
// we take a blank object {}, overwrite the property with person, then use the thrid argument to fold in our own ones.
67+
console.log(cap2);
68+
69+
// We will hopefully soon see the object ...spread
70+
// const cap3 = {...person};
71+
// use an object spread, that works exactly the same way as we spread into an array.
72+
73+
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.
75+
const sch = {
76+
name: 'Steve',
77+
age: 100,
78+
social: {
79+
twitter: '@steve',
80+
facebook: 'steve.chang'
81+
}
82+
}
83+
84+
console.clear();
85+
console.log(sch)
86+
87+
const dev = Object.assign({}, sch)
88+
89+
// not
90+
dev.social.twitter = 'color';
91+
92+
const dev2 = JSON.parse(JSON.stringify(sch));
93+
</script>
94+
95+
</body>
96+
</html>

0 commit comments

Comments
 (0)