Now, if we want to change the
To change this behaviour, we can use Object.freeze() method which freezes an object.
Here the value of
obj_id not changed. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, and writability of existing properties, and also prevents the values of existing properties from being changed.
But, what if we want to change value of the
obj_title from "My Object" to "This Object" which is present inside
obj_name object of weirdObject after using
Yes, we can do that..!! This is called Shallow freeze which only applies to the immediate properties of the object itself and will prevent future property addition, removal or value re-assignment operations only on object. If the value of those properties are objects themselves, those objects are not frozen and may be the target of property addition, removal or value re-assignment operations.
To make object immutable, we will have to add explicit function which will check is wheather the object has another object present inside it.
Thanks for reading and happy coding!