function Animal(){
this.species = "动物";
}
function Cat(name,color){
this.name = name;
this.color = color;
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
var cat1 = new Cat("大毛","黄色");
console.log(cat1.species)![图片描述][1]
当你定义一个构造函数的时候
此时在
Cat
的原型对象上会有一个constructor
属性,指向它自己(即Cat
)当你执行以下代码时
你已经修改了
Cat
的prototype
,此时Cat
的prototype
等于Animal
的实例,Cat
的原型对象已经没有了constructor
这个属性,所以需要你手动给他重新赋值Cat.prototype.constructor = Cat
,当然一般情况下用不到
constructor
这个属性,所以你不重新指定constructor
也是可以的