Why Is Foo.hasownproperty('__proto__') Equal To False?
Solution 1:
In fact, __proto__ is inherited from Object.prototype:
foo.hasOwnProperty('__proto__') // falseObject.prototype.hasOwnProperty('__proto__') // trueAnd according to MDN article,
There is nothing special about the
__proto__property. It is simply an accessor property -- a property consisting of a getter function and a setter function -- on Object.prototype.
As you say, intuitively it can seem that, since __proto__ is intrinsically related to each object, it should be an own property.
But it isn't like this. Instead, Object.prototype.__proto__ has a getter function which returns differently when called on different objects.
You can obtain something similar if you run
Object.defineProperty(
Object.prototype,
'self',
{get: function(){returnthis}}
)
Now you can call .self on different objects and you will get different results.
Also note this behavior isn't exclusive of __proto__. For example, the id property of an HTML element isn't an own property neither:
var el = document.createElement('div');
el.id = 'foo';
el.hasOwnProperty('id'); // falseElement.prototype.hasOwnProperty('id'); // true
Post a Comment for "Why Is Foo.hasownproperty('__proto__') Equal To False?"