DEV Community

Discussion on: Deep vs Shallow Copy - with Examples

Collapse
 
cecilelebleu profile image
Cécile Lebleu

This is a great write up! Thank you for sharing. I do have a question though.
I don’t understand the example about nested objects; why is copy in the end still the same as the original obj? I feel that with the accompanying explanation maybe the snippet is wrong, but I don’t know much about how these details work; would you mind explaining this one a bit further?

let obj = {a:1, b:2, c: {a:1}}
let copy = {...obj}
obj['c']['a'] = 5
// obj is {a:1, b:2, c: 5}
// copy is {a:1, b:2, c: {a:1}}
Collapse
 
laurieontech profile image
Laurie

I wrote out a full explanation and the realized the snippet is indeed wrong! Thanks for catching that.

Collapse
 
cecilelebleu profile image
Cécile Lebleu

Thanks for clearing it out!