- 原型链
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
}
function SubType(){
this.subproperty = false;
}
SubType.prototype = new SuperType();
- 借用构造函数
function SuperType(){
this.colors = ['red','blue','green'];
}
function SubType(){
SuperType.call(this)
}
- 组合继承(伪经典继承)
function SuperType(name){
this.name = name;
this.colors = ['red','blue','green']
}
SuperType.prototype.sayName = function(){
alert(this.name)
}
function SubType(name,age){
SuperType.call(this,name)
this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
- 原型式继承
function object(o){
function F(){}
F.prototype = o;
return new F();
}
var person = {
name:"Nicholas",
friends:["Shelby","Court","Van"]
};
var anotherPerson = object(person);
- 寄生式继承
function createAnother(original){
var clone = object(original)
clone.sayHi = function(){
alert('hi')
};
return clone;
}
var person = {
name:'Nicholas',
friends:['Shelby','Court','Van']
}
var anotherPerson = createAnother(person)
- 寄生组合式继承
function inheriPrototype(subType,superType){
var prototype = object(supeType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
function SuperType(name){
this.name = name;
this.colors = ['red','blue','green'];
}
superType.prototype.sayName = function(){
alert(this.name);
}
function SubType(name,obj){
SuperType.call(this,name);
this.age = age;
}
inheritPrototype(SubType,SuperType)