function Fish(name, color){
this.name=name;
this.color=color;
}
Fish.prototype.livesIn="water";
Fish.prototype.price=20;
var fish1=new Fish("mackarel", "gray");
var fish2=new Fish("goldfish", "orange");
var fish3=new Fish("salmon", "white");
for (int i=1; i<=3; i++){
alert(fish.name+","+fish.color+","+fish.livesIn+","+fish.price);
}
输出应该是:
"mackarel, gray, water, 20"
"goldfish, orange, water, 20"
"salmon, white water, 20"
原型类型属性:
你不需要显示的声明一个prototype属性,因为在每一个构造函数中都有他的存在。
prototype是一个原型类型对象。
当一个对象呗创建时,这个构造函数 将会把它的属性prototype赋给新对象的内部属性
__prototype__.
这个__prototype__被这个对象用来查找它的属性。
prototype 原型类型属性
最新推荐文章于 2023-06-08 07:30:00 发布