Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Trick get a new copy of object/array using spread operator #1823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

@wonderingabout
Copy link
Contributor

@wonderingabout wonderingabout commented Mar 26, 2020

A common trick using the spread operator :)
Credit goes to @Dorus for teaching me this trick !

Copy link

CLAassistant commented Mar 26, 2020
edited
Loading

CLA assistant check
All committers have signed the CLA.

@wonderingabout wonderingabout changed the title (削除) Trick deep copy spread (削除ここまで) (追記) Trick get a new copy of object/array using spread operator (追記ここまで) Mar 26, 2020

As we remember, arrow functions don't have their own `this`. Now we know they don't have the special `arguments` object either.
````
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not change this. It corresponds to the symbol on line 107.

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, fixed


## Get a new copy of an object/array

Remember when we talked about `Object.assign()`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 230 and 231 should be in one line?

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally, github formatting does automatically a word wrap for one skipped line, the purpose was to keep easy to read code in the github editor
the javascript.info may not behave the same way, so i fixed it to one long line

## Get a new copy of an object/array

Remember when we talked about `Object.assign()`
[in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in)?
[in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in)?

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks

Remember when we talked about `Object.assign()`
[in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in) ?

It is possible to do the exact same thing with the spread operator !
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It is possible to do the exact same thing with the spread operator!
It is possible to do the exact same thing with the spread operator!

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks

let arrCopy = [...arr]; // spread the array into a list of parameters
// then put the result into a new array
// do the arrays have the exact same contents ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// do the arrays have the exact same contents?
// do the arrays have the exact same contents?

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks

// do the arrays have the exact same contents ?
alert(JSON.stringify(arr) === JSON.stringify(arrCopy)); // true (same content)
// are the arrays equal (they share the same reference) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// are the arrays equal (they share the same reference)?
// are the arrays equal (they share the same reference)?

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks

alert(arrCopy) // 1, 2, 3
```

Note that it is possible to do the exact same to copy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 252 and 253 should be in one line?

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as #1823 (comment)

// do the objects have the exact same contents ?
alert(JSON.stringify(obj) === JSON.stringify(objCopy)); // true (same content)
// are the arrays equal (they share the same reference) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// are the arrays equal (they share the same reference)?
// are the arrays equal (they share the same reference)?

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks

alert(obj === objCopy); // false (not same reference)
```

This way of copying an object is much shorter than
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 267 and 268 should be in one line?

Copy link
Contributor Author

@wonderingabout wonderingabout Mar 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as #1823 (comment)

@wonderingabout wonderingabout force-pushed the trick-deep-copy-spread branch 2 times, most recently from 0c158d9 to a54f242 Compare March 27, 2020 06:51
@lex111 lex111 requested a review from paroche March 29, 2020 03:00
Copy link
Collaborator

paroche commented Mar 29, 2020

Alexey -- is there a specific change you want me to look at? Everything I see above seems to be marked "outdated".

In the a54f242 branch linked to in the last section there is some awkward language:

'Note that it is possible to do the exact same to copy of objects: '

could be improved:

'Note that it is possible to do the exact same thing to copy objects:'

or

'Note that it is possible to do the exact same thing to make a copy of an object:'
(my preference)

Rest of the language is OK.

Anything else?

Copy link
Contributor Author

wonderingabout commented Mar 29, 2020
edited
Loading

fixed, it's indeed clearer

i removed the unneeded "exact" word :

'Note that it is possible to do the same thing to make a copy of an object:'

Copy link
Collaborator

paroche commented Mar 29, 2020

Fine by me!

Copy link
Contributor Author

@paroche did you accidentally hit the close PR instead of the merge PR button (just asking myself 🤔 )

Copy link
Collaborator

paroche commented Mar 29, 2020

You could say that. Anyway, as far as I'm concerned it should be merged (and closed).

@paroche paroche reopened this Mar 29, 2020
@paroche paroche merged commit c038ef4 into javascript-tutorial:master Mar 29, 2020
Copy link
Collaborator

paroche commented Mar 29, 2020
edited
Loading

And it is.

Not sure if it was my place, but if not, it can be undone.

Copy link
Contributor Author

yes, you can always @ me if we need to change something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@paroche paroche Awaiting requested review from paroche

1 more reviewer

@leviding leviding leviding left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /