- JScript code
function Odemo1(){} Odemo1.prototype.sayName=function(){} function Odemo2(){} Odemo2.prototype=new Odemo1(); var a=new Odemo2(); alert(a instanceof Odemo1); //显示true alert(a instanceof Odemo2); //显示true alert(a instanceof Object); //显示true alert(a.constructor==Odemo1); //显示true alert(a.constructor==Odemo2); //为什么这里显示false alert(Odemo1.prototype.isPrototypeOf(a)); //显示true alert(Odemo2.prototype.isPrototypeOf(a)); //显示true
问题如以上代码所示,
说白了,为什么a.constructor指向的是Odemo1
a不是Odemo2的实例对象么?
------解决方案--------------------
Odemo2.prototype=new Odemo1(); // 重写Odemo2的原型,就是让Odemo2继承Odemo1,Odemo1.prototype.constructor指向Odemo1,所以Odemo2也是一样的。