0
var i = 0;
i.__proto__

Why do I see extra Object in prototype chain for Number object?

When I try to check i.__proto__.__proto__.__proto__ is null but devtools screenshot below its of type Object.

enter image description here

  • It gets wrapped in an object because you tried to access a property of it, and `Number.prototoype` inherits from `Object.prototype` – CertainPerformance Mar 06 '22 at 20:30
  • It's called a **wrapper type**. – connexo Mar 06 '22 at 20:32
  • @CertainPerformance could you please tell me more on this or share some resources to go through. – JavaDeveloper Mar 06 '22 at 20:42
  • @connexo could you please tell me more on this or share some resources to go through. – JavaDeveloper Mar 06 '22 at 20:43
  • 2
    Did you read the linked canonical, which goes into great detail? – CertainPerformance Mar 06 '22 at 20:43
  • @CertainPerformance I checked https://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object. I am not getting "It gets wrapped in an object because you tried to access a property of it". When I try to check i.__proto__.__proto__.__proto__ is null but dev tools screenshot shows its of type Object. – JavaDeveloper Mar 06 '22 at 20:50
  • 1
    No, `i.__proto__.__proto__ === Object.prototype`, so its own prototype should really be `null`. Your screenshot is looking at the `prototype` property, not the `__proto__` property – CertainPerformance Mar 06 '22 at 20:52
  • @CertainPerformance Right, that is also my understadning. Is there issue with chrome devtools? Why is displaying `prototype` of `Object` instead of `null` when I click on `(...)` to expand `__proto__` of `Object`. Or I am missing something? – JavaDeveloper Mar 06 '22 at 20:59
  • 1
    @JavaDeveloper I guess it evaluates the `__proto__` getter on the originally logged value, i.e. `Number.prototype`, and you get `Object.prototype`. Notice it has a `get __proto__: (...)` again, it'll be the same there. In conclusion: don't use the deprecated `__proto__` getter for anything, just look at the `[[Prototype]]` in the devtools and use `Object.getPrototypeOf` to access it in code. – Bergi Mar 06 '22 at 21:33
  • @Bergi Yes looks like that. I observed same thing in other objects also. Thanks. – JavaDeveloper Mar 06 '22 at 21:37

0 Answers0