DEV Community

Discussion on: How to manipulate immutably and mutably JavaScript Array with only ES6+

Collapse
 
aminmansuri profile image
hidden_dude

I'd call that a "defensive copy" rather than being inmutable.

Collapse
 
snamiki1212 profile image
Nash

Thank you comment!

Right! strictly speaking, it's not immutable. I didn't know the word "defensive copy".

Collapse
 
aminmansuri profile image
hidden_dude

I guess it wouldn't be hard in Javascript to create a decorator object that wraps your list / array where you get an error or no-op if you try to mutate it.

That would have the advantage of not needing to copy a potentially huge object.
(though map/reduce/etc.. generally do make copies or at least "stream" the new values)

Collapse
 
aminmansuri profile image
hidden_dude

inmutable would be interesting though.. I have no idea how to do that.