Appearance
Object.preventExtensions(), Object.seal(), and Object.freeze
Property Descriptor
Each data property has these important attributes:
value-- the property's value.writable-- whether the value can be changed.enumerable-- whether the property appears inObject.keys(),for...in, etc.configurable-- whether the property can be deleted or its descriptor changed.
Object.preventExtensions()
Prevents adding new properties.
const obj = { name: "Chandan" };
Object.preventExtensions(obj);
obj.age = 20; // Ignored (or TypeError in strict mode)
obj.name = "Rahul"; // Works
delete obj.name; // WorksEffects:
- Cannot add new properties.
- Can modify existing writable properties.
- Can delete configurable properties.
- Does not change property descriptors.
Object.seal()
Locks the object's shape.
const obj = { name: "Chandan" };
Object.seal(obj);
obj.name = "Rahul"; // Works
obj.age = 20; // Not allowed
delete obj.name; // Not allowedEffects:
- Cannot add new properties.
- Cannot delete properties.
- Existing writable properties can still be modified.
- Sets
configurable: falsefor all own properties. - Leaves
writableunchanged. - Leaves
enumerableunchanged.
Object.freeze()
Makes the object effectively immutable (shallow).
const obj = { name: "Chandan" };
Object.freeze(obj);
obj.name = "Rahul"; // Not allowed
obj.age = 20; // Not allowed
delete obj.name; // Not allowedEffects:
- Cannot add new properties.
- Cannot delete properties.
- Cannot modify existing data properties.
- Sets
configurable: falsefor all own properties. - Sets
writable: falsefor all own data properties. - Leaves
enumerableunchanged. - Shallow only.
Shallow Freeze Example
const obj = {
user: { name: "Chandan" }
};
Object.freeze(obj);
obj.user.name = "Rahul"; // WorksOnly the top-level object is frozen.
Understanding configurable
configurable controls whether a property can be:
- Deleted.
- Redefined with
Object.defineProperty(). - Have attributes like
enumerablechanged.
Once configurable is set to false, it can never become true again.
Exception:
- If
writableistrue, it may be changed tofalse. writable: falsecannot later becometruewhenconfigurableisfalse.
Comparison
| Feature | preventExtensions | seal | freeze |
|---|---|---|---|
| Add new properties | No | No | No |
| Delete properties | Yes | No | No |
| Modify existing values | Yes | Yes (if writable) | No |
configurable | Unchanged | false | false |
writable | Unchanged | Unchanged | false (data properties) |
enumerable | Unchanged | Unchanged | Unchanged |
| Shallow | Yes | Yes | Yes |
Summary
preventExtensions()→ Prevents adding new properties.seal()→ Prevents adding and deleting properties. Existing writable values can still change.freeze()→ Prevents adding, deleting, and changing existing data property values.freeze()andseal()are shallow.configurable: falseis irreversible.