0

In ES6, the following yields a compilation error:

class A {
  constructor() {
    this.someVal = 10;
  }
}

class B extends A {
  constructor() {
    this.otherVal = 20;
  }
}

The error (as yielded by Babel 6) is:

'this' is not allowed before super()

It is clear to me why super needs to be invoked first, but I do not understand why it needs to be invoked explicitly. Is there a rationale for not simply, as in Java (for example), simply letting super be invoked without arguments if nothing else is specified?

csvan
  • 8,782
  • 12
  • 48
  • 91
  • @SRK I'm not sure that he will. – Omri Aharon Mar 02 '16 at 16:01
  • @SRK thanks, I checked that link, as well as https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super. Neither of them, to the best of my understanding of either, provide an actual *rationale* for why this is so though - they simply state matter-of-factly that this is how things are. I was after the former. – csvan Mar 02 '16 at 16:05
  • @RandoHinn the answer to that question answers this one too, thanks for the reference! – csvan Mar 02 '16 at 16:07
  • Better answer can be found here http://www.2ality.com/2015/02/es6-classes-final.html – Santosh Ram Kunjir Mar 02 '16 at 16:08
  • 1
    Because explicit is better than implicit magic. – Mulan Mar 02 '16 at 16:28
  • 1
    @naomik YMMV, personally I find it perfectly reasonable that the compiler/runtime is allowed to implicitly handle unambigious cases. For example, consider simple inheritance in Java, where there is no need to invoke `super` unless you need to pass arguments to it. If one understands how `super` works in that context, one also knows that there is absolutely no need to call it explicitly - it only makes the code needlessly verbose. – csvan Mar 02 '16 at 16:39
  • Right "if one understands how..." is an endless blackhole. If you approach code design saying "it's reasonable once you understand it", you're going to end up with garbage, imo. Also, Java is one of the most painful languages to develop in. "Needlessly verbose"? Do you also omit semicolons because they're optional? *Once you understand how* automatic semicolon insertion works, there should be almost no reason to use semicolons anymore, right? Why are we typing `this.x` all the time when we could just use coffeescript and type `@x` instead. Aren't we tired of `this`? – Mulan Mar 02 '16 at 16:47
  • Give up struggling for more and more sugar. Implicit APIs aren't clever. They're just obscure and frustrating until you finally see inside the blackbox of truth. – Mulan Mar 02 '16 at 16:51
  • @naomik, IMO less is more - if it is not needed, *and if it does not disrupt the expectations of other devs or the specs of the project I work on*, I prefer not using it. If we talk about Java, I do not use either `this` or `super` if I can avoid it (and I agree that the language itself is a PITA). If I could avoid it in ES6, I would, but I can't as it is required syntax. – csvan Mar 02 '16 at 17:10

0 Answers0