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("大毛","黄色");
alert(cat1.species); // 动物
使用prototype完成继承,
1,Cat和Animal的属性都是构造函数定义的为什么可以通过prototype去设定,两个对象的prototype应该都是空的啊?
2,为什么 Cat.prototype = new Animal();之后prototype会包含三个属性(name,color,species)呢?我想的是把cat的prototype赋给了animal,应该只有animal的属性了?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
学习是最好的投资!