构造函数继承

function SuperType(){

this.colors = [1,2,3];

}

 

function SubType(){

//继承属性

SuperType.call(this);

}

 

var in1 = new SubType();

in1.colors.push(4);

alert(in1.colors);

 

var in2 = new SubType();

in2.colors.push(5);

alert(in2.colors);

 

在子类构造函数中执行超类的函数,则子类的实例中都会有自己的colors属性副本

 

参数式继承

function SuperType(name){

this.name = name;

}

 

function SubType(name){

//继承属性

SuperType.call(this,name);

}

 

var in1 = new SubType("Jack");

alert(in1.name);  //jack

 

var in2 = new SubType("gogo");

alert(in2.name); //gogo

优点:每个子类实例都有自己的属性副本

缺点:无法复用函数

posted @ 2014-12-04 11:47  lcw5945  阅读(113)  评论(0编辑  收藏  举报