DEV Community

Discussion on: const 📦, seal 🤐, freeze ❄️ & immutability 🤓 in JS

Collapse
 
moopet profile image
Ben Sinclair

I was looking at these the other day and think they're a disaster waiting to happen. Why do they let you try to change things you can't change without throwing an exception? When trying to reassing a const does?

MDN says

Attempting to delete or add properties to a sealed object, or to convert a data property to accessor or vice versa, will fail, either silently or by throwing a TypeError (most commonly, although not exclusively, when in strict mode code).

Which is just about as vague as you can get. It's, "do this, and that might happen. YOLO."

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

It means if you don't use strict mode it will fail silently but as most transpiler tools will allow the option to compile in strict mode it's just a Boolean away. Or you can just add strict mode yourself. Then catching exceptions becomes a useful idea in JavaScript.

Collapse
 
frehner profile image
Anthony Frehner

Yeah! I even just wrote an article exploring how strict mode affects Freeze-ing objects and calling functions and stuff, since I just barely had to dive into it. dev.to/frehner/exploring-javascrip... Hope it helps :)