DEV Community

Discussion on: Understanding Deep Copy and Shallow Copy in Javascript

Collapse
 
monaye profile image
Monaye Win

People confuse that spreader do a deeply nested copy. Need to be aware that both spreader and obj.assign doesn't do nested deep copy.

Collapse
 
edwardluu profile image
Edward Luu

Could you share the detail of this?

Collapse
 
iliashterevgit profile image
Info Comment hidden by post author - thread only visible in this permalink
ilia-shterev-git

I think that what he/she means is that that both spreader and obj.assign do level 1 clone. So if the object to be cloned contains other objects they will be cloned by ref or shallow copy. So later if you want to change that nested objects like:
myClonedObj.myNestedObj.somePropertyOfTheNestedObj = someNewVal;
then all other object nesting that object will have it changed.

Some comments have been hidden by the post's author - find out more