原型重构

108 阅读1分钟

function Person(name,age){

this.name=name;

this.age=age;

}

Person.prototype.eat = function(){

console.log('吃火锅')

}

var p = new Person('小红',10);

Person.prototype = {//原型重构

constructor:Person,//重构的时候烧鹅一个constructor属性,要手动加一下

eat(){

console.log('吃水果')

},

play(){

console.log('玩游戏')

}

}

var p1 = new Person('小明',20)

p1.eat();

p.eat();