What are the usecases for Object.freeze() and Object.seal() in JavaScript?
Asked Answered
M

4

7

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

Masters answered 9/12, 2022 at 0:49 Comment(1)
"calling .freeze() on every declared object" - why would you suggest that? This is not what you're doing with const either, you still have variables declared with let.Boger
B
4

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.

Boger answered 9/12, 2022 at 1:27 Comment(0)
P
7

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.

Profuse answered 9/12, 2022 at 0:59 Comment(0)
B
4

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.

Boger answered 9/12, 2022 at 1:27 Comment(0)
N
3

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.

Nephrosis answered 9/12, 2022 at 0:53 Comment(0)
J
1

Object.freeze() can be useful for unit tests and mock objects. You can freeze an object to ensure it remains unchanged and prevent side effects throughout your tests.

e.g.

export const fakeUser: User = Object.freeze({
  id: '00000000',
  // ...other fields
});

export const createFakeUser = () => {
  return {
    ...fakeUser,
  };
};

This way you have a consistent base object which you can create for different tests without affecting the original fakeUser object.

Jointly answered 28/2 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.