Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@Jameslikestea @juunas
Object.assign() is not a deep clone.
Actually there is no good JS solution to deep clone. Even the one I posted loses any methods that you would’ve added to obj1 prototype chain 🧐 -
@arturgrigio I was under the impression it was if you assign to an empty object and a variable equal the return... never mind
-
@Jameslikestea easy to test...
obj1 = {foo:{bar:0}}
obj2 = Object.assign({}, obj2)
obj1.foo.bar = 1
console.log(obj2.foo.bar)
This should log out: 1 not: 0 -
Because, like it or not, this is a hack, and although it isn't very legit-looking, it has been proven (at least the last time I researched it) time and time again to be the fastest way to deep clone objects, probably because the JSON parser is so fucking fast due to JSON strings being returned by servers all the fucking time.
Related Rants
Just followed the JavaScript MDN dev docs example to do this:
// Deep Clone
let obj1 = { a:1, b: { c:2 }};
let obj2 = JSON.parse(JSON.strigify(obj1))
Why does it feel wrong to write this code?
rant
js
deep clone
shitty code