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: 1-js/04-object-basics/02-object-copy/article.md
+14
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,4 @@
1
+
1
2
# Object references and copying
2
3
3
4
One of the fundamental differences of objects versus primitives is that objects are stored and copied "by reference", as opposed to primitive values: strings, numbers, booleans, etc -- that are always copied "as a whole value".
@@ -8,6 +9,7 @@ Let's start with a primitive, such as a string.
8
9
9
10
Here we put a copy of `message` into `phrase`:
10
11
12
+
11
13
```js
12
14
let message ="Hello!";
13
15
let phrase = message;
@@ -25,12 +27,14 @@ Objects are not like that.
25
27
26
28
Let's look at an example of such variable:
27
29
30
+
28
31
```js
29
32
let user = {
30
33
name:"John"
31
34
};
32
35
```
33
36
37
+
34
38
And here's how it's actually stored in memory:
35
39
36
40

@@ -50,6 +54,7 @@ For instance:
50
54
```js no-beautify
51
55
let user = { name:"John" };
52
56
57
+
53
58
let admin = user; // copy the reference
54
59
```
55
60
@@ -61,6 +66,7 @@ As you can see, there's still one object, now with two variables that reference
61
66
62
67
We can use any variable to access the object and modify its contents:
63
68
69
+
64
70
```js run
65
71
let user = { name:'John' };
66
72
@@ -96,9 +102,11 @@ And here two independent objects are not equal, even though they look alike (bot
96
102
let a = {};
97
103
let b = {}; // two independent objects
98
104
105
+
99
106
alert( a == b ); // false
100
107
```
101
108
109
+
102
110
For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj ==5`, objects are converted to primitives. We'll study how object conversions work very soon, but to tell the truth, such comparisons are needed very rarely, usually they appear as a result of a programming mistake.
103
111
104
112
## Cloning and merging, Object.assign
@@ -120,6 +128,7 @@ let user = {
120
128
};
121
129
122
130
*!*
131
+
123
132
let clone = {}; // the new empty object
124
133
125
134
// let's copy all user properties into it
@@ -128,6 +137,7 @@ for (let key in user) {
128
137
}
129
138
*/!*
130
139
140
+
131
141
// now clone is a fully independent object with the same content
132
142
clone.name="Pete"; // changed the data in it
133
143
@@ -142,6 +152,7 @@ The syntax is:
142
152
Object.assign(dest, [src1, src2, src3...])
143
153
```
144
154
155
+
145
156
- The first argument `dest` is a target object.
146
157
- Further arguments `src1, ..., srcN` (can be as many as needed) are source objects.
147
158
- It copies the properties of all source objects `src1, ..., srcN` into the target `dest`. In other words, properties of all arguments starting from the second are copied into the first object.
@@ -174,6 +185,7 @@ alert(user.name); // now user = { name: "Pete" }
174
185
175
186
We also can use `Object.assign` to replace `for..in` loop for simple cloning:
176
187
188
+
177
189
```js
178
190
let user = {
179
191
name:"John",
@@ -192,6 +204,7 @@ It copies all properties of `user` into the empty object and returns it.
192
204
Until now we assumed that all properties of `user` are primitive. But properties can be references to other objects. What to do with them?
193
205
194
206
Like this:
207
+
195
208
```js run
196
209
let user = {
197
210
name:"John",
@@ -208,6 +221,7 @@ Now it's not enough to copy `clone.sizes = user.sizes`, because the `user.sizes`
0 commit comments