JavaScript Object freeze() Method
The Object.freeze() method is used to freeze an object. Freezing an object does not allow new properties to be added to the object and prevents removing or altering the existing properties.
Object.freeze() preserves the enumerability, Configurability, writability, and prototype of the object. It returns the passed object and does not create a frozen copy.
Syntax:
Object.freeze(obj)
Parameters:
- obj: It is the object which has to be frozen.
Return Value:
- Object.freeze() returns the object that was passed to the function.
Example 1: In this example, the object “obj2” has been assigned property from object “obj1”, and the properties of “obj1” are frozen therefore new properties and values are prevented from being added to “obj2”.
// Creating an object constructor and assigning values to it
const obj1 = { property1: 'initial_data' };
// Creating a second object which will freeze
// the properties of the first object
const obj2 = Object.freeze(obj1);
// Updating the properties of the frozen object
obj2.property1 = 'new_data';
// Displaying the properties of the frozen object
console.log(obj2.property1);
Output :
"initial_data"
Example 2: In this example, the object “obj” has been assigned “prop: function” which has been later deleted since the object “obj wasn’t frozen. After that, a new object “o” has been assigned the frozen values of “obj” which prevented it from further updations.
// Creating an object constructor and assigning values to it
let obj = { prop: function () { }, name: 'adam' };
// Displaying the properties of the object created
console.log(obj);
// Updating the properties of the object
obj.name = 'billy';
delete obj.prop;
// Displaying the updated properties of the object
console.log(obj);
// Freezing the object using object.freeze() method
let o = Object.freeze(obj);
// Updating the properties of the frozen object
obj.name = 'chris';
// Displaying the properties of the frozen object -->
console.log(obj);
Output :
Object { prop: function () {}, name: "adam" }
Object { name: "billy" }
Object { name: "billy" }
Applications:
- Object.freeze() is used for freezing objects and arrays, o make an object immutable.
We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.
Supported Browsers:
- Google Chrome
- Mozilla
- Opera
- Safari
JavaScript Object freeze() Method- FAQs
What happens if you try to modify a frozen object?
In strict mode, attempting to modify a frozen object throws a TypeError. In non-strict mode, the operation fails silently without throwing an error.
How does Object.freeze() interact with prototypes?
Freezing an object does not affect its prototype chain. The object’s prototype can still be modified unless the prototype itself is also frozen.
How does Object.freeze() handle non-enumerable properties?
Object.freeze() affects all properties of the object, including non-enumerable ones. All properties become immutable and cannot be reconfigured.
Does Object.freeze() work with Proxy objects?
When used with Proxy objects, Object.freeze() can prevent certain changes to the target object. However, the proxy itself can still intercept and handle operations unless it is also frozen.
What is the most common use of the Object.freeze()
method?
- Ensuring objects remain immutable throughout the application’s lifecycle
- Preventing unintended modifications to objects
- Maintaining a consistent state in the application
- Making configurations or constant data read-only