In the following code I have an unexpected behaviour for me. I was waited to see the method A2::method() to be called, but it is A0::method() instead. More tests showed me that if A1 returns something, it will keep public prop from A2 but forget all methods from A2. Maybe I'm doing something wrong. Is there a workaround
class A0 {
method() {
return "from A0 " + this.prop
}
}
class A1 {
constructor() {
return new A0;
}
}
class A2 extends A1 {
constructor(v) {
super()
console.log('here')
this.prop = v;
}
prop = 42;
method() {
return "from A2 " + this.prop
}
}
const a = new A2(42);
console.log(a.prop, a.method())
Result
[LOG]: "here"
[LOG]: 42, "from A0 42"
Expected
[LOG]: "here"
[LOG]: 42, "from A2 42"
As the documentation says:
In your example, the object you have created is not an actual instance of
A1orA2In other terms, you have created an object that does not have
A1orA2in its prototype chain, but theA2constructor still attachedpropto it.