//借用构造函数(constructor stealing),有时候也叫伪造对象或者经典继承
//弊端:原型方法不可复用,只有构造函数的属性可以复用
function SuperType() {
this.colors = ["red", "blue"];
}
function SubType() {
SuperType.call(this);
//SuperType.apply(this);
}
var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors);//["red", "blue", "black"]
var instance2 = new SubType();
console.log(instance2.colors);//["red", "blue"]
//相对于原型链,借用构造函数可以在子类型构造函数中向超类型构造函数传递参数
function SuperType2(name) {
this.name= name;
}
function SubType2(name, age) {
//继承了SuperType2同时还传递了参数
SuperType2.call(this, name);
//SuperType2.apply(this, [name]);
this.age = age;
}
var instance3 = new SubType2("Tom", "20");
console.log(instance3.name);//Tom
console.log(instance3.age);//20