I've done some searching and found that JavaScript objects can be frozen or sealed, meaning that they cannot be modified or have new properties added to them, respectively.
I understand what these methods do, but not why one would ever want to use them in a codebase.
One guess I have for why to use freeze()
is to prevent errors in the category of accidental modification of variables: declaring variables with const
by default prevents a lot of such errors (though const
only applies to bindings, and not variables themselves) and imposes very little syntactic burden. On the other hand, calling .freeze()
on every declared object seems like it would be deeply impractical, and I haven't ever seen a codebase do this.
However, I don't have even a viable guess for when to use seal()
.
.freeze()
on every declared object" - why would you suggest that? This is not what you're doing withconst
either, you still have variables declared withlet
. – Boger