function Parent(){
this.a = 'Parent'
}
function Tom() {
this.a = 'Tom'
}
Parent.__proto__.print = function(){
console.log(this.a)
}
Parent.print()
Tom.print()
var child = new Parent()
child.print()
// undefined
// undefined
// Uncaught TypeError: child.print is not a function
Parent和Tom都是Function的实例,因此相当于在Function.prototype上面挂载了一个print方法,因此Parent.print()可以调用到这个方法,但是没有返回值,一个原因是 Parent方法没有执行,一个是因为this此时指向的是Parent,Parent方法上没有a属性,而child本身是基于构造函数创建了一个对象,child.__proto__.__proto__ === Object.prototype,因此在其原型链上找不到print方法。
instanceof构造函数的属性是否出现在某个实例的原型链上isPrototypeOf测试一个对象是否在另一个对象的原型链上hasOwnProperty判断一个对象自身属性中是否有某个属性getPrototypeOf返回指定对象的原型









网友评论