Home > Enterprise >  What are the usecases for Object.freeze() and Object.seal() in javascript?
What are the usecases for Object.freeze() and Object.seal() in javascript?

Time:12-09

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().

CodePudding user response:

One use is to create constants. Say you have a roofing app and you have four roofing types:

const roofPrices = Object.freeze({ metal: 200, slate: 350, clay: 150, solar: 400});

By freezing the object, you can make sure you don't accidentally alter prices, or by sealing it, that you don't accidentally add or remove new "types."

Edit: If anyone is wondering why the const declaration doesn't take care of this, const (as opposed to let) only assigns the variable to one place in memory. This means you can't change a scalar const variable, but since objects can be altered without altering their place in memory, you must use other methods to make them truly constant.

CodePudding user response:

In JavaScript, the Object.freeze() method is used to prevent the modification of existing properties and the addition of new properties to an object. This means that the object becomes read-only and cannot be changed in any way. Object.freeze() is useful for creating objects that should remain constant and not be modified by any part of the program.

On the other hand, the Object.seal() method is used to prevent the addition of new properties to an object, but allows existing properties to be modified. This means that the object is still mutable, but cannot be extended. Object.seal() is useful for creating objects with a fixed set of properties, but where the values of those properties may need to change.

In general, both Object.freeze() and Object.seal() are used to create objects that have a fixed and known set of properties, which can help to prevent unintended changes and improve the reliability and predictability of a program. These methods can also be useful for creating objects that should be used as constants in a program, such as configuration objects or lookup tables.

CodePudding user response:

The difference between (const) variables and (frozen) objects is that the former are local while the latter are often passed around through the entire application. Accidental modifications are much easier if people don't agree on the expectations whether an object should be mutable or not. Often enough, modifications are ok however, the entire paradigm of OOP relies on objects changing state and communicating with each other (through messages, be that method calls or setter invocations). Getting "your" objects modified is the norm, and allows other parts of the code to customise them as necessary. This is why you have not seen codebases where every object is frozen.

You would however freeze objects when your code relies on them being immutable, and doesn't trust other code to keep up that invariant. If bad things would happen, you'd rather get an exception when attempting to modify the object and find that bug immediately, instead of having a wrong value causing havoc down the road without clear provenience.

And yes, this is more common than you might have thought, functional-programming-oriented libraries often (deep) freeze objects, at least in development mode, so that buggy code is discovered as soon as possible.

  • Related