jskatas.org Continuously Learn JavaScript. Your Way.

Class: extends

How to do inheritance, using extends.

Classes can inherit from another using extends

the default super class is Object

a class A is an instance of Object
let A assert.equal(new A() instanceof Object, true);
when B extends A, B is also instance of Object
class A {} class B {} assert.equal(new B() instanceof A, true); assert.equal(new B() instanceof Object, true);
a class can extend null, and is not an instance of Object
class NullClass extends null { // This is how a class, that extends `null` can be written. constructor() { return Object.create(new.target.prototype); } } let nullInstance = new NullClass(); assert.equal(nullInstance instanceof Object, false);

instance of

when B inherits from A, new B() is also an instance of A
let A; class B extends A {} assert.equal(new B() instanceof A, true);
extend over multiple levels
class A {} class C extends B {} assert.equal(new C instanceof A, true);

Related Katas


Difficulty Level


First Published

17 April 2015


5 tests to solve