
JavaScript: Create | Read | Update | Delete Operations - freeze(), seal(), preventExtensions()
Object.freeze():-
This method freezes the object which means new properties cannot be added, existing properties cannot be removed or modified. The enumerability, configurability or writability of existing properties is prohibited. It returns the same object (does not create a copy) that was passed into the function. Also, the frozen object is immutable (not necessarily constant).
Syntax
Object.freeze(obj)
Have you heard about Shallow Freeze?
When you call the method Object.freeze()
on an object
, the freeze rules are applicable only to the immediate properties of object
which implies if the values of these properties are object
themselves those objects
are not bound by any rules and are not frozen.
Object.seal():-
This method seals the object
which means it allows to change existing properties but new properties cannot be added to it. Also, all existing properties are marked as non-configurable. But it does not prevent the value
of data properties from being changed. The __proto__
property is sealed too.
Syntax
Object.seal(obj)
Object.preventExtensions():-
This method makes the object
non-extensible i.e. new properties can never be added to an object
. This process is irreversible i.e. if an object
is marked as non-extensible, it cannot be reversed. However, properties can still be added to the object prototype
.
Syntax
Object.preventExtensions(obj)
Happy Learning!
![]() | ![]() | ![]() | ![]() |
Like | Comment | Save | Share |