Native cloning is coming to Javascript soon!

If you’ve ever wondered “why doesn’t Javascript have a method to deep clone?”, you wouldn’t be the only one.

For years we’ve been using various methods or libraries to clone objects and it’s never felt quite right. Thankfully, structuredClone is comin…


This content originally appeared on DEV Community and was authored by James Thomson

If you've ever wondered "why doesn't Javascript have a method to deep clone?", you wouldn't be the only one.

For years we've been using various methods or libraries to clone objects and it's never felt quite right. Thankfully, structuredClone is coming to a browser near you! Don't get scared off by the sea of red for browser support. All browsers have implemented it in their nightly releases, so it's on the way.

How do we use it you say? Simple:

const myObj = {
  title: 'Something',
  myNestedObj: {
    title: 'Something else',
    // ... and so on
  }
}
const myDeepCopy = structuredClone(myObj);

That's it. You now have a deep clone of your object. There are some limitations to be aware of though.

As always,
Happy coding! 🤓
Cover photo credit


This content originally appeared on DEV Community and was authored by James Thomson


Print Share Comment Cite Upload Translate Updates
APA

James Thomson | Sciencx (2021-12-16T23:52:02+00:00) Native cloning is coming to Javascript soon!. Retrieved from https://www.scien.cx/2021/12/16/native-cloning-is-coming-to-javascript-soon/

MLA
" » Native cloning is coming to Javascript soon!." James Thomson | Sciencx - Thursday December 16, 2021, https://www.scien.cx/2021/12/16/native-cloning-is-coming-to-javascript-soon/
HARVARD
James Thomson | Sciencx Thursday December 16, 2021 » Native cloning is coming to Javascript soon!., viewed ,<https://www.scien.cx/2021/12/16/native-cloning-is-coming-to-javascript-soon/>
VANCOUVER
James Thomson | Sciencx - » Native cloning is coming to Javascript soon!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/16/native-cloning-is-coming-to-javascript-soon/
CHICAGO
" » Native cloning is coming to Javascript soon!." James Thomson | Sciencx - Accessed . https://www.scien.cx/2021/12/16/native-cloning-is-coming-to-javascript-soon/
IEEE
" » Native cloning is coming to Javascript soon!." James Thomson | Sciencx [Online]. Available: https://www.scien.cx/2021/12/16/native-cloning-is-coming-to-javascript-soon/. [Accessed: ]
rf:citation
» Native cloning is coming to Javascript soon! | James Thomson | Sciencx | https://www.scien.cx/2021/12/16/native-cloning-is-coming-to-javascript-soon/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.