JavaScript Object getPrototypeOf() - Get Prototype Object

Updated on November 6, 2024
getPrototypeOf() header image

Introduction

The Object.getPrototypeOf() method in JavaScript is essential for retrieving the prototype (i.e., the internal [[Prototype]]) of a specified object. This method is a part of the standard built-in Object constructor and plays a crucial role in JavaScript's prototype-based inheritance mechanism, wherein an object can inherit properties from another object.

In this article, you will learn how to effectively use the Object.getPrototypeOf() method to access the prototype object of any given object. Discover practical applications and examples showcasing how this method can be used to explore and manipulate prototype chains in JavaScript applications.

Using getPrototypeOf() with Various Objects

Getting the Prototype of a Custom Object

  1. Create a custom object and a constructor function.

  2. Use Object.getPrototypeOf() to retrieve the prototype of an instance of the object.

    javascript
    function Person(name) {
        this.name = name;
    }
    
    const person1 = new Person("Alice");
    const prototypeOfPerson1 = Object.getPrototypeOf(person1);
    console.log(prototypeOfPerson1);
    

    This snippet displays the prototype object of person1, which is an instance of Person. Usually, this is the Person.prototype object.

Exploring the Prototype of Built-in Types

  1. Generate an instance of a built-in type like an Array or Object.

  2. Apply Object.getPrototypeOf() to understand the established prototype for these types.

    javascript
    const arr = [1, 2, 3];
    const prototypeOfArray = Object.getPrototypeOf(arr);
    console.log(prototypeOfArray);
    

    Here, the prototype of an array is usually Array.prototype, from which arrays inherit methods like map, filter, etc.

Modifying Prototype through getPrototypeOf()

  1. Demonstrate how you can modify the prototype object obtained via getPrototypeOf().

  2. Manipulate properties on the prototype to affect all instances sharing it.

    javascript
    function Cat(name) {
        this.name = name;
    }
    
    const cat1 = new Cat("Whiskers");
    const catProto = Object.getPrototypeOf(cat1);
    catProto.speak = function() {
        console.log(`${this.name} says meow!`);
    };
    
    const cat2 = new Cat("Paws");
    cat2.speak();
    

    In this code, by adding a speak method to Cat's prototype, cat2 can utilize this method even though it was added after its creation. This demonstrates the dynamic nature of prototypes in JavaScript.

Corner Cases and Limitations

Accessing null as Prototype

  1. Examine scenarios where the prototype is intentionally set to null.

    javascript
    const objWithNullProto = Object.create(null);
    const proto = Object.getPrototypeOf(objWithNullProto);
    console.log(proto); // Output: null
    

    This example shows that objects can have null as their prototype, indicating no inheritance from Object.prototype.

Non-Object Arguments

  1. Be aware that Object.getPrototypeOf() throws a TypeError if not passed an object.

    javascript
    try {
        const proto = Object.getPrototypeOf("notAnObject");
    } catch (e) {
        console.log(e); // Output: TypeError
    }
    

    This reinforces the requirement that only objects are valid arguments for Object.getPrototypeOf().

Conclusion

The Object.getPrototypeOf() function in JavaScript is a powerful tool for retrieving and manipulating the prototype of objects. It enables developers to access the prototype chain, which is a core concept in JavaScript’s object-oriented design. By understanding how to use this method effectively across various scenarios, you enhance your capability to work with JavaScript's prototypal inheritance, allowing for more dynamic and flexible code. Remember, while prototypes provide significant power, they also introduce complexities that must be managed wisely.