<script type="text/javascript">
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实例应该不能调用sayAge和sayName方法了,但实际能正常调用sayAge,而不能掉用sayName,为什么??
SubType.prototype.sayAge = function(){
alert(this.age);
};
var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
//instance1.sayName(); //"Nicholas";
instance1.sayAge(); //29//调用sayAge正常运行,不会报错
var instance2 = new SubType("Greg", 27);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";//调用sayName会报错
instance2.sayAge(); //27
</script>
相关分类