聖盃模式
- 聖盃模式的核心就是拿一個空的建構函式去當中間人,解決組合模式的缺陷。
舉個例子
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??"";
}
function inherit(targer, original) {
function Fn() {};
Fn.prototype = original.prototype;
targer.prototype = new Fn();
targer.prototype.constructor = targer;
};
inherit(Student, Person);
new Student('小明', 18, '男', 100).say();