构造函数的 __proto__ 属性指向什么?

What does a constructor function's __proto__ attribute point to?

我正试图回过头来更好地理解原型继承。我明白实例的 __proto__ 属性指向构造函数的 prototype 对象,但是构造函数的 __proto__ 属性指向什么?

我曾假设构造函数本身是 Function 的一个实例,它会指向 Function 构造函数的 prototype 对象,但下面显示它是一个空函数。

var Example = function(){
   this.attribute = 'example';
}

var exampleInstance = new Example();

exampleInstance.__proto__ === Example.prototype // true
Example.__proto__ // function() {}

[编辑] Ovidiu Dolha 现在已经证实了我的理解,所以也许这会对某些人有所帮助。

这是一个原型链,一直到最后它发现 "Object" 作为根。

ECMAScript 5.1 规范是这样表述的:

15.3.4 函数原型对象的属性:

The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object

和15.2.4 对象原型对象的属性

The value of the [[Prototype]] internal property of the Object prototype object is null

在您的控制台中试试这个。

console.log(exampleInstance);

Example
attribute
:
"example"
__proto__
:
Object
constructor
:
()
__proto__
:
Object
__defineGetter__
:
__defineGetter__()
__defineSetter__
:
__defineSetter__()
__lookupGetter__
:
__lookupGetter__()
__lookupSetter__
:
__lookupSetter__()
constructor
:
Object()
hasOwnProperty
:
hasOwnProperty()
isPrototypeOf
:
isPrototypeOf()
propertyIsEnumerable
:
propertyIsEnumerable()
toLocaleString
:
toLocaleString()
toString
:
toString()
valueOf
:
valueOf()
get __proto__
:
__proto__()
set __proto__
:
__proto__()

Example.__proto__ 将与 Function.prototype 相同,就像 exampleInstance.__proto__Example.prototype

相同

这是因为 Example 是函数的一个实例。

一切最终都会到达Object.prototype,这是原型继承的根。

请注意,您应该尽可能避免使用 __proto__,因为它被认为已弃用。而是使用 Object.getPrototypeOf()