js继承之二(组合模式=借用构造函数+原型链方式)

借用构造函数模式:不能继承原型上的属性,可以避免引用类型修改问题

原型链:能够继承原型上的属性,会发生引用类型修改

 

so:敲黑板!

function CarModel(c){
      this.color=c||"白色";
      this.arr=[1,2,3];
      this.getColor=function(){
	        console.log('我的颜色是'+this.color);
      }
}
CarModel.prototype.test="lla";
function Car(brand){
	 CarModel.call(this);//借用构造函数    继承构造函数的属性
	 this.brand=brand;
     this.getBrand=function(){
	      console.log('我的品牌是'+this.brand);
      }
}

Car.prototype=new CarModel(); //原型链模式  继承原型对象上的属性

var car1=new Car("丰田");

console.log(car1);

  

  结果:

皆大欢喜。

 

posted @ 2017-04-16 13:53  llauser  阅读(321)  评论(0)    收藏  举报