Js继承之组合模式
组合模式
- 前置知识 原型 原型链 apply
举个例子
function Person(name, age) {
this.name = name??"";
this.age = age??"";
};
Person.prototype.say = function () {
console.log('我的名字是' + this.name + ',今年' + this.age + '岁')
}
function Student(name, age, gender, score) {
Person.apply(this, [name, age]);
this.gender = gender??"";
this.score = score??"";
}
Student.prototype = new Person();
console.log(new Student('ZhangSan', 20, '男', 100));