JavaScript: Object.prototype.isPrototypeOf

By Xah Lee. Date: . Last updated: .
root.isPrototypeOf(branch)
Return true if root is in Prototype Chain of branch. Else false.

If root is branch, return false

// example of isPrototypeOf

const j1 = { "j1k": 1 };
const j2 = { "j2k": 1 };

Object.setPrototypeOf(j2, j1);

console.log(j1.isPrototypeOf(j2));
// isPrototypeOf returns true for grand parents too

const t1 = {};
const t2 = Object.create(t1);
const t3 = Object.create(t2);
const t4 = Object.create(t3);

console.log(t1.isPrototypeOf(t4));
// example of isPrototypeOf on same objects
const tt = {};
console.log(tt.isPrototypeOf(tt) === false);

JavaScript Object and Inheritance

BUY ΣJS JavaScript in Depth