JavaScript > Objects and Arrays > Advanced Object Concepts > Object.seal()
Understanding Object.seal() in JavaScript
This code snippet demonstrates how to use Object.seal()
in JavaScript to prevent new properties from being added to an object and marking all existing properties as non-configurable. We'll explore the implications and usage with clear examples.
Basic Usage of Object.seal()
This code demonstrates the core functionality of Object.seal()
. After sealing the 'person' object, we attempt to add a new property ('city') and delete an existing property ('age'). These operations fail. However, modifying an existing property ('name') is still permitted. Note that the behavior for failed operations is different depending on if strict mode is enabled. In strict mode it will throw an error.
// Create a simple object
const person = {
name: 'John',
age: 30
};
// Seal the object
Object.seal(person);
// Try to add a new property
person.city = 'New York'; // This will fail in strict mode, otherwise silently ignored.
// Try to delete an existing property
delete person.age; // This will fail in strict mode, otherwise silently ignored.
// Try to modify an existing property
person.name = 'Jane'; // This will succeed
console.log(person); // Output: { name: 'Jane', age: 30 }
Concepts Behind Object.seal()
Object.seal()
prevents new properties from being added to an object and marks all existing properties as non-configurable. This means you cannot add or delete properties. The values of existing properties can still be changed as long as they are writable. Sealing an object makes its structure fixed. It essentially combines Object.preventExtensions()
and setting configurable: false
for all existing properties.
Real-Life Use Case
Imagine you are developing a configuration object that should not be modified after initialization. Using Object.seal()
ensures that no new settings can be added, and no existing settings can be removed, preventing unexpected behavior in your application. For example, a settings object representing API keys or database connection details should not be altered after the initial configuration.
Best Practices
Use Object.seal()
when you want to create an object with a fixed set of properties and prevent accidental or malicious modifications to its structure. Ensure that you understand the implications of sealing an object before applying it, as it can impact the flexibility of your code. Consider carefully which objects in your application require this level of immutability.
Interview Tip
Be prepared to discuss the difference between Object.seal()
, Object.freeze()
, and Object.preventExtensions()
. Understand that Object.seal()
prevents adding or deleting properties but allows modifying existing writable properties. Object.freeze()
is more restrictive and prevents modification of existing properties. Object.preventExtensions()
only prevents adding new properties.
When to Use Them
Use Object.seal()
when you need to ensure the structure of an object remains constant, but you still need to allow modification of its property values. This is useful for configuration objects, data transfer objects (DTOs) or any object where the properties are known and should not be changed. This offers a middle ground between full mutability and full immutability of Object.freeze()
.
Memory Footprint
Sealing an object itself doesn't directly change the memory footprint significantly, but it can help with optimization in some JavaScript engines by allowing them to make assumptions about the object's structure. The actual memory used by the properties of the object remains the same. However, by preventing changes, the engine can potentially optimize the object's layout.
Alternatives
If you need even stricter immutability, consider using Object.freeze()
. If you only want to prevent the addition of new properties but still allow deletion and modification, use Object.preventExtensions()
.
Pros
Cons
FAQ
-
What happens if I try to add a property to a sealed object?
In strict mode, attempting to add a property to a sealed object will throw a TypeError. In non-strict mode, the operation will silently fail, and the property will not be added. -
Can I modify the values of existing properties in a sealed object?
Yes, you can modify the values of existing properties as long as they are writable.Object.seal()
only prevents adding new properties and deleting existing ones, and it makes all existing properties non-configurable, not non-writable. -
How is
Object.seal()
different fromObject.freeze()
?
Object.seal()
prevents adding new properties and deleting existing ones, whileObject.freeze()
prevents adding, deleting, and modifying properties.Object.freeze()
essentially seals the object and also sets all its writable properties to read-only.