Skip to content

Object.getPrototypeOf()

Why Object.getPrototypeOf ?

The Object.getPrototypeOf () method returns the prototype of an independent variable (that is, the value of the internal [[Prototype]] property). Note that this method is a global function and that we need to pass an object to it.

Syntax:

Object.getPrototypeOf(obj)  

  • obj: It is an object whose prototype is to be returned.

Returns:

  • This method returns the prototype of the entered object using Object.getPrototypeof (). If there are no inherited properties, this method returns null.

JavaScript Object.getPrototypeOf Examples

Example 1


        let animals = {
            types: true
        };

        let horse = Object.create(animals);
        
        let arr = Object.getPrototypeOf(horse);

        console.log(arr)
        console.log(Object.setPrototypeOf(arr, {}) );

output:

{types: true}
{types: true}


Example 2


        let myObject = {};

        let obj = Object.create(myObject);

        let arr = Object.getPrototypeOf(obj)

        console.log(arr === myObject)

output:

true


Example 3

const prototype1 = {};  
const object1 = Object.create(prototype1);  
const prototype2 = {};  
const object2 = Object.create(prototype2);  
console.log(Object.getPrototypeOf(object1) === prototype1);  
console.log(Object.getPrototypeOf(object2) === prototype2);   

output:

true
true


Example 4


        let proto = {};
        let obj = Object.create(proto);
        let arr = Object.getPrototypeOf(obj) === proto;
        console.log(arr);

output:

true


Sources


Browser Support

Chrome5
Edgeyes
Firefox3.5
Internet Explorer9
Opera12.1
Safari5
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: