JavaScript: Object.prototype.isPrototypeOf
a.isPrototypeOf(b)
-
Return
true
if a is in prototype chain of b. (if a is b, returnfalse
).
[see Prototype and Inheritance]
// 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 === Reflect.getPrototypeOf(t2) ); // true
// .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
const tt = {}; console.log( tt.isPrototypeOf(tt) ); // false