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
+6-10
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ And here's how it's actually stored in memory:
37
37
38
38
The object is stored somewhere in memory (at the right of the picture), while the `user` variable (at the left) has a "reference" to it.
39
39
40
-
We may think of an object variable, such as `user`, as like a sheet of paper with the address of the object on it.
40
+
We may think of an object variable, such as `user`, like a sheet of paper with the address of the object on it.
41
41
42
42
When we perform actions with the object, e.g. take a property `user.name`, the JavaScript engine looks at what's at that address and performs the operation on the actual object.
43
43
@@ -104,11 +104,9 @@ For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj
104
104
105
105
So, copying an object variable creates one more reference to the same object.
106
106
107
-
But what if we need to duplicate an object? Create an independent copy, a clone?
107
+
But what if we need to duplicate an object?
108
108
109
-
That's also doable, but a little bit more difficult, because there's no built-in method for that in JavaScript. But there is rarely a need -- copying by reference is good most of the time.
110
-
111
-
But if we really want that, then we need to create a new object and replicate the structure of the existing one by iterating over its properties and copying them on the primitive level.
109
+
We can create a new object and replicate the structure of the existing one, by iterating over its properties and copying them on the primitive level.
112
110
113
111
Like this:
114
112
@@ -133,7 +131,7 @@ clone.name = "Pete"; // changed the data in it
133
131
alert( user.name ); // still John in the original object
134
132
```
135
133
136
-
Also we can use the method [Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) for that.
134
+
We can also use the method [Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign).
137
135
138
136
The syntax is:
139
137
@@ -190,7 +188,7 @@ There are also other methods of cloning an object, e.g. using the [spread syntax
190
188
191
189
## Nested cloning
192
190
193
-
Until now we assumed that all properties of `user` are primitive. But properties can be references to other objects. What to do with them?
191
+
Until now we assumed that all properties of `user` are primitive. But properties can be references to other objects.
194
192
195
193
Like this:
196
194
```js run
@@ -205,9 +203,7 @@ let user = {
205
203
alert( user.sizes.height ); // 182
206
204
```
207
205
208
-
Now it's not enough to copy `clone.sizes=user.sizes`, because the `user.sizes` is an object, it will be copied by reference. So `clone` and `user` will share the same sizes:
209
-
210
-
Like this:
206
+
Now it's not enough to copy `clone.sizes=user.sizes`. `user.sizes` is an object, and will be copied by reference, so `clone` and `user` will share the same sizes:
0 commit comments