How To Make a DEEP Copy in JavaScript (No Libraries and No Hacks!)

preview_player
Показать описание
There's a new way to make deep copies of objects in JavaScript!

**RESOURCES**

**SPONSORED BY STORYBLOK**

**QUESTIONS ABOUT MY SETUP**

**STAY IN TOUCH 👋**

**DISCORD**

**TIMESTAMPS**
00:00 - Intro
00:45 - Object Reassignment
01:35 - Spread Operator
02:30 - Sponsor Shoutout - Storyblok
04:15 - 3rd Party Libraries Like Lodash
04:40 - StructuredClone
05:59 - Wrap Up
Рекомендации по теме
Комментарии
Автор

StructuredClone is actually a web API (like setTimeout, etc), not part of JS. If the object contains methods, structuredClone will throw an exception, the JSON hack will strip them for the new object without erroring,

jasonrm
Автор

Always learning interesting and useful features. I didn’t know that spread operator was not going deep. Thanks 🙏

christopheanfry
Автор

Love it! Finally a real native way to do a deep copy! 👍

Joe-SoftwareEngineer
Автор

Thanks for this valuable code guide. 👍

manojjangid
Автор

Thank you so much. This is problem that I faced last week, made me worth following you :p

AjayKumar-hgje
Автор

Great and important topic on JavaScript James. Master JS is so important 🙂

universecode
Автор

Thank you so much for this, James! You really helped a lot.

__jake.m
Автор

Great video! IE11 was killed off anyway so this is basically full support. However, it was only added to Node 17 so it won't work if you're on an older version of Node.

colinmarshall
Автор

What version of NodeJS does it support?
Also what about an Instance object? not JSON object but an object yielded by `new Class()`,
and does it also clone the object methods?

deckyfiyemonda
Автор

If object has function it copy that also?

pakkavideos
Автор

hey James, which theme are you using?

michaelkrison
Автор

It would be cool if they extended the spread operator with an argument such as {….foo, 2} meaning copy two levels deep 🤔

astb
Автор

Still need to depend upon third party libraries, as structured clones do not work in case of function

shobhitkumar_shorts
Автор

Shoot I use spread operator all of my codes! Does this work with array of object too??

willyhorizont
Автор

Pretty good support ?! I would say no. Check the table for browser compatibility, it kinda sucks. I think the hack method is the best, 2nd option you can use a 3rd party lib. But who am I ? Just a petty sr developer with many years of experience that knows: compatibility is a "beach" and frugality is very important.

everusa
Автор

Javascript is just a weird language, I feel like it try to act like a programming language but we are obligated to use it since the browser work with js .

AlaaBallout
Автор

"Not IE11, that's a separate conversation".... When the E11 conversation going ot happen? ( O _ O )

talkMeGooseman
Автор

Don't use if your users have older chrome versions or other browsers

orbitory
Автор

This is absurd to have to jump through so many hoops just to copy an object. It should absolutely be a native JS function.

bmp