I wanted to comment on this old question, but it appears to be locked.
Here is my use case:
- An object
obj
is created with constructorBase
.obj instanceof Base
returns true. - I want to change the prototype of
obj
such that it appears as ifobj
was constructed fromDerived
. That is, I wantobj
to get access to the methods ofDerived
obj instanceof Derived
to return true
The reason is that obj
is to have a type in the hierarchy that is unknown at the time of its creation and determined by what happens after. I want to be able to move it down the hierarchy.
I believe I can do this with
obj.__proto__ = Derived.prototype;
but __proto__
will be deprecated in the next version of JavaScript. The proxies API, which has changed since the question I linked above was asked, does not seem to support my use case.
Is there an alternative implementation for my use case that exists now or is planned for the future?
The only alternative I can see right now is to use
obj2 = Object.create(Derived.prototype);
obj2.extend(obj);
and never store more than one reference to obj, but that cost is quite an inconvenience.
Here is a fiddle demonstrating the issue.