function Person() {}
var person = new Person();
Person.prototype.eat = function() {
console.log('food')
}
person.eat() // "food"
function Person() {}
var person = new Person();
Person.prototype ={
constructor:Person,
eat:function(){
console.log('food')
}
}
person.eat() // error : person.eat is not a function
例子
首先对象是引用类型,
Person.prototype
是一个指针,指向的是具体原型对象。person.__proto__
指向的是创建实例时的初始原型对象。所以重写对象
Person.prototype
,并不会影响person.__proto__
指向初始原型对象,只是修改了Person.prototype
的指针指向。