每一个对象都有一个__proto__(先后各两个下划线)属性来标识本身所继承的原型对象。
__proto__属性对性能影响很是严重,不建议使用。git
只有函数才有prototype属性。
当你建立函数时,JS会为这个函数自动添加prototype属性。构造函数原型的constructor默认指向自身。github
function Person(){ this.name="aaa"; } Person===Person.prototype.constructor // true console.log(Person.prototype);// 结果见下图
每一个内置对象都是一个native object。一个内置的对象同时也是一个构造函数。function是一种对象类型。函数
function是内置Function的实例,即普通函数是Function的实例
。所以普通函数的constructor是Function。Function的constructor仍是他本身。性能
function Person(){var a=1;} Person.constructor //function Function() { [native code] } Function //function Function() { [native code] } Function.constructor //function Function() { [native code] } Function===Function.constructor //true
函数都有prototype属性和__proto__ 属性。
函数的__proto__ 属性均是function () { [native code] }
。能够这样理解,function既是对象,又是函数。function做为对象,有__proto__ 属性,由于__proto__ 属性指向构造函数的原型,而function是由Function建立的,所以普通函数.__proto__ =Function.prototype。
this
function Person(){var a=1;} Person.__proto__===Function.prototype ///true Function.prototype //function () { [native code] }
Function.prototype和Function.__proto__为同一对象。这是因为构造Function的是他本身,根据普通函数.__proto__ =Function.prototype
,因此Function.__proto__ =Function.prototype
。spa
Function.prototype //function () { [native code] } Function.__proto__ // function () { [native code] } Function.prototype===Function.__proto__ //true
Function.prototype的__proto__是Object.prototype。能够这样理解:Function.prototype也是一个原型对象,而普通对象的原型是Object.prototype,因此prototype
Function.prototype.__proto__===Object.prototype //true
注意:使用 Function.prototype.bind创造的函数,没有prototype属性。code
Object.__proto__是Function.prototype
。由于Object自己是个(构造)函数,是Function的实例。对象
Object.__proto__ //function () { [native code] } Object.constructor //function Function() { [native code] }
原型链的尽头(root)是Object.prototype。全部对象均从Object.prototype继承属性。blog
Object.prototype.__proto__ // null
某个对象.__proto__ =构造函数.prototype 。
var a={}; a.__proto__===Object.prototype //true a.__proto__ //结果见下图
普通对象是Object构造函数的实例,因此普通对象的原型是Object.prototype。
function Person(){this.name="aa";} var b=new Person(); b.__proto__===Person.prototype; //true b.__proto__ //结果见下图
上图中b对象是Person构造函数的实例,因此b对象的原型是Person.prototype。
1. Object和其它函数由Function产生,Function的constructor是他本身。
2. 一个对象一定有__proto__,而原型链的顶端是Object.prototype。
3. Object.prototype 是对象,可是不是经过Object函数建立的。由于Object.prototype.__proto__为null。
4. Object的__proto__是Function.prototype,可是Function.prototype也是一个原型对象,所以Function.prototype.__proto__为Object.prototype。