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

JavascriptClassInheritance

Javascript Problem Overview


Eg:

function A(){}
function B(){}
B.prototype = new A();

How can I check if the class B inherits class A?

Javascript Solutions


Solution 1 - Javascript

Try the following:

ChildClass.prototype instanceof ParentClass

Solution 2 - Javascript

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)

Solution 3 - Javascript

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

Solution 4 - Javascript

Gotchas: Note that instanceof does not work as expected if you use multiple execution contexts/windows. See §§.


Also, per https://johnresig.com/blog/objectgetprototypeof/, this is an alternative implementation that is identical to instanceof:

function f(_, C) { // instanceof Polyfill
  while (_ != null) {
    if (_ == C.prototype)
      return true;
    _ = _.__proto__;
  }
  return false;
}

Modifying it to check the class directly gives us:

function f(ChildClass, ParentClass) {
  _ = ChildClass.prototype;
  while (_ != null) {
    if (_ == C.prototype)
      return true;
    _ = _.__proto__;
  }
  return false;
}


Sidenote

instanceof itself checks if obj.proto is f.prototype, thus:

function A(){};
A.prototype = Array.prototype;
[]instanceof Array // true

and:

function A(){}
_ = new A();
// then change prototype:
A.prototype = [];
/*false:*/ _ instanceof A
// then change back:
A.prototype = _.__proto__
_ instanceof A //true

and:

function A(){}; function B(){};
B.prototype=Object.prototype;
/*true:*/ new A()instanceof B 

If it's not equal, proto is swapped with proto of proto in the check, then proto of proto of proto, and so on. Thus:

function A(){}; _ = new A()
_.__proto__.__proto__ = Array.prototype
g instanceof Array //true

and:

function A(){}
A.prototype.__proto__ = Array.prototype
g instanceof Array //true

and:

f=()=>{};
f.prototype=Element.prototype
document.documentElement instanceof f //true
document.documentElement.__proto__.__proto__=[];
document.documentElement instanceof f //false

Solution 5 - Javascript

I do not think Simon meant B.prototype = new A() in his question, because this is certainly not the way to chain prototypes in JavaScript.

Assuming B extends A, use Object.prototype.isPrototypeOf.call(A.prototype, B.prototype)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimonView Question on Stackoverflow
Solution 1 - JavascriptNirvana TikkuView Answer on Stackoverflow
Solution 2 - JavascriptDenys SéguretView Answer on Stackoverflow
Solution 3 - Javascriptpery mimonView Answer on Stackoverflow
Solution 4 - JavascriptPacerierView Answer on Stackoverflow
Solution 5 - JavascriptjlchereauView Answer on Stackoverflow