Search code examples
javascriptecmascript-6prototype

Why Object.getPrototypeOf(instance.constructor) is not equal to instance.constructor.prototype?


The title says its all, but here's the code:

class A {}
class B extends A {}

const b = new B()


Object.getPrototypeOf(b.constructor) === b.constructor.prototype // false

Object.getPrototypeOf(b.constructor) === A // true
b.constructor.prototype === A // false. why?

I mean, the code above is counter-intuitive. Why is it made this way?


Solution

  • __proto__ and prototype is not the same.

    Let's say you have an object obj. Now, obj.prototype won't actually provide you with the prototype of obj, as you expect. To get the prototype of obj you have to do obj.__proto__. Run the following code and you'll have your answer. Also, Read this to know more about the differences between __proto__ and prototype. :)

    class A {}
    class B extends A {}
    
    const b = new B()
    
    console.log(Object.getPrototypeOf(b.constructor) === b.constructor.__proto__) // true
    console.log(Object.getPrototypeOf(b.constructor) === A) // true
    console.log(b.constructor.__proto__ === A) // true