这里简单提一句构造器和原型共同使用,也就是原型存储方法和共享属性,构造函数定义实例属性。
function Dog(age,size)
{
this.age=age;
this.size=size;
this.son=["adobe","sun"];
}
Dog.prototype={
constructor:Dog;
toAge:function(){
alert(this.age);
}
}
var dog1=new Dog(11,22);
var dog2=new Dog(22,33);
dog1.son.push("name");
alert(dog1.son);//"adobe,sun,name"
alert(dog2.son);//"adobe,sun"
目前这种方法较为常见。