Is there a method to implement partial immutability on an object in a way that triggers an error if any attempt at mutation is made?
For instance, consider let obj = {a: 1, b: 2}
. I am interested in making obj.a
and obj.b
immutable while still allowing additional keys to be added to the object, such as obj.c = 3
.
One approach I contemplated was nesting properties within sub-objects and utilizing Object.freeze
like this:
let obj = {subObj:{a: 1, b:2}};
Object.freeze(obj.subObj);
However, it seems that subsequent changes fail silently. For example, obj.subObj.a = 3
does not alter a
, nor does it raise any indication of an issue. Is there a way to ensure an error notification is triggered instead?