图解原型及原型链

从图中可以总结一下几点:

1、 __proto__是对象的内部属性,每个对象都有__proto__属性, 它的值就是原型对象。
2、 new Function时会为每个函数自动创建一个prototype属性,以提供该函数用作构造函数的可能性(15.3.2.1)。所以,函数才有prototype属性。
3、 JavaScript的世界观里, 函数也是对象, 函数是一等公民。所以,函数既有__proto__属性也有prototype属性。
4、 几乎所有函数的prototype都是对象,除了一个特例:Function.prototype 是一个特殊函数。
5、 几乎所有函数都有prototype属性,而特殊函数 Function.prototype 没有prototype属性。
6、 构造函数Foo.proto、 Function.__proto__和 Object.__proto__都指向 Function.prototype。所以, Object/Array/String等等构造函数本质上和Function一样,均继承于Function.prototype。
7、 Function.prototype.__proto__指向Object.prototype。
8、 原型链的尽头是Object.prototype。所有对象均从Object.prototype继承属性。Object.prototype.__proto__为null。


以下是出自 Standard ECMA-262 的摘抄

4.3.4 constructor

function object that creates and initialises objects
NOTE The value of a constructor’s “prototype” property is a prototype object that is used to implement inheritance and shared properties.

4.3.5 prototype

object that provides shared properties for other objects
NOTE When a constructor creates an object, that object implicitly references the constructor’s “prototype” property for the purpose of resolving property references. The constructor’s “prototype” property can be referenced by the program expression constructor.prototype, and properties added to an object’s prototype are shared, through inheritance, by all objects sharing the prototype. Alternatively, a new object may be created with an explicitly specified prototype by using the Object.create built-in function.

15.2.3 Properties of the Object Constructor

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

15.2.3.1 Object.prototype

The initial value of Object.prototype is the standard built-in Object prototype object (15.2.4).
This property has the attributes {[[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

15.2.4 Properties of the Object Prototype Object

The value of the [[Prototype]] internal property of the Object prototype object is null, the value of the [[Class]] internal property is "Object", and the initial value of the [[Extensible]] internal property is true.

15.2.4.1 Object.prototype.constructor

The initial value of Object.prototype.constructor is the standard built-in Object constructor.

15.3.1 The Function Constructor Called as a Function

When Function is called as a function rather than as a constructor, it creates and initialises a new Function object. Thus the function call Function(…) is equivalent to the object creation expression new Function(…) with the same arguments.

15.3.2.1 new Function (p1, p2, … , pn, body)

A prototype property is automatically created for every function, to provide for the possibility that the function will be used as a constructor.

15.3.3 Properties of the Function Constructor

The Function constructor is itself a Function object and its [[Class]] is "Function". The value of the [[Prototype]] internal property of the Function constructor is the standard built-in Function prototype object (15.3.4).

15.3.3.1 Function.prototype

The initial value of Function.prototype is the standard built-in Function prototype object (15.3.4).
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

15.3.4 Properties of the Function Prototype Object

The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.
The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object (15.2.4). The initial value of the [[Extensible]] internal property of the Function prototype object is true.
The Function prototype object does not have a valueOf property of its own; however, it inherits the valueOf property from the Object prototype Object.
The length property of the Function prototype object is 0.

15.3.4.1 Function.prototype.constructor

The initial value of Function.prototype.constructor is the built-in Function constructor.


参考地址:

原型图链接:https://www.processon.com/view/5b654b99e4b0edb750f95ffd

Standard ECMA-262:http://www.ecma-international.org/ecma-262/5.1/

posted @ 2018-08-14 15:06  MiyaMiya  阅读(287)  评论(0编辑  收藏  举报