0

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?)

MsGirlPerl
  • 337
  • 1
  • 4
  • 11
  • https://stackoverflow.com/questions/8765242/how-why-to-use-super-in-code – Ele Apr 13 '18 at 17:10
  • https://stackoverflow.com/questions/38034059/javascript-whats-the-difference-between-call-and-super – Ele Apr 13 '18 at 17:10
  • [Google search: what is super keyword in js](https://www.google.com/search?q=what+is+super+keyword+in+js+site:stackoverflow.com&client=firefox-b-ab&sa=X&ved=0ahUKEwjFyf723rfaAhUBmJAKHbwoAcgQrQIITygEMAI&biw=1366&bih=664) – Ele Apr 13 '18 at 17:10
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super – Scott Marcus Apr 13 '18 at 17:24
  • No, it is not calling `__proto__`. That's a deprecated getter/setter and should not be used by anything. – Bergi Apr 13 '18 at 17:28

1 Answers1

3

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.

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375