87,901
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
var c1=function(){this.name="xw1";}
var c2=function(){this.name="xw2";}
c2.prototype=new c1();
var o2=new c2();
alert(o2.constructor);//c1
</script>
c2.prototype=new c1();//这句话修改了c2原型链中的constructor
//单纯的function里没有constructor属性,
//o2.constructor其实是在o2的原型链中找constructor属性,
//c2.prototype.constructor原本应该是c2的,
//但是c2.prototype=new c1();这句话修改了c2原型链中的constructor,使其指向了c1
//所以最后一句alert(o2.constructor);会再c2原型链中找到已经指向c1的constructor
//当执行c2.prototype.construction=c2的时候,变会使原型链中的constructor重新指向c2
var c1=function(){this.name="xw1";}
var c2=function(){this.name="xw2";}
c2.prototype=new c1();
var o2=new c2();
console.log(o2.hasOwnProperty('constructor'));//false
console.log(o2.__proto__.hasOwnProperty('constructor'))//o2.__proto__指的是new c1();
console.log(o2.__proto__.__proto__.hasOwnProperty('constructor'));//true
alert(o2.constructor);//其实o2.constructor是延其原型链一直找到o2.__proto__.__proto__才找到的,而并非o2对象自身的属性