JavaScript getPrototypeOf with Example



In JavaScript, the Object.getPrototypeOf() method is used to get the prototype of a specified object. This method is used to check prototype of user created object and is often used to compare if the two given objects have the same prototype or not.

This method is important for JavaScript's prototype-based inheritance, helping developers understand and work with the prototype chain of objects. In this article, we will look at into the syntax, usage, and examples of getPrototypeOf().

Syntax of the Object.getPrototypeOf() Function

The basic syntax for Object.getPrototypeOf() is as follows:

Object.getPrototypeOf(obj)

obj(parameter): The object whose prototype is to be returned. This parameter is required.

Why to Use Object.getPrototypeOf()

The JavaScript Object.getPrototypeOf() is most useful in the following ways:

  • Debugging: This is used to check the prototype chain of objects when debugging.
  • Inheritance: This method is important for JavaScript's prototype-based inheritance, helping developers understand and work with the prototype chain of objects.
  • Understanding Built-in Types: This method is used to check the prototypes of built-in types like arrays or strings.
  • Modifying Prototypes: This method is used to get a prototype so you can change it and impact all objects that inherit from it.

Example 1

In the following the Object.getPrototypeOf() method is used to get the prototype of a test object on clicking the "click here" button.

<!DOCTYPE html>
<html>
<head>
<title>Document</title>
<style>
    body {
        font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    }
    .sample {
        font-size: 18px;
        font-weight: 500;
    }
</style>
</head>
<body>
    <h1>JavaScript getPrototypeOf()</h1>
    <div class="sample"></div>
    <button class="Btn">CLICK HERE</button>
    <h3>
    Click on the above button to get the prototype of object test
    </h3>
    <script>
        let sampleEle = document.querySelector(".sample");
        function fun(name, age) {
            this.name = name;
            this.age = age;
        }
        document.querySelector(".Btn").addEventListener("click", () => {
            let test = new fun("Rohan", 22);
            sampleEle.innerHTML = "The prototype for test object is = " + Object.getPrototypeOf(test);
        });
    </script>
</body>
</html>

Output


Example 2

In this example, we create a car prototype that has a drive function. Then, we make a myCar object that gets its properties from the car prototype. Finally, we use Object.getPrototypeOf(myCar) to get the prototype and call the drive function

// Define a prototype object
const car = {
    drive: function() {
        console.log("Driving");
    }
};

// Create a specific car object that inherits from car
const myCar = Object.create(car);

// Get the prototype of myCar
const myCarPrototype = Object.getPrototypeOf(myCar);

// Call the drive method from the prototype
    myCarPrototype.drive();

Output

Driving

Conclusion

In this article, we have seen Object.getPrototypeOf() is a useful tool for understanding and working with the prototype chain in JavaScript. By learning how to use this method, developers can manage inheritance and object relationships more effectively in their applications.

Updated on: 2025-03-07T12:57:36+05:30

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements