My understanding is that the Object.__proto__
is the 'top-level' prototype object in javascript. I would expect its __proto__
to be null, but in Google Chrome (haven't tried other browsers), it isn't. Why is that?
I know the following image is probably a re-hash of the one below, but I made it myself to check my understanding. Is there anything wrong with it?
__proto__ is a way to inherit properties from an object in JavaScript. __proto__ a property of Object. prototype is an accessor property that exposes the [[Prototype]] of the object through which it is accessed. POSTly is a web-based API tool that allows for fast testing of your APIs (REST, GraphQL).
Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain.
_proto_ is a property that is automatically created in all JavaScript objects. This property references the prototype that created it. A prototype is an object that is automatically created for all functions.
It first creates an empty object, then sets the prototype of this object to the prototype property of the constructor, then calls the constructor function with this pointing to the newly-created object, and finally returns the object.
Object
is a function, it's __proto__
is an empty function function() {}
. The root object is an empty object {}
, not Object
. So, when you have an object like {foo:1, bar:1}
its relationships look like this:
I think you're mistaking Object.__proto__
for Object.prototype
.
Object.prototype.__proto__
is indeed null, because Object
doesn't extend anything.
Object
itself, however, is a function - aka. an instance of Function
.
Since Function
extends Object
, it's prototype has a __proto__
property.
You can thus take a detour over Object.__proto__.__proto__
to reach Object.prototype
, in fact:
Object.prototype === Object.__proto__.__proto__ // should yield true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With