Object.preventExtensions, seal, and freeze
Explore how to use Object.preventExtensions, seal, and freeze to make JavaScript objects less mutable. This lesson covers how these methods restrict property additions, deletions, and modifications to help enforce functional programming principles and maintain object integrity.
We'll cover the following...
In this lesson, we’ll cover three functions that allow us to enforce functional programming. Each of these functions makes our object inflexible to varying degrees. We can prevent property addition, or we can freeze an object entirely.
Object.preventExtensions
Preventing Extensions
This is a function that helps us approach immutability. Once it’s called on an object, we can’t add anything to it.
Deletions and changes are still allowed. Only property addition is forbidden.
Checking Extensibility
We have a utility method that tells us if an object is extensible or not: Object.isExtensible.
Locked Prototype
Object.preventExtensions permanently locks an object’s prototype. Attempts to use Object.setPrototypeOf or to change the __proto__ property directly result in an error.
Configuration
Property attributes (configurable, writable, ...