Skip to content

Commit 1dc862c

Browse files
authored
Merge pull request #2966 from OmerBaddour/patch-2
minor fix, and explanation improvements
2 parents 87b5780 + adca4b3 commit 1dc862c

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

1-js/04-object-basics/02-object-copy/article.md

+6-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ And here's how it's actually stored in memory:
3737

3838
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.
3939

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.
4141

4242
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.
4343

@@ -104,11 +104,9 @@ For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj
104104
105105
So, copying an object variable creates one more reference to the same object.
106106
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?
108108
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.
112110
113111
Like this:
114112
@@ -133,7 +131,7 @@ clone.name = "Pete"; // changed the data in it
133131
alert( user.name ); // still John in the original object
134132
```
135133
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).
137135
138136
The syntax is:
139137
@@ -190,7 +188,7 @@ There are also other methods of cloning an object, e.g. using the [spread syntax
190188
191189
## Nested cloning
192190
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.
194192
195193
Like this:
196194
```js run
@@ -205,9 +203,7 @@ let user = {
205203
alert( user.sizes.height ); // 182
206204
```
207205
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:
211207
212208
```js run
213209
let user = {

0 commit comments

Comments
 (0)