Is there a way to enforce partial immutability of an object that throws an error if someone tries to change it?
For example, let obj = {a: 1, b: 2}and I want to obj.aand obj.bhave the same, but that still lets you add additional keys to obj, that is, to resolve obj.c = 3.
I thought about nesting properties in sub-objects and used Object.freezeas follows:
let obj = {subObj:{a: 1, b:2}};
Object.freeze(obj.subObj);
But it seems that after that he fails, i.e. obj.subObj.a = 3does not mutate a, but also does not give any indication of a problem. Is there any way to make it throw an error?
source
share