JS: Object.prototype.isPrototypeOf

JS: Object.prototype.isPrototypeOf

a.isPrototypeOf(b)

Return true if a is in prototype chain of b. (if a is b, return false)

[see JS: Get/Set Prototype]

// example of using isPrototypeOf() method

const t1 = {"a":1};
const t2 =  Object.create(t1);    // t2's parent is t1

console.log( t1.isPrototypeOf(t2) ); // true

console.log( t1 === Object.getPrototypeOf(t2) ); // true

a.isPrototypeOf(b) return true even if a is 2 or more levels above b, not just direct parent.

// .isPrototypeOf() returns true for grand parents too

const t1 = {};                    // you
const t2 = Object.create(t1);    // child of t1
const t3 = Object.create(t2);    // grand child of t1
const t4 = Object.create(t3);    // great grand child of t1

console.log( t1.isPrototypeOf(t4) ); // true

x.isPrototypeOf(x) return false.

const tt = {};
console.log( tt.isPrototypeOf(tt) ); // false

Reference

ECMAScript® 2016 Language Specification#sec-object.prototype.isprototypeof

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Patreon me $5. Ask me question on patreon