1、借用构造函数法
function SuperType(name) {
this.name = name;
this.sayName = function () {
alert(this.name);
}
}
function SubType(name, age) {
SuperType.call(this, name); // 这里借用了父类的构造函数
this.age = age;
}
2、对象冒充
function SuperType(name) {
this.name = name;
this.sayName = function() {
alert(this.name);
}
}
function SubType(name, age) {
this.supertype = SuperType; // 在这里使用了对象冒充
this.supertype(name);
this.age = age;
}
3、组合继承
function SuperType(name) {
this.name = name;
}
SuperType.prototype = {
sayName: function () {
alert(this.name);
}
}
function SubType(name, age) {
SuperType.call(this, name); // 在这里继承了属性
this.age = age;
}
SubType.prototype = new SuperType(); // 这里继承方法