1

I'm studying/making personal notes on JavaScript again from scratch and bump into a few things for which I'd like some explanation.

Can someone explain this:

Object.prototype.hasOwnProperty("__proto__"); //True 
Object.prototype.__proto__; //null 

Object.hasOwnProperty("__proto__"); //False 
Object.__proto__; //function(){} 

Why does it say that Object doesn't have own property __proto__, and what is the function that it outputs on the last line?


Edit: the below part has been solved here: Why in JavaScript both "Object instanceof Function" and "Function instanceof Object" return true?

Additional question, why are the following statements both true?

Function instanceof Object //True 
Object instanceof Function //True 
2
  • 1
    In regard to your additional question: Function and Object are both functions (“constructors” so to say), and also both objects, because all functions are objects. Commented Sep 27, 2015 at 17:33
  • 2
    Your additional question has been already in this post elaborately. Commented Sep 27, 2015 at 17:36

1 Answer 1

2

A note about __proto__

This is not a standard property as of ECMAScript 5. This is not at all defined in the language specification of ECMAScript 5. But all the environments widely support its usage. As it is not part of the language specification, its usage is discouraged and the recommended way to access the internal prototype object is to use Object.prototype.getPrototypeOf or Object.prototype.setPrototypeOf.

Note 1: __proto__ has been standardized only in ECMAScript 2015.

Note 2: Setting the prototype object with setPrototypeOf is supported only in ECMAScript 2015.


Now, let's see the reason for each of the lines in your question in the following points.

  1. Now, the environments which support __proto__ have defined them in Object.prototype object, as per MDN. Since most of the objects inherit from Object, they all inherit __proto__ property as well. That is why Object.prototype.hasOwnProperty("__proto__"); returns true.

  2. But the value of that is null, because this section of the language specification says that the internal property [[Prototype]] of Object.prototype should be null

  3. Object.hasOwnProperty("__proto__"); returns False because __proto__ is actually defined on Object.prototype and Object is just inheriting it. As __proto__ is not its own property, it is returning False.

  4. Object.__proto__ returns Function object, because this section of the language specification clearly says that the internal [[Prototype]] property should be the Function object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.