-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14.js
154 lines (130 loc) · 5.33 KB
/
14.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function refererenceCopying() {
// start with strings, numbers and booleans
console.log(
"%c ** Numbers **",
"font-size:20px; color:red; background:black"
);
let age = 100;
let ageCopy = age;
console.log("age : " + age, "\nageCopy : " + ageCopy);
console.log("Updating by : age = 200");
age = 200;
console.log("age : " + age, "\nageCopy : " + ageCopy);
console.log(
"%c ** Strings **",
"font-size:20px; color:red; background:black"
);
let name = "karna98";
let nameCopy = name;
console.log("name : " + name, "\nnameCopy : " + nameCopy);
console.log("Updating by : name = 'Vedant'");
name = "Vedant";
console.log("name : " + name, "\nnameCopy : " + nameCopy);
console.log(
"%c ** Arrays **",
"font-size:20px; color:red; background:black"
);
console.log("Let's say we have an array");
const array = ["Wes", "Sarah", "Ryan", "Poppy"];
console.log("-> array : " + array);
console.log("and we want to make a copy of it by using '='");
const arrayCopy = array;
console.log("-> arrayCopy : " + arrayCopy);
console.log("Updating by : arrayCopy[3] = Dave");
// You might think we can just do something like this:
arrayCopy[3] = "Dave";
// however what happens when we update that array?
console.log("-> array : " + array, "\n-> arrayCopy : " + arrayCopy);
console.log(
"now here is the problem! we have edited the original array too! Why? It's because that is an array reference, not an array copy. They both point to the same array! So, how do we fix this? We take a copy instead!"
);
console.log("Creating Copy By : ");
// 'one way'
console.log("1) array.slice()");
var arrayCopyNew = array.slice();
// or create a new array and concat the old one in
console.log("2) [].concat(array)");
arrayCopyNew = [].concat(array);
// or use the new ES6 Spread
console.log("3) [...array]");
arrayCopyNew = [...array];
// now when we update it, the original one isn't changed
console.log("Updating by : arrayCopyNew[3] = NameChange");
arrayCopyNew[3] = "NameChange";
console.log("-> array : " + array, "\n-> arrayCopyNew : " + arrayCopyNew);
console.log(
"%c ** Objects **",
"font-size:20px; color:red; background:black"
);
// The same thing goes for objects, let's say we have a person object
// with Objects
const objectPerson = {
name: "Vedant",
age: 22,
};
console.log("objectPerson :");
console.table(objectPerson);
// and think we make a copy:
console.log("We create a copy by : objectPersonCopy = objectPerson");
const objectPersonCopy = objectPerson;
console.log("Updating by : objectPersonCopy.name = 'karna98'");
objectPersonCopy.name = "karna98";
console.log("objectPerson :");
console.table(objectPerson);
console.log("objectPersonCopy :");
console.table(objectPersonCopy);
// how do we take a copy instead?
console.log(
'How do we take a copy instead? \n So we assigned by using : objectPersonCopyNew = Object.assign({}, objectPerson, { name: "HungryGamer" })'
);
const objectPersonCopyNew = Object.assign({}, objectPerson, {
name: "HungryGamer",
});
console.log("objectPerson :");
console.table(objectPerson);
console.log("objectPersonCopyNew :");
console.table(objectPersonCopyNew);
// We will hopefully soon see the object ...spread
console.log(
"Creating copy of object using spread : objectPersonCopyNew1 = { ...objectPerson }"
);
const objectPersonCopyNew1 = { ...objectPerson };
console.table(objectPersonCopyNew1);
// 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.
console.log("\n Getting deeper into Array and Objects");
const objectMan = {
name: "Vedant",
age: 22,
social: {
github: "karna98",
LinkedIn: "vedantwakalkar",
},
};
console.log("objectMan :");
console.log(objectMan);
console.log(
"Creating copy of objectMan : objectManCopy = Object.assign({}, objectMan)"
);
const objectManCopy = Object.assign({}, objectMan);
console.log(
"Updating by : objectManCopy.age = 21 and objectManCopy.social.github = 'HungryGamer'"
);
setTimeout(function () {
objectManCopy.age = 21;
objectManCopy.social.facebook = "HungryGamer";
}, 5000);
console.log("objectMan :");
console.log(objectMan);
console.log("objectManCopy :");
console.log(objectManCopy);
console.log(
"By using above method of creating a copy , the value still changed due to copy created is valid for level-1 of Object i.e. if we change the 'name' or 'age' in Copyobject it won't change in original one but if we change the level-2 elements i.e. 'social.github' or 'social.LinkedIn' in CopyObject, the changes will reflect in OriginalObject as well"
);
console.log(
"Creating copy of objectMan : objectManCopyNew = JSON.parse(JSON.stringify(objectMan))"
);
const objectManCopyNew = JSON.parse(JSON.stringify(objectMan));
console.log("objectManCopyNew :");
console.log(objectManCopyNew);
}
window.addEventListener("DOMContentLoaded", refererenceCopying);