Search code examples
javascriptobjectprototypeecmascript-5

Prototypes and __proto__ in JavaScript


Object is a function Object.prototype is an object whose constructor is Object itself.

But what is Object()?

can someone explain why this statement outputs true

Object.prototype.__proto__ === Object().__proto__.__proto__

Solution

  • But what is Object()?

    Evaluating Object() produces a new empty object based on the Object prototype.

    can some explain why this statement outputs true

    Object.prototype.__proto__ === Object().__proto__.__proto__

    Object() produces a new object which is based on the Object prototype. For an object created from a particular prototype, the __proto__ is a reference to that prototype.

    Therefore, it follows that Object.prototype and Object().__proto__ both reference the same value:

    console.log(Object.prototype === Object().__proto__)

    Since these both refer to the same thing, it also follows that your equality expression above is true. (incidentally Object.prototype.__proto__ is null, so it would also be equal to any other null value).