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
+16-9
Original file line number
Diff line number
Diff line change
@@ -160,16 +160,17 @@ We can also use the method [Object.assign](https://developer.mozilla.org/en-US/d
160
160
The syntax is:
161
161
162
162
```js
163
-
Object.assign(dest, src1[, src2, src3...])
163
+
Object.assign(dest, ...sources)
164
164
```
165
165
166
166
- The first argument `dest` is a target object.
167
-
- Further arguments`src1, ..., srcN` (can be as many as needed) are source objects.
168
-
- 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.
169
-
- The call returns `dest`.
167
+
- Further arguments is a list of source objects.
170
168
171
-
For instance, we can use it to merge several objects into one:
172
-
```js
169
+
It copies the properties of all source objects into the target `dest`, and then returns it as the result.
170
+
171
+
For example, we have `user` object, let's add a couple of permissions to it:
We also can use `Object.assign` to replace `for..in` loop forsimple cloning:
200
+
We also can use `Object.assign` to perform a simple object cloning:
197
201
198
-
```js
202
+
```js run
199
203
let user = {
200
204
name: "John",
201
205
age: 30
@@ -204,9 +208,12 @@ let user = {
204
208
*!*
205
209
let clone = Object.assign({}, user);
206
210
*/!*
211
+
212
+
alert(clone.name); // John
213
+
alert(clone.age); // 30
207
214
```
208
215
209
-
It copies all properties of`user` into the empty object and returns it.
216
+
Here it copies all properties of `user` into the empty object and returns it.
210
217
211
218
There are also other methods of cloning an object, e.g. using the [spread syntax](info:rest-parameters-spread) `clone = {...user}`, covered later in the tutorial.
0 commit comments