<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>
javascript原型繼承問題,問題在下面代碼注釋里,麻煩大家了,謝謝
yuqingzhijie3596863
2017-03-22 10:10:23