function Student(Name,age){
this.Name=Name
this.age=age
this.friends=[]
}
Student.prototype.eating=function(){
console.log(this.Name,'ecting')
}
function Person(Name,age){
Student.call(this,Name,age)
}
//缺点就是所有给原型链添加方法都会添加给父类,导致全局共享
Person.prototype=Student.prototype
var F=new Person('张三',18)
var F2=new Person('王五',18)
F.friends.push('11')
F.eating()
console.log(F)
console.log(F.friends,F2.friends)
父类原型赋值给子类
于 2022-02-13 12:54:38 首次发布