构造器模式和原型模式共同使用

  这里简单提一句构造器和原型共同使用,也就是原型存储方法和共享属性,构造函数定义实例属性。

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"

  目前这种方法较为常见。

posted @ 2015-01-30 13:38  小北先森  阅读(138)  评论(0编辑  收藏  举报