jskatas.org Continuously Learn JavaScript. Your Way.

Class: more extends

More in depth extends stuff

Classes can inherit from another

extend an old style "class", a function, still works
let A; class B extends A {} assert.equal(new B() instanceof A, true);

prototypes are as you know them

A is the prototype of B
class A {} class B extends A {} const isIt = A.isPrototypeOf(null); assert.equal(isIt, true);
As prototype is also Bs prototype
class A {} class B extends A {} const proto = B; // Remember: don't touch the assert!!! :) assert.equal(A.prototype.isPrototypeOf(proto), true);

extends using an expression

e.g. the inline assignment of the parent class
let A; class B extends (A = {}) {} assert.equal(new B() instanceof A, true);
or calling a function that returns the parent class
const returnParent = (beNull) => beNull ? null : class {}; class B extends returnParent {} assert.equal(Object.getPrototypeOf(B.prototype), null);

Related Katas

Class

Difficulty Level

ADVANCED

First Published

20 April 2015

Stats

5 tests to solve