There is a way to do this, with your specific example! It is, however, very naughty. Use it in the production code of your choice.
The problem here is not only that non-configurable properties cannot be configurable - this means that when calling Object.defineProperty it checks the existing property of the object, as well as the prototype chain of the object.
And the solution to this problem is quite simple: temporarily get rid of the prototype chain!
Warning: Changing the sequence of prototypes of an existing facility is a dangerous operation and should only be performed under adult supervision. Side effects may include: serious bouts of recompilation, decreased performance of the entire application, and automatic F in most code verification procedures with a sober examiner.
let parent = {}; Object.defineProperty(parent, "property", { get:() => 1, configurable : false }; // here child.property === 1 of course let child = Object.create(parent); // however, if we like abusing our JavaScript engine, we can do this: Object.setPrototypeOf(child, null); Object.defineProperty(child, "property", { get() => 2, configurable : true }); Object.setPrototypeOf(child, parent); //now child.property === 2!
In your case, the property is defined on the Agent prototype, and not on the Agent object. Therefore, you can use this trick. The new property will mask the old and will be used instead. The old property will still exist, it will simply be locked.
Gregros
source share