DEV Community

Discussion on: What do you dislike about your favorite language?

Collapse
 
kenbellows profile image
Ken Bellows

Have you played with Object.freeze()? It makes an object immutable at the top level (though the properties of objects within that object can still be changed)

Collapse
 
omenlog profile image
Omar E. Lopez

Hi, yes I know about it, as you mention inner objects can be modified unless that you freeze these objects recursively but anyway I don't think that this can be classified as immutability imposed by the language itself

Thread Thread
 
kenbellows profile image
Ken Bellows

So imagining that it was recursive, would you want immutability to be the default behavior rather than having to apply it with a method?

Thread Thread
 
pris_stratton profile image
pris stratton • Edited

I guess he wants const to really mean constant =)

I think a nice way to ensure objects are immutable is to use a closure when constructing them and only return getter methods, like in Douglas Crockford’s constructor example.

Thread Thread
 
omenlog profile image
Omar E. Lopez

Yes can be, immutable by default but at the same time giving the option to developers of make some value mutable, similar at how mut keyword work in Rust, I like that approach, in this way you can work with mutable data but immutability would be the main paradigm in this issue

Thread Thread
 
pris_stratton profile image
pris stratton

Makes sense. I’d like that feature in JS.