Probably want to have a deepFreeze() as well as a deepCopy(). Can be helpful if one wants to employ an immutability idiom in JavaScript.
BUT...
I have to point out that immutability really isn't part of the JavaScript paradigm.
You see it in languages like D, Haskell, OCaml/F#, and Elm. But in those language it's part of the core paradigm of those languages.
Trying to mimic that kind of immutability -- even though it is a powerful technique -- in JavaScript is tricky. Any other JavaScript programmer looking at your code will be very puzzled as to the rationale for doing that and all the extra logic stitched in at the right places to support immutability idiom.
It's worth it as an educational exercise. I would be very conservative rolling a JavaScript-based immutability idiom out into production code.
Thanks Elijay, this has been pointed out to me by a few people. I've misused the word, 'immutable' in this post. The data isn't immutable, there's just no link back to the original data.
Also, I'm not trying to suggest using any of these deep copying techniques. It was more to point out that the spread operator didn't do quite what I thought it did.
I may go back and try to firm up these points and change my wording. It's causing a bit of confusion.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Probably want to have a
deepFreeze()as well as adeepCopy(). Can be helpful if one wants to employ an immutability idiom in JavaScript.BUT...
I have to point out that immutability really isn't part of the JavaScript paradigm.
You see it in languages like D, Haskell, OCaml/F#, and Elm. But in those language it's part of the core paradigm of those languages.
Trying to mimic that kind of immutability -- even though it is a powerful technique -- in JavaScript is tricky. Any other JavaScript programmer looking at your code will be very puzzled as to the rationale for doing that and all the extra logic stitched in at the right places to support immutability idiom.
It's worth it as an educational exercise. I would be very conservative rolling a JavaScript-based immutability idiom out into production code.
Thanks Elijay, this has been pointed out to me by a few people. I've misused the word, 'immutable' in this post. The data isn't immutable, there's just no link back to the original data.
Also, I'm not trying to suggest using any of these deep copying techniques. It was more to point out that the spread operator didn't do quite what I thought it did.
I may go back and try to firm up these points and change my wording. It's causing a bit of confusion.