jskatas.org Continuously Learn JavaScript. Your Way.

Class: super in method

Use of super inside a method.

Inside a class use super to access parent methods

use of super without extends fails (already when transpiling)
class A {hasSuper() { return super; }} assert.equal(new A().hasSuper(), false);
super with extends calls the method of the given name of the parent class
class A {hasSuper() { return true; }} class B extends A {hasSuper() { return super.hasSuper; }} assert.equal(new B().hasSuper(), true);
when overridden a method does NOT automatically call its super method
class A {hasSuper() { return true; }} class B extends A {hasSuper() { return 'nothing'; }} assert.equal(new B().hasSuper(), void 0);
super works across any number of levels of inheritance
class A {iAmSuper() { return true; }} class B extends A {} class C extends B {iAmSuper() { return iAmSuper(); }} assert.equal(new C().iAmSuper(), true);
accessing an undefined member of the parent class returns undefined
class A {} class B extends A {getMethod() { return super.constructor; }} assert.equal(new B().getMethod(), void 0);

Related Katas

Class

Difficulty Level

INTERMEDIATE

First Published

21 April 2015

Stats

5 tests to solve