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
1 Answer 1
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.
Now, the environments which support
__proto__have defined them inObject.prototypeobject, as per MDN. Since most of the objects inherit fromObject, they all inherit__proto__property as well. That is whyObject.prototype.hasOwnProperty("__proto__");returns true.But the value of that is
null, because this section of the language specification says that the internal property[[Prototype]]ofObject.prototypeshould benullObject.hasOwnProperty("__proto__");returnsFalsebecause__proto__is actually defined onObject.prototypeandObjectis just inheriting it. As__proto__is not its own property, it is returningFalse.Object.__proto__returnsFunctionobject, because this section of the language specification clearly says that the internal[[Prototype]]property should be theFunctionobject.
FunctionandObjectare both functions ("constructors" so to say), and also both objects, because all functions are objects.