I'm trying to do hot code reloading with ES6 classes. I need to be able to modify a classes' constructor, without replacing the class with a new one (because other people may have a reference to it).
However, I'm finding that it looks as if the class object has some internal reference to the constructor it was originally defined with; instantiating the class with new
doesn't look up either constructor
or prototype.constructor
on the class.
Example:
class OldC { constructor() { console.log("old"); } }
class NewC { constructor() { console.log("new"); } }
OldC.prototype.constructor = NewC.prototype.constructor;
OldC.constructor = NewC.constructor;
new OldC();
---> "old"
(Updating all the other methods works fine; it's just the constructor I'm having trouble with.)
Thinking that the constructor might be being found via [[prototype]]
, I've also added this:
Object.setPrototypeOf(OldC, Object.getPrototypeOf(NewC));
Object.setPrototypeOf(OldC.prototype, Object.getPrototypeOf(NewC.prototype));
That doesn't help either (and I wouldn't have expected it to given that no subclassing is happening).
After all this, inspecting OldC shows that the prototype properties are exactly as I would expect them to be, with OldC.prototype.constructor
being the new one. But still, constructing an instance of OldC calls the original constructor.
What's going on, and how can I fix it?