Skip to content

Commit f4e09b9

Browse files
committed
day 16
1 parent e29dfa4 commit f4e09b9

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

each day build day!/Day 16/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Js fundamentals reference vs copy
2+
3+
The most common pitfall in js is probably due to not understanding the difference between primitive types and objects.
4+
5+
TL:DR; arrays and objects are referenced, to make copies use either of the methods `slice`,`concat`,`[...]`,`Assign`,`{...}`

each day build day!/Day 16/index.html

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Fundamentals Reference vs Copy</title>
8+
</head>
9+
10+
<body>
11+
<center>
12+
<h1> Js Fundamentals : Reference vs Copy</h1>
13+
</center>
14+
<script>
15+
// start with strings, numbers and booleans
16+
17+
// Let's say we have an array
18+
const players = ['Mario', 'Luigi', 'Deadpool', 'Poopman'];
19+
20+
// and we want to make a copy of it.
21+
22+
23+
// You might think we can just do something like this:
24+
25+
// however what happens when we update that array?
26+
27+
// oh no - we have edited the original array too!
28+
29+
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
30+
31+
// So, how do we fix this? We take a copy instead!
32+
33+
// one way
34+
const players2 = players.slice();
35+
// or create a new array and concat the old one in
36+
const player3 = [].concat(players);
37+
// or use the new ES6 Spread
38+
const player4 = [...players];
39+
// now when we update it, the original one isn't changed
40+
const players5 = Array.from(players);
41+
// The same thing goes for objects, let's say we have a person object
42+
players4[3] = "lol";
43+
console.log("copy", players4, "original :", players);
44+
// with Objects
45+
const person = {
46+
name: 'Mario Bros',
47+
age: 40
48+
};
49+
50+
// and think we make a copy:
51+
52+
// how do we take a copy instead?
53+
const shallowCopy = Object.assign({}, person, { number: 11, age: 23 })
54+
console.log("original :", person, " updated :", shallowCopy);
55+
56+
// We will hopefully soon see the object ...spread
57+
const copyOfObject = { ...person }
58+
// 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.
59+
60+
const nestedObject = {
61+
name: 'yuan',
62+
age: 10,
63+
social: {
64+
twitter: '@yuan',
65+
facebook: 'yuan.player'
66+
}
67+
};
68+
69+
console.clear();
70+
console.log(nestedObject);
71+
// cheap trick
72+
const dev = Object.assign({}, nestedObject);
73+
74+
const dev2 = JSON.parse(JSON.stringify(nestedObject));
75+
console.table(dev2);
76+
77+
</script>
78+
79+
80+
</body>
81+
82+
</html>

0 commit comments

Comments
 (0)