一、对象继承
function SupperType(){
this.supProp = "Supper property";
}
SupperType.prototype.showSupperProp = function(){
console.log(this.supProp);
};
function SubType(){
this.subProp = "Sub property";
}
SubType.prototype = new SupperType();
SubType.prototype.constructor = SubType ;
SubType.prototype.showSubProp = function(){
console.log(this.subProp);
} ;
var SubType = new SubType();
SubType.showSupperProp();
SubType.showSubProp();
二、借用构造函数继承
```javascript
function SuperType(name){
this.name = name ;
this.showSupperName = function(){
console.log(this.name);
};
}
function SubType_1(name,age){
SuperType.call(this,name);
this.age = age;
}
SubType_1.prototype.showSubName = function(){
console.log(this.name);
};
var subType = new SubType_1("构造函数继承",20);
subType.showSupperName();
subType.showSubName();
console.log(subType.name);
console.log(subType.age);
三、混合继承
function Person(name,age){
this.name = name;
this.age = age;
}
Person.prototype.setName = function(name){
this.name = name;
};
function Student(name,age,price){
Person.call(this,name,age);
this.price = price;
}
Student.prototype = new Person();
Student.prototype.constructor = Student;
Student.prototype.setPrice = function(price){
this.price = price;
};
var s = new Student("组合继承",24,15000);
console.log(s.name,s.age,s.price);
s.setName("组合继承2");
s.setPrice(16000);
console.log(s.name,s.age,s.price);