revision:
The isPrototypeOf() method checks if an object exists in another object's prototype chain..
Note: "isPrototypeOf()"" differs from the "instanceof" operator. In the expression "object instanceof AFunction", object's prototype chain is checked against "AFunction.prototype", not against "AFunction" itself.
isPrototypeOf(object)
Parameters:
object : required. The object whose prototype chain will be searched.
function Foo() {} function Bar() {} Bar.prototype = Object.create(Foo.prototype); const bar = new Bar(); console.log(Foo.prototype.isPrototypeOf(bar)); // Expected output: true console.log(Bar.prototype.isPrototypeOf(bar)); // Expected output: true
<div> <p id="proto-1"></p> <p id="proto-2"></p> <p id="proto-3"></p> <p id="proto-4"></p> <p id="proto-5"></p> <p id="proto-6"></p> <p id="proto-7"></p> <p id="proto-8"></p> </div> <script> class Foo {} class Bar extends Foo {} class Baz extends Bar {} const foo = new Foo(); const bar = new Bar(); const baz = new Baz(); // prototype chains: // foo: Foo --> Object // bar: Bar --> Foo --> Object // baz: Baz --> Bar --> Foo --> Object console.log(Baz.prototype.isPrototypeOf(baz)); // true document.getElementById("proto-1").innerHTML = "is prototype ? : " + Baz.prototype.isPrototypeOf(baz); console.log(Baz.prototype.isPrototypeOf(bar)); // false document.getElementById("proto-2").innerHTML = "is prototype ? : " + Baz.prototype.isPrototypeOf(bar); console.log(Baz.prototype.isPrototypeOf(foo)); // false document.getElementById("proto-3").innerHTML = "is prototype ? : " + Baz.prototype.isPrototypeOf(foo); console.log(Bar.prototype.isPrototypeOf(baz)); // true document.getElementById("proto-4").innerHTML = "is prototype ? : " + Bar.prototype.isPrototypeOf(baz); console.log(Bar.prototype.isPrototypeOf(foo)); // false document.getElementById("proto-5").innerHTML = "is prototype ? : " + Bar.prototype.isPrototypeOf(foo); console.log(Foo.prototype.isPrototypeOf(baz)); // true document.getElementById("proto-6").innerHTML = "is prototype ? : " + Foo.prototype.isPrototypeOf(baz); console.log(Foo.prototype.isPrototypeOf(bar)); // true document.getElementById("proto-7").innerHTML = "is prototype ? : " + Foo.prototype.isPrototypeOf(bar); console.log(Object.prototype.isPrototypeOf(baz)); // true document.getElementById("proto-8").innerHTML = "is prototype ? : " + Object.prototype.isPrototypeOf(baz); </script>
<div> <p id="proto-9"></p> <p id="proto-10"></p> <p id="proto-11"></p> </div> <script> function obj1() { } function obj2() { } obj1.prototype = Object.create(obj2.prototype); const obj3 = new obj1(); console.log(obj1.prototype.isPrototypeOf(obj3)); console.log(obj2.prototype.isPrototypeOf(obj3)); document.getElementById("proto-9").innerHTML = "is prototype ? : " + obj1.prototype.isPrototypeOf(obj3); document.getElementById("proto-10").innerHTML = "is prototype ? : " + obj2.prototype.isPrototypeOf(obj3); </script>