Reputation: 357
I understand that classes in ES6 are really syntactic sugar. Is the super() call really just calling proto? (Is it mapped to the [[prototype]] object?)
Upvotes: 0
Views: 83
Reputation: 665040
It's a bit more than that. It also remembers where the method was defined.
const example = {
method() {
return super.method();
}
}
is syntactic sugar for
const example = {
method() {
return Object.getPrototypeOf(example).method.call(this);
}
}
and
class Example {
method() {
return super.method();
}
}
is syntactic sugar for
class Example {
method() {
return Object.getPrototypeOf(Example.prototype).method.call(this);
}
}
As for super()
calls in constructors, it similarly uses Object.getPrototypeOf
on the constructor, but does a bit more there, so
class Example extends Parent {
constructor() {
super();
}
}
is doing
class Example extends Parent {
constructor() {
this = Reflect.construct(Object.getPrototypeOf(Example), [], new.target);
}
}
Is it mapped to the [[prototype]] object?
Yes. Not to the [[prototype]] of the object that the function was called on (this
), but to the [[prototype]] of the object that the function was defined in.
Upvotes: 4