Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Imagine you have a vault with different security levels! JavaScript gives you three methods to protect objects from changes:freeze() (complete lockdown),seal() (can't add/remove, but can modify), andpreventExtensions() (can't add new properties).
Complete lockdown!
Partial lock!
Block new entries!
Object.isFrozen(obj)Check if frozen
Object.isSealed(obj)Check if sealed
Object.isExtensible(obj)Check if extensible
See the differences between the three methods
Protect app configuration from accidental changes
| Action | freeze() | seal() | preventExtensions() |
|---|---|---|---|
| Add properties | ❌ | ❌ | ❌ |
| Remove properties | ❌ | ❌ | ✅ |
| Modify values | ❌ | ✅ | ✅ |
| Change descriptors | ❌ | ❌ | ✅ |
All three methods are shallow - they only protect the immediate properties. Nested objects can still be modified unless you deep freeze them!
Once frozen or sealed, you can't undo it! The object is permanently locked. Be absolutely sure before applying these methods.
In non-strict mode, violations fail silently (no error). Use 'use strict' to get proper errors!
Complete immutability - nothing can be changed
Can modify values, but can't add/remove properties
Only blocks adding new properties
Nested objects need to be frozen separately