JS: Object .prototype .isPrototypeOf

By Xah Lee. Date: . Last updated: .
A.isPrototypeOf(B)
  • Return true if A is in Prototype Chain of B. Else false.
  • If A is B, return false
// example of isPrototypeOf
const xdad = {};
const xson = Object.create(xdad);
console.assert(xdad.isPrototypeOf(xson));
// isPrototypeOf returns true for grand parents too
const xgen1 = {};
const xgen2 = Object.create(xgen1);
const xgen3 = Object.create(xgen2);
const xgen4 = Object.create(xgen3);
console.assert(xgen1.isPrototypeOf(xgen4));
// example of isPrototypeOf on same objects
const xx = {};
console.assert(xx.isPrototypeOf(xx) === false);