原型链继承
function SuperType() {
this.colors = ["red", "blue", "green"];
};
function SubType() {}
//继承了SuperType
SubType.prototype = new SuperType();
var instance1 = new SubType();
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
var instance2 = new SubType();
alert(instance2.colors); //"red,blue,green,black"
使用原型链会有一定的问题,如上所述,所有实例都共享一个color属性,修改后会相互影响;第二个问题是不能再不影响所有对象示例的情况下传递参数。
借用构造函数
在子类型构造函数中调用超类型构造函数。也可以使用apply()
和call()
方法在新创建的对象上执行构造函数。如下所示:
function SuperType() {
this.colors = ["red", "blue", "green"];
}
function SubType() { //继承了SuperType
SuperType.call(this);
}
var instance1 = new SubType();
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
var instance2 = new SubType();
alert(instance2.colors); //"red,blue,green"
使用该方法还可以传递参数
function SuperType(name) {
this.name= name;
}
function SubType() { //继承了SuperType,还可以传递参数
SuperType.call(this, "Nicholas"); //apply()方法也可以
this.age = 10;
}
var instance = new SubType();
alert(instance.name); //"Nicholas"
alert(instance.age); //10
要使用构造函数,那么方法都在构造函数中定义,因此函数不能复用。
组合继承
用原型链和构造函数组合到一起。即使用原型链实现对原型属性和方法的继承,再使用借用构造函数实现对实例的继承。这样既可以实现函数复用又能是每个实例有自己的属性。
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.sayAge = function() {
alert(this.age);
}
var instance1 = new SubType("nicholas", 10);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"nicholas"
instance1.sayAge(); //10
var instance2 = new SubType("greg", 23);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"greg"
instance2.sayAge(); //23
组合继承结合了原型链和组合继承的优点,而且,instanceof
和isPrototypeof
也能识基于组合继承创建的对象。
一般组合继承用的比较多,还有原型式继承和寄生式继承。