What are the differences between Object.freeze() and Object.seal() in JavaScript?Gable E
In JavaScript,Object.freeze()
andObject.seal()
are methods used to control the mutability and behavior of objects. While they both provide object-level immutability, there are important differences between them. Let's explore these differences:
1. Immutability:
-Object.freeze()
:
TheObject.freeze()
method makes an object completely immutable by preventing any modifications to its properties. Once an object is frozen, you cannot add, delete, or modify its properties. Additionally, any attempts to change the values of existing properties or change the object's prototype will be ignored.
-Object.seal()
:
TheObject.seal()
method allows modifications to the values of existing properties but prevents adding or deleting properties. Once an object is sealed, you can modify the values of its existing properties, but you cannot add new properties or delete existing ones. The object's prototype is also retained and can be modified.
2. Effect on Existing Properties:
-Object.freeze()
:
When an object is frozen, the values of its existing properties become read-only. Any attempts to modify these properties will be silently ignored in strict mode, while in non-strict mode, they will fail silently or throw an error depending on the JavaScript engine's behavior.
-Object.seal()
:
When an object is sealed, the values of its existing properties can be modified. You can change the values of the properties, but you cannot add or delete properties. However, the configurable attribute of the existing properties is set tofalse
, meaning you cannot change their attributes likewritable
orconfigurable
.
3. Extensibility:
-Object.freeze()
:
A frozen object is not extensible. You cannot add new properties or methods to a frozen object, and any attempts to do so will be ignored or throw an error in strict mode.
-Object.seal()
:
A sealed object is also not extensible. You cannot add new properties to a sealed object. However, you can modify the values of existing properties.
4. Prototype:
-Object.freeze()
:
Freezing an object also freezes its prototype chain. The prototype object and its properties become read-only as well.
-Object.seal()
:
Sealing an object does not affect its prototype chain. You can still modify the properties of the prototype object.
5. Usage:
-Object.freeze()
:
TheObject.freeze()
method is commonly used when you want to ensure that an object's properties remain constant and cannot be modified.
-Object.seal()
:
TheObject.seal()
method is useful when you want to prevent adding or deleting properties while still allowing modifications to the values of existing properties.
Here's an example to illustrate the differences:
1 2 3 4 5 6 7 8 9 10 11 12
const obj = { name: 'John', age: 30 }; Object.freeze(obj); obj.name = 'Jane'; // Ignored in strict mode, fails silently in non-strict mode delete obj.age; // Ignored in strict mode, fails silently in non-strict mode obj.gender = 'Male'; // Ignored in strict mode, fails silently in non-strict mode Object.seal(obj); obj.name = 'Jane'; // Modifies the value of the existing property delete obj.age; // Ignored in strict mode, fails silently in non-strict mode obj.gender = 'Male'; // Ignored in strict mode, fails silently in non-strict mode
To summarize:
-Object.freeze()
makes an object completely immutable, preventing modifications to existing properties and changes to the object's prototype.
-Object.seal()
allows modifications to the values of existing properties but prevents adding or deleting properties. The object's prototype can still be modified.
-Object.freeze()
creates a read-only object, whileObject
.seal()
allows modification of existing property values but not the structure of the object.
- Both methods make objects non-extensible, meaning new properties cannot be added.
-Object.freeze()
also freezes the prototype chain, whileObject.seal()
does not affect it.
Choose the method that best fits your requirements for controlling object mutability and behavior.
Similar Questions
What are the differences between JavaScript's Object.freeze() and Object.seal() methods?
What are the differences between Object.keys() and Object.values() in JavaScript?
What are the differences between JavaScript's Object.create() and new Object()?
What are the differences between JavaScript's Object.keys() and Object.values()?
What are the differences between JavaScript's Object.assign() and Object spread?
What are the differences between JavaScript's Object.create() and Object.assign() methods?
What are the differences between Object.assign() and the spread operator in JavaScript?
What are the differences between map() and forEach() in JavaScript?
What are the differences between Promise.all() and Promise.race() in JavaScript?
What are the differences between Date.now() and new Date() in JavaScript?
What are the differences between setTimeout() and setInterval() in JavaScript?
What are the differences between setTimeout() and setInterval() in JavaScript?
What are the differences between Map and WeakMap in JavaScript?
What are the differences between parseInt() and parseFloat() in JavaScript?
What are the differences between Array.some() and Array.every() in JavaScript?
What are the differences between Function.call() and Function.apply() in JavaScript?
What are the differences between Array.pop() and Array.shift() in JavaScript?
What are the differences between Array.from() and the spread operator in JavaScript?
What are the differences between Array.filter() and Array.find() in JavaScript?
What are the differences between Array.forEach() and Array.map() in JavaScript?