修改构造器默认prototype后,新实例的constructor指向问题和解决办法

Chrome中调试

源码:

构造器  function Dog(){this.tail=true}

构造新实例 var benji= new Dog(); var rusty=new Dog();

新实例的constructor  benji.constructor    //ƒ Dog(){this.tail=true}  Dog

在原型中添加方法  Dog.prototype.say=function (){return 'woff'};

修改构造器的prototype Dog.prototype={paws:4,hair:true};

构造新实例 var lucky=new Dog();

新实例的constructor  lucky.constructor  //ƒ Object() { [native code] }  Object

解决办法:重写某对象的prototype时,重置相应constructor属性    Dog.prototype.constructor=Dog;

原文地址:https://www.cnblogs.com/js123/p/8276508.html