What is Object.freeze()?
Object.freeze() is a static method in JavaScript that makes an object immutable. It prevents any modifications to the object's properties and prototype.
What is Object.freeze()?
The Object.freeze() method is used to freeze an object. Freezing an object prevents new properties from being added to it, existing properties from being removed, existing properties (or their enumerability, configurability, or writability) from being changed, and the object's prototype from being re-assigned. It essentially locks down the object's structure and data.
Once an object is frozen, it cannot be unfrozen. The method returns the same object that was passed in. It's important to note that Object.freeze() performs a 'shallow freeze', meaning it only freezes the direct properties of the object itself. If the object contains other objects, those nested objects are not frozen and can still be modified.
Syntax
Object.freeze(obj)
Where obj is the object to be frozen.
Example
const user = {
name: 'Alice',
age: 30,
address: {
city: 'New York',
zip: '10001'
}
};
Object.freeze(user);
// Attempting to modify properties of the frozen object
user.name = 'Bob'; // Fails silently in non-strict mode, throws TypeError in strict mode
delete user.age; // Fails silently/throws TypeError
user.gender = 'Female'; // Fails silently/throws TypeError
// Modifying a nested object (shallow freeze)
user.address.city = 'London'; // This will succeed
console.log(user); // Output: { name: 'Alice', age: 30, address: { city: 'London', zip: '10001' } }
console.log(Object.isFrozen(user)); // Output: true
In the example above, attempts to change user.name, delete user.age, or add user.gender will fail. However, user.address.city *can* be changed because Object.freeze() only performs a shallow freeze; the address object itself is not frozen.
Key Characteristics
- Immutability: Prevents all modifications to the object's properties (add, delete, update) and prototype.
- Shallow Freeze: Only freezes the top-level properties. Nested objects remain mutable unless explicitly frozen.
- Returns the Object: The method returns the same object that was passed as an argument.
- Idempotent: Calling
Object.freeze()on an already frozen object has no effect. - Strict Mode Implications: In strict mode, attempts to modify a frozen object will throw a
TypeError. In non-strict mode, these attempts will silently fail.
Use Cases
- Configuration Objects: Freezing configuration objects ensures their settings are not accidentally altered during application runtime.
- Immutable Data Structures: Useful when working with data that should not change after creation, promoting predictable behavior.
- Public APIs: When exposing an object as part of a public API, freezing it can prevent external code from modifying internal state.
- Performance Optimizations: While not a primary use, some JavaScript engines can optimize frozen objects, as they know the object structure won't change.
Difference from `const`
const applies to variables, making sure the variable reference cannot be reassigned. It does not make the value itself immutable. For objects, const means the variable myObject will always point to the same object in memory, but the properties of myObject can still be changed. Object.freeze(), on the other hand, makes the object's *content* immutable, regardless of how the variable referencing it was declared.