混合继承
混合继承:对象冒充继承+原型链继承
//1.父类构造函数
function Student(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
this.arr = [1,2,3];
}
Student.prototype.classId = "1116"
Student.prototype.study = function(){
console.log("good good study , day day sleep!");
}
//2.子类构造函数
function MiniStudent(name,age,sex){
//3.对象冒充继承
Student.call(this,name,age,sex)
this.play = function(){
console.log("玩王者荣耀");
}
}
//混合继承 对象冒充(构造函数中属性和方法)+原型链继承(继承原型对象中属性和方法)
//4.原型链继承
MiniStudent.prototype = new Student("小明",3,"男");
//5.实例化对象
var ms1 = new MiniStudent("小郝",10,"女");
console.log(ms1);
优点:可以传参,引用数据类型不会再一改全改,可以继承父类原型中的属性和方法
缺点:创建了一个多余的父类实例