Developed By
Gautam Kumar - Full stack developer
DEEP DIVE INTO
In JavaScript, object immutability can be achieved using methods like Object.freeze() and Object.seal(). These methods offer different levels of immutability and protection to objects. Let's dive deeper into Object freezing and sealing:
Object.freeze()
is used to make an object immutable, meaning its properties cannot be added, modified, or removed. It creates a deep immutable state, affecting not only the object but also its nested objects and arrays.
Any attempts to modify a frozen object or its properties will fail silently in non-strict mode or throw an error in strict mode.
javascriptconst obj = {
name: "John",
age: 30,
};
Object.freeze(obj);
obj.age = 31; // This assignment will be ignored silently in non-strict mode
console.log(obj); // Output: { name: 'John', age: 30 }
Object.seal()
prevents new properties from being added to an object and marks all existing properties as non-configurable. While you can modify the values of existing properties in a sealed object, you cannot change their attributes, such as writable or configurable.
However, in contrast to Object.freeze()
, it doesn’t make the object's contents immutable, only the structure.
javascriptconst obj = {
name: "John",
age: 30,
};
Object.seal(obj);
obj.age = 31; // This assignment will work, as it changes the value
obj.gender = "male"; // This addition will fail silently in non-strict mode
console.log(obj); // Output: { name: 'John', age: 31 }
Freezing vs. Sealing:
Object.freeze()
creates a completely immutable object (values and structure), whereas O
Object.seal()
only prevents addition and removal of properties but allows the modification of existing properties.
Flexibility:
Object.freeze()
is stricter and more secure, ensuring complete immutability.
Object.seal() allows modification of existing values but restricts structural changes.
Error Handling:
Object.freeze()
throws an error if changes are attempted on a frozen object in strict mode.
Object.seal()
silently ignores property additions in non-strict mode but allows modifications to existing properties.
Performance:
Object.freeze()
might have a slightly higher performance cost due to deeper immutability, while
Object.seal()
may be faster since it allows property value modifications.
Both Object.freeze()
and Object.seal()
work on the top-level properties of an object. Nested objects and arrays will need to be frozen or sealed separately for complete immutability.
These methods don't provide protection against changes in the object's referenced data types (like objects and arrays). Changes to their properties can still occur unless they are also frozen or sealed.
When applying these methods, ensure they are used appropriately as per your requirements. Object.freeze()
provides the highest level of immutability, while Object.seal()
allows more flexibility.
By utilizing Object.freeze() and Object.seal(), you can control the mutability of your objects in JavaScript, ensuring integrity and avoiding unintended modifications in your data structures.