I need to alter an object which is non extensible. Is there any work around that we can alter this object property ?
I have read the Documentation that says so "There is no way to make an object extensible again once it has been made non-extensible."
Is there any workarounds ? Like Duplicating the object or something?
Another possibility, in addition to duplicating the object, is to create a new object whose prototype is the non-extensible object:
const object1 = {
foo: 'foo',
};
Object.preventExtensions(object1);
// We can't assign new properties to object1 ever again, but:
const object2 = Object.create(object1);
object2.bar = 'bar';
console.log(object2);
/* This will create a property *directly on* object2, so that
`object2.foo` refers to the property on object2,
rather than falling back to the prototype's "foo" property: */
object2.foo = 'foo 2';
console.log(object2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With