1
class Overflow {}
class Stack extends Overflow {
  constructor() {
    super();
  }
}

let stack = new Stack();

https://plnkr.co/edit/JqRfuDAav9opvapwCGpT?p=preview

If I use construtor() I must call super() always.

Why isn't super() auto called in constructor?

Edit: Is super() here calling the base constructor AND setting the prototype? Seems wrong. Edit: Why is super() required? Even when I've no intention of calling the base constructor.

16
  • "Why isn't super() auto called in constructor?" Which arguments should be passed to it? None, all or a subset? Any decision would probably lead to a discussion why that approach was chosen and not another... May 10, 2016 at 18:13
  • The call to super() is required always. Try to write a valid ES6 constructor without super().
    – old
    May 10, 2016 at 18:24
  • I know... I don't understand what you are trying to tell me with your comment. May 10, 2016 at 18:25
  • "Why is super() required? Even when I've no intention of calling the base constructor." Because this needs to be initialized properly (i.e. the object generated by calling the constructor needs to be constructed properly). That is primarily a concern for extending exotic objects / classes, such as Array. May 10, 2016 at 18:26
  • Related: esdiscuss.org/topic/… May 10, 2016 at 18:32

2 Answers 2

4

Why isn't super() auto called in constructor?

So you can decide where in the constructor to call it. This is perfectly valid, for instance:

constructor(foo) {
    let bar;
    if (foo) {
        bar = /*...*/;
    } else {
        bar = /*...*/;
    }
    super(bar);
}

...provided no code prior to the call to super uses this (or super.xyz).

Yes, it would have been possible to define the language such that if you didn't have any call to super in your constructor at all, it was added automatically for you at the beginning (a'la Java), but they just decided not to do it that way.

Is super() here calling the base constructor AND setting the prototype? Seems wrong.

No, it's calling the base constructor. The prototype was set before your constructor was called. It's exactly like this old ES5 code:

function Derived() {
    Base.call(this);
}

...in:

function Base() {
}

function Derived() {
    Base.call(this);
}
Derived.prototype = Object.create(Base.prototype);
Derived.prototype.constructor = Derived;

Why is super() required? Even when I've no intention of calling the base constructor.

If you have no intention of calling the base constructor, then your subclass isn't a subclass and shouldn't be extending the base. In a strictly-typed language you'd probably make the base (or a subset of the base) an interface and have your class implement that rather than subclassing. In JavaScript, no need, just make it quack like the duck you want it to be. If an instanceof relationship is important, create a new base that will act like an interface but doesn't do anything anywhere, and have the old base and your class subclass it directly.

4
  • Is super() here calling the base constructor() AND setting the prototype? Seems wrong that I'm required to call a super() I didn't asked.
    – old
    May 10, 2016 at 17:19
  • Perhaps my question is more towards to why calling the super() is required.
    – old
    May 10, 2016 at 17:36
  • It isn't strictly required. You could also return something, so if it auto-added super() at the beginning, you'd be doing extra work. It's also not quite exactly like the ES5 code though, as this is uninitialized until you've called super() so there isn't even an object to have a prototype yet. May 10, 2016 at 17:56
  • @old: It is required to correctly initialize this. If you think it should be called implicitly, which arguments should be passed to it? May 10, 2016 at 18:11
1

Axel FTW http://www.2ality.com/2015/02/es6-classes-final.html

Summary

Why super is required?

  • The new keyword class isn't just syntax sugar.
  • Allocation and instantiation only happens in the base constructor.

But why this?

  • To allow exotic objects to be extended (thank you Felix Kling).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.