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
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,7 +203,7 @@ let user = {
203
203
alert( user.sizes.height ); // 182
204
204
```
205
205
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:
206
+
Now it's not enough to copy `clone.sizes=user.sizes`, because`user.sizes` is an object, and will be copied by reference, so `clone` and `user` will share the same sizes:
user.sizes.width++; // change a property from one place
223
-
alert(clone.sizes.width); // 51, see the result from the other one
223
+
alert(clone.sizes.width); // 51, get the result from the other one
224
224
```
225
225
226
-
To fix that, we should use a cloning loop that examines each value of `user[key]` and, if it's an object, then replicate its structure as well. That is called a "deep cloning".
226
+
To fix that and make `user` and `clone` truly separate objects, we should use a cloning loop that examines each value of `user[key]` and, if it's an object, then replicate its structure as well. That is called a "deep cloning".
227
227
228
228
We can use recursion to implement it. Or, to not reinvent the wheel, take an existing implementation, for instance [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep) from the JavaScript library [lodash](https://lodash.com).
0 commit comments