关键词
__proto__
对象和函数都有的属性,始终指向其函数的原型属性prototype
prototype
只有函数才有的原型属性constructor
对象和原型对象的属性,当做指针,指向对象的函数和拥有原型对象的函数
结论
- 对象通过
__proto__
不断寻找其构造函数的prototype
的过程叫原型链 - 实例对象的
__proto__
指向构造函数的prototype
- 构造函数的
prototype
的constructor
等于构造函数本身
代码示例
function Person(name){
this.name = name
this.eat = function(){
console.log(this.name+' 构造函数的 eat')
}
}
Person.prototype.eat = function(){
console.log(this.name+' 原型上的eat')
}
var person = new Person('zhangsan')
person.eat()
console.log('person.__proto__ === Person.prototype', person.__proto__ === Person.prototype)
console.log('Person.prototype.constructor===Person', Person.prototype.constructor===Person)
console.log('person.constructor === Person.prototype', person.constructor === Person.prototype)