您的位置:首页 > 其它

构造函数继承

2014-12-04 11:47 113 查看
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


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

缺点:无法复用函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: