Freezing an object
Prevent modification of an object using Object.freeze()
The Object.freeze()
method freezes an object. A frozen object can no longer be changed,prevents new properties from being added to it and existing properties from being removed.
After calling Object.freeze(user)
, attempting to modify the user object will have no effect:
Use Case
The most common use case is in a web server configuration. When configuring a web server, itβs often necessary to provide settings that are not expected to change. As a result, we freeze the configuration object to ensure its immutability.
Here we used const
and freeze
both because, In JavaScript, itβs important not to confuse const, with immutability, const
creates a variable name binding which canβt be reassigned after creation. const
does not create immutable objects. For example, if you have a const
variable that holds an object, you canβt make it point to a different object, but you can still modify the properties or contents of the object it points to. This is why const doesnβt make objects themselves immutable, only the variable holding the object. Thats why we need to freeze the object to make it immutable.
Advantages
- Freezing an object can improve performance in some cases, since the JavaScript engine does not need to check whether the object has been modified before performing certain operations.
- It can also help prevent accidental changes to an object that is shared between multiple parts of the application.