JS: Object.prototype.isPrototypeOf

JS: Object.prototype.isPrototypeOf


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

var t1 = {"a":1};
var 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

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

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

x.isPrototypeOf(x) return false.

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


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

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon