In the JavaScript world we already know how the classes and objects are important. Among the Object constructor methods, the Object.freeze() method helps to freeze an object or arrays. Freezing an object does not allow to change any properties from the object. It returns the passed object and does not create a frozen copy.
Frozen object can be prevents from:
- Adding new properties to the object.
- Removing or altering existing properties from the object.
- Preserves the enumerability, configurability, writability and the prototype of the object
- Changing values of the existing object properties and prototype.
The syntax is:
Object.freeze(obj)
where the obj is the object which has to be freezed.
Example:
Input : const obj = { name: 'initial_name'};
const newObj = Object.freeze(obj);
newObj.name = 'new_name';
console.log(newObj.name);
Output : "initial_name"
Top comments (1)
nice one !!