Search code examples
javascriptinheritanceconstructorprototype-programming

is Object.getPrototypeOf() same as Object.constructor.prototype in Javascript?


Is there a difference between Object.getPrototypeOf(obj) and obj.constructor.prototype? Or are these two referencing the same thing?


Solution

  • NO

    It returns the internal [[Prototype]] value.

    For example:

    var o = Object.create(null);
    Object.getPrototypeOf(o); // null
    o.constructor.prototype; // error
    
    var p = {};
    var o = Object.create(p);
    Object.getPrototypeOf(o); // p
    o.constructor.prototype; // Object.prototype
    

    o.constructor.prototype only works with objects created through new ConstructorFunction or where you have manually set the Prototype.prototype.constructor === Prototype relationship.