You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 14 - JavaScript References VS Copying/index-START.html
+37Lines changed: 37 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,28 @@
8
8
9
9
<script>
10
10
// start with strings, numbers and booleans
11
+
letage=100;
12
+
letage2=age;
13
+
console.log(age,age2);
14
+
age=200;
15
+
console.log(age);
16
+
17
+
letname='Caden';
18
+
letname2=name;
19
+
console.log(name,name2);
20
+
name='Caden Albaugh';
21
+
console.log(name,name2);
11
22
12
23
// Let's say we have an array
13
24
constplayers=['Wes','Sarah','Ryan','Poppy'];
14
25
15
26
// and we want to make a copy of it.
27
+
constteam=players;
28
+
29
+
console.log(team,players);
16
30
17
31
// You might think we can just do something like this:
32
+
team[3]='lux';
18
33
19
34
// however what happens when we update that array?
20
35
@@ -25,27 +40,49 @@
25
40
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
26
41
27
42
// So, how do we fix this? We take a copy instead!
43
+
constteam2=players.slice();
28
44
29
45
// one day
30
46
31
47
// or create a new array and concat the old one in
48
+
constteam3=[].concat(players);
32
49
33
50
// or use the new ES6 Spread
51
+
constteam4=[...players];
52
+
53
+
constteam5=Array.from(players);
34
54
35
55
// now when we update it, the original one isn't changed
36
56
37
57
// The same thing goes for objects, let's say we have a person object
38
58
39
59
// with Objects
60
+
constperson={
61
+
name: 'Wes Bos',
62
+
age: 80
63
+
};
40
64
41
65
// and think we make a copy:
66
+
constcaptain=person;
67
+
captain.number=99;
42
68
43
69
// how do we take a copy instead?
70
+
constcapt2=Object.assign({},person,{number: 99});
44
71
45
72
// We will hopefully soon see the object ...spread
73
+
capt3={...person};// This is shallow, meaning it will only go one level deep
46
74
47
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.
Copy file name to clipboardExpand all lines: 17 - Sort Without Articles/index-START.html
+10Lines changed: 10 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,16 @@
45
45
<script>
46
46
constbands=['The Plot in You','The Devil Wears Prada','Pierce the Veil','Norma Jean','The Bled','Say Anything','The Midway State','We Came as Romans','Counterparts','Oh, Sleeper','A Skylit Drive','Anywhere But Here','An Old Dog'];
0 commit comments