Skip to content

Commit c99d740

Browse files
committed
closes #3179
1 parent 666f356 commit c99d740

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

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

+16-9
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,17 @@ We can also use the method [Object.assign](https://developer.mozilla.org/en-US/d
160160
The syntax is:
161161

162162
```js
163-
Object.assign(dest, src1[, src2, src3...])
163+
Object.assign(dest, ...sources)
164164
```
165165

166166
- 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.
170168

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:
172+
173+
```js run
173174
let user = { name: "John" };
174175
175176
let permissions1 = { canView: true };
@@ -181,6 +182,9 @@ Object.assign(user, permissions1, permissions2);
181182
*/!*
182183
183184
// now user = { name: "John", canView: true, canEdit: true }
185+
alert(user.name); // John
186+
alert(user.canView); // true
187+
alert(user.canEdit); // true
184188
```
185189
186190
If the copied property name already exists, it gets overwritten:
@@ -193,9 +197,9 @@ Object.assign(user, { name: "Pete" });
193197
alert(user.name); // now user = { name: "Pete" }
194198
```
195199
196-
We also can use `Object.assign` to replace `for..in` loop for simple cloning:
200+
We also can use `Object.assign` to perform a simple object cloning:
197201
198-
```js
202+
```js run
199203
let user = {
200204
name: "John",
201205
age: 30
@@ -204,9 +208,12 @@ let user = {
204208
*!*
205209
let clone = Object.assign({}, user);
206210
*/!*
211+
212+
alert(clone.name); // John
213+
alert(clone.age); // 30
207214
```
208215
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.
210217
211218
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.
212219

0 commit comments

Comments
 (0)