

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it possible to make a nested object immutable using Object.freeze() in JavaScript?
Object.freeze() method can make an object immutable but in case of a nested object, it can't prevent mutability. Object.freeze() can only provide immutability to the outside parent object, it can't access any inner child(nested) objects.
Example
In the following example, there are no nested objects. Object.freeze() method will try to freeze the whole object. Therefore there is no change in the property "name" even after freezing the object.
<html> <body> <script> const person = { "name" : "Suresh", "Country" : "India", "Designation" : "Mayor" } document.write("Before freeze the name is" +" "+ person.name ); document.write("</br>"); Object.freeze(person); person.name = "John"; document.write("After freeze the name is" +" "+ person.name); </script> </body> </html>
Output
Before freeze the name is Suresh After freeze the name is Suresh
When a nested object is tried to frozen by Object.freeze() the result is in vain because Object.freeze() can't access nested objects.
Example
In the following example, even though the object is frozen by Object.freeze() method the property of a nested object is changed.
<html> <body> <script> const person = { "name" : "Suresh", "Country" : "India", "Designation" : "Mayor", "CompaniesOwned" :{ "Company1" : "Tesla", "Company2" : "SolarCity", "Company3" : "Spacex" } } document.write("Before freeze " + " " + "Company2 is" + " "+ person.CompaniesOwned.Company2); Object.freeze(person); document.write("</br>"); person.CompaniesOwned.Company2 = "Neuralica"; document.write("After freeze" + " " + "Company2 is" + " "+ person.CompaniesOwned.Company2); </script> </body> </html>
Output
Before freeze Company2 is SolarCity After freeze Company2 is Neuralica
- Related Questions & Answers
- How to freeze an Object in JavaScript?
- Is it possible to display substring from object entries in JavaScript?
- Is it possible to change directory by using File object in Java?
- Filter nested object by keys using JavaScript
- Constructing a nested JSON object in JavaScript
- Print JSON nested object in JavaScript?
- Sum of nested object values in Array using JavaScript
- Recursively list nested object keys JavaScript
- How to convert square bracket object keys into nested object in JavaScript?
- How to modify properties of a nested object in JavaScript?
- Extract key value from a nested object in JavaScript?
- List all duplicate values in a nested JavaScript object
- Is it possible to exclude nested fields in MongoDB with a wildcard?
- Is it possible to make a case-insensitive query in MongoDB?
- Difference between mutable and immutable object
Advertisements