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', 29);
instance1.colors.push('black');
alert(instance1.colors);
instance1.sayName();
instance1.sayAge();
var instance2 = new SubType('Greg', 27);
alert(instance2.colors);
instance2.sayName();
instance2.sayAge();
我不明白为什么用“借用构造函数”(即:SuperType.call(this, name);),colors会相互独立??
SuperType.call(this, name); this就是window, SuperType本来就是在window下的一个属性。