JS: Object.prototype.isPrototypeOf

By Xah Lee. Date: . Last updated: .
objA.isPrototypeOf(objB)
  • Return true if objA is in Prototype Chain of objB. Else false.
  • If objA is objB, return false
// example of isPrototypeOf

const xdad = {};
const xson = {};

// make xdad the parent of xson
Object.setPrototypeOf(xson, 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.log(xx.isPrototypeOf(xx) === false);

JavaScript. Object and Inheritance