前端的设计模式 -- 混合模式
混合模式
function Person(name,age){
this.name = name;
this.age = age;
};
Person.prototype.printName = function(){
console.log(this.name);
}
function Student(name,age){
继承 Person 的属性
Person.call(this,name,age);
}
function create(prototype){
function F(){};
F.prototype = prototype;
return new F();
}
// 让Student的原型指向一个对象,该对象的原型指向了Person.prototype,通过这种方式继承 Person 的方法
Student.prototype = create(Person.prototype);
Student.prototype.printAge = function(){
console.log(this.age);
}
var student = new Student('xin',22);
student.printName(); // "xin"
.

浙公网安备 33010602011771号