Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make a non Extensible Object to extensible?

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?

like image 335
Edwin Thomas Avatar asked Dec 23 '22 05:12

Edwin Thomas


1 Answers

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);
like image 134
CertainPerformance Avatar answered Dec 25 '22 22:12

CertainPerformance