常用的方法
比如,现在有一个“动物”对象的构造函数。
function Animal(){
this.species = "动物";
}
还有一个“猫”对象的构造函数。
function Cat(name,color){
this.name = name;
this.color = color;
}
1.绑定构造函数继承
function Cat(name,color){
Animal.apply(this, arguments);
this.name = name;
this.color = color;
}
var cat1 = new Cat("大毛","黄色");
alert(cat1.species); // 动物
2.原型链继承
1 Cat.prototype = new Animal(); 2 3 Cat.prototype.constructor = Cat; 4 5 var cat1 = new Cat("大毛","黄色"); 6 7 alert(cat1.species); // 动物
3.object方法继承
function object(o) { function F() {} F.prototype = o; return new F(); } var Doctor = object(Chinese); Doctor.career = '医生'; alert(Doctor.nation); //中国
另外一些方式
如拷贝继承、直接继承prototype、使用空对象作为中介继承等
详见