<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;? ? ? ? }????????//---------------------------------問(wèn)題在下一行------------------------------? ? ? ? //SubType.prototype = new SuperType();//這里我特意注釋掉,然后我覺(jué)得subtype實(shí)例應(yīng)該不能調(diào)用sayAge和sayName方法了,但實(shí)際能正常調(diào)用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//調(diào)用sayAge正常運(yùn)行,不會(huì)報(bào)錯(cuò)? ? ? ? ??? ? ? ? var instance2 = new SubType("Greg", 27);? ? ? ? alert(instance2.colors); ?//"red,blue,green"? ? ? ? instance2.sayName(); ? ? ?//"Greg";//調(diào)用sayName會(huì)報(bào)錯(cuò)? ? ? ? instance2.sayAge(); ? ? ? //27? ? ? ?? ? ? ??? ? </script>
javascript原型繼承問(wèn)題,問(wèn)題在下面代碼注釋里,麻煩大家了,謝謝
yuqingzhijie3596863
2017-03-22 10:10:23