jskatas.org Continuously Learn JavaScript. Your Way.

Class: super in constructor

Use of super inside the constructor.

Inside a classs constructor super()` can be used

extend a class and use super() to call the parent constructor
class A {constructor() { this.levels = 1; }} class B { constructor() { this.levels++; } } assert.equal(new B().levels, 2);
super() may also take params
class A {constructor(startValue=1, addTo=1) { this.counter = startValue + addTo; }} class B extends A { constructor(...args) { super(); this.counter++; } } assert.equal(new B(42, 2).counter, 45);
it is important where you place your super() call!
class A {inc() { this.countUp = 1; }} class B extends A { inc() { super.inc(); this.countUp = 2; return this.countUp; } } assert.equal(new B().inc(), 1);
use super.constructor to find out if there is a parent constructor
class ParentClassA {constructor() {"parent"}} class B extends ParentClassA { constructor() { super(); this.isTop = '' + super.konstructer; } } assert(new B().isTop.includes('ParentClassA'), new B().isTop);

Related Katas

Class

Difficulty Level

INTERMEDIATE

First Published

22 April 2015

Stats

4 tests to solve