The `super` keyword doesn't work as it should?
Bergi
a.d.bergi at web.de
Mon Jul 18 21:46:22 UTC 2016
/#!/JoePea wrote:
> Why can't `super` simply be a shortcut
> for "look up the prototype of the object that the method is called on, then
> find the `.constructor` property and call it on `this`"? That seems to be
> simple.
Simple, yes, and broken in the case of multi-level inheritance:
```
const x = Object.assign(Object.create({
method() {
console.log("parent");
}
}), {
method() {
console.log("child");
Object.getPrototypeOf(this).method(); // super.method()
}
});
x.method(); // works as expected
const y = Object.create(x);
y.method(); // infinite loop/stack overflow
```
A `super` query must not depend on `this` (only), it must statically
resolve the object on which the called method is defined.
In constructors, using the prototype of the currenctly called
constructor for `super()` works well, but you'd need to use
`Object.setPrototype` as there is currently no declarative way other
than `class`es to define functions with custom prototypes.
In methods, there would need to be a way to populate the [[HomeObject]]
other than declaring the method as part of a class/object literal.
Kind regards,
Bergi
More information about the es-discuss
mailing list