admin管理员组

文章数量:1419624

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();

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?

Share Improve this question edited Mar 18, 2019 at 21:29 David Given asked Mar 18, 2019 at 21:20 David GivenDavid Given 13.7k10 gold badges81 silver badges133 bronze badges 7
  • What is OldObject in your example? "instantiating the class with new doesn't look up either constructor or prototype.constructor on the class." No, because you use new on a function object and it's the function object itself that is invoked. – Felix Kling Commented Mar 18, 2019 at 21:23
  • Sorry, typo. Fixed. – David Given Commented Mar 18, 2019 at 21:28
  • "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)." I think the only way is to have the constructor itself delegate to another function which you can replace. – Felix Kling Commented Mar 18, 2019 at 21:28
  • 1 The question boils down to: "can I mutate a function". See this answer – trincot Commented Mar 18, 2019 at 21:29
  • In JS, a "class" isn't its own thing. It's just syntax sugar for creating a constructor function. So the .constructor of both "classes" is Function. – ziggy wiggy Commented Mar 18, 2019 at 21:32
 |  Show 2 more ments

1 Answer 1

Reset to default 4

Still, constructing an instance of OldC calls the original constructor.

Yes, because OldC itself is the constructor function. You need to overwrite the OldC variable to change what new OldC does.

Modify a classes' constructor, without replacing the class with a new one (because other people may have a reference to it).

As @trincot pointed out in the ments, you cannot modify the code of a function. You must replace the constructor with a new one, there's no way around it.

You can keep the prototype object though (which is mutable), as this is what most other things (especially the old instances) will reference.

NewC.prototype = OldC.prototype;
NewC.prototype.constructor = NewC;
OldC = NewC;

People who took a reference to the old constructor that cannot be changed now can't be helped. Your best bet is not handing out the class itself at all, but only a factory whose behaviour your update code knows to modify.

本文标签: javascriptHow to modify the constructor of an ES6 classStack Overflow