How to check if a Javascript Class inherits another (without creating an obj)? How to check if a Javascript Class inherits another (without creating an obj)? javascript javascript

How to check if a Javascript Class inherits another (without creating an obj)?


Try the following:

ChildClass.prototype instanceof ParentClass


You can test direct inheritance with

B.prototype.constructor === A

To test indirect inheritance, you may use:

B.prototype instanceof A

(this second solution was first given by Nirvana Tikku)


back to 2017:
check if that work for you

ParentClass.isPrototypeOf(ChildClass)

Alternative if you want protection against shadowing:

const isPrototypeOf = Function.call.bind(Object.prototype.isPrototypeOf);// Usage:isPrototypeOf(ParentClass, ChildClass); // true or false