Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Object.__proto__.__proto__ not null?

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?

Edit

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? enter image description here

like image 339
jmrah Avatar asked Jul 29 '15 16:07

jmrah


People also ask

What is object __ proto __?

__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).

Does object have prototype?

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.

What is _proto_?

_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.

What is prototypal nature of object?

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.


2 Answers

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:

enter image description here

like image 86
georg Avatar answered Sep 18 '22 08:09

georg


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
like image 23
Siguza Avatar answered Sep 21 '22 08:09

Siguza