super() on class that extends

Kyle Simpson getify at gmail.com
Fri Apr 10 21:22:52 UTC 2015


Neither the base (parent) nor derived (child) class requires a constructor, nor does the child class require a `super()` call. If you omit either constructor, an assumed one is present. However, if you *do* declare a constructor in a derived class, you'll need to call `super()` in it.

So, to the point of your original question, this is totally valid:

```js
class A {
  foo() { console.log("A:foo"); }
}

class B extends A {
  bar() { super.foo(); }
}

var x = new B();

x.bar(); // A:foo
```

See it in action:

http://babeljs.io/repl/#?experimental=false&evaluate=true&loose=false&spec=false&playground=false&code=class%20A%20%7B%0A%20%20foo()%20%7B%20console.log(%22A%3Afoo%22)%3B%20%7D%0A%7D%0A%0Aclass%20B%20extends%20A%20%7B%0A%20%20bar()%20%7B%20super.foo()%3B%20%7D%0A%7D%0A%0Avar%20x%20%3D%20new%20B()%3B%0A%0Ax.bar()%3B





More information about the es-discuss mailing list