咱们知道在js中万物皆对象,如今咱们假设每个对象建立时都会产生一个原型空间(原型对象 )函数
实例由其定义共享原型空间
由__proto__来寻找定义的原型空间
由prototype来寻找本身的原型空间spa
psprototype
p:prototype [p]: _proto_ 此图片为转载
此图片为转载设计
全部函数都是由Function构造函数构造的
function函数也是Function构造函数构造的因此时function本身构造了本身因此本身指向本身的原型空间
咱们能够看出图中function的原型空间和animal的原型空间都是object类型(其实第二列的原型空间都是object的实例,但逻辑上不是)3d
//全部函数类型的隐式原型都相同 由于都是Function的原型对象建立 //因此构造函数的__proto__是Function //那么Function的隐式原型 ? var fun = new Function(); console.log(fun.__proto__===Function.prototype); console.log(Function.__proto__===Function.prototype); //因此Function自己的隐式原型和显示原型相同 //能够看出Object也是函数定义的因此他的隐士原型应该是Function 的显示原型 console.log(Object.__proto__===Function.prototype)
那么object的原型空间的原型空间应该是undefined但这样就会让原型链没有尽头
为了逻辑完善就令其为null
能够看出原形空间的定义是由其定义的原型空间来定义的(实例和其定义共享原型空间)指针
function Animal(){} function Bird(){} function Swallow(){} Bird.prototype= new Animal() Swallow.prototype=new Bird() var swallow = new Swallow() console.log(swallow.__proto__.prototype) console.log(Swallow.__proto__.prototype) console.log(Bird.__proto__.prototype) console.log(Animal.__proto__.prototype) console.log(Function.prototype.__proto__.prototype) console.log(Swallow.prototype) console.log(Animal.prototype) var animal = new Animal console.log(animal.__proto__.__proto__) var obj = {} console.log(typeof (obj.__proto__.__proto__)) ////ps没有修正construct
那么若假设Pobject来定义了Object的原型空间的原型空间那么图中全部的(undefined)的位置的_proto_都会指向Pobject的原型空间以这个逻辑就能够让无限迭代下去(指针的指针的.....的指针)
这样向上寻找原型链和向下寻找原型链就统一了,但js设计封锁了向上无限迭代(Object的原型空间为null)code