Cloning elements without adding them to active page, or duplicating objects in-memory with a new ID #86
ADTC
started this conversation in
Show and tell
Replies: 1 comment 1 reply
-
What is your question or request here? To have a method for in-memory clone? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If I do this:
The elements are duplicated and added to the active page.
This was quite a surprise to me, as I expected the
clone
function to simply create an in-memory clone with a newid
and return it (not add it to active page). If it's adding it to the active page, I would have expected it to be calledduplicate
instead, with another functionclone
to do the in-memory clone.Since we don't have that option, I suppose the best way right now is to first use an object cloning method as mentioned in freeCodeCamp:
I think we should prefer
JSON.parse(JSON.stringify
since it's deep cloning while the other two are shallow copying.And then, the next step is to make a new ID using
nanoid
:Easy enough to do my own implementation:
Beta Was this translation helpful? Give feedback.
All reactions