skip to Main Content

I am able to access Object.getPrototypeOf method, but why is the same not available for instantiated objects?

const obj = {}
obj.getPrototypeOf // is not a function

3

Answers


  1. Because it’s a static method (it’s meant to be used on a class, not an instance)

    Example:

    class X {
      static getX() {}
    }
    
    const x = new X()
    // works
    X.getX()
    // throws
    x.getX()
    Login or Signup to reply.
  2. It’s a static method, you can’t use it on an instance.

    Try instead:

    const myObj = Object.create({})
    
    console.log(Object.getPrototypeOf(myObj))
    Login or Signup to reply.
  3. The main reason, as others have noted, is that it’s a static method on the Object constructor, not an instance or prototype function. So the correct way to use it is const proto = Object.getPrototypeOf(theObject);. But read on for why you might not have wanted to use an object method for this even if it did have one.

    Objects do have a __proto__ accessor property but using it is a Bad Idea™ because A) The object may not have it,¹ and B) Even if the object does have it, it may lie to you, just as it can with the prototype method hasOwnProperty that checks to see if the object has an "own" property with a given name. That opportunity for the object to lie is one motivation for moving away from putting these things on the prototype (along with avoiding web compatibility problems and improving JavaScript engine performance). Recently, a new static method on Object, Object.hasOwn, was added to replace the prototype function hasOwnProperty specifically because of this issue. Like getPrototypeOf, it’s a static method on Object, not a prototype method, and it won’t lie to you. (Proxy objects notwithstanding.)

    So in those rare situations you need to get the prototype of an object, just do it via Object.getPrototypeOf(theObject).


    Example of objects with no __proto__ accessor property:

    // A basic example of an object with no __proto__ accessor property:
    const o1 = Object.create(null);
    console.log("__proto__" in o1);              // false
    console.log(o1.__proto__);                   // undefined
    console.log(Object.hasOwn(o1, "__proto__")); // false
    console.log(o1.__proto__);                   // undefined
    
    // Here's another one, and this one has a prototype
    const o2 = Object.create(
        Object.assign(
            Object.create(null),
            {
                method() { console.log("Hi there"); }
            }
        )
    );
    console.log("__proto__" in o2);              // false
    console.log(o2.__proto__);                   // undefined
    console.log(Object.hasOwn(o2, "__proto__")); // false
    console.log(Object.hasOwn(o2, "method"));    // false
    console.log("method" in o2);                 // true
    o2.method();                                 // "Hi there"
    .as-console-wrapper {
        max-height: 100% !important;
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search