Object.prototype.isPrototypeOf()

Why Object.prototype.isPrototypeOf ?

The isPrototypeOf () method checks whether an object exists in another object hierarchy. This returns boolean values. If true returns, that object is in control of the specified object. If False returns, that object is not in control of the specified object.

object.isPrototypeOf( obj )

  • obj:The object prototype to be checked against Object.
  • object: The object chain to be checked in for the prototype.

returns:

  • true if object is the prototype of ofalse if o is not an object or if object is not the prototype of o.

JavaScript Object.prototype.isPrototypeOf Examples

Example 1


        function myObject1() { }
        function myObject2() { }

        myObject1.prototype = Object.create(myObject2.prototype);

        let myObject3 = new myObject1();
        console.log(myObject1.prototype.isPrototypeOf(myObject3));
        console.log(myObject2.prototype.isPrototypeOf(myObject3));

output:

true
true


Example 2

        function per1() { };
        function per2() { };
        function per3() { };

        per1.prototype = Object.create(per2.prototype);
        per2.prototype = Object.create(per1.prototype);
        var MyObject = new per3();

        console.log(per3.prototype.isPrototypeOf(per3));
        console.log(per1.prototype.isPrototypeOf(per3));
        console.log(per2.prototype.isPrototypeOf(per3));
        console.log(Object.prototype.isPrototypeOf(per1));

output:

false
false
false
true


Sources:


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started