您的位置:首页 > Web前端 > JavaScript

JavaScript学习笔记之继承

2015-05-06 03:37 253 查看
        一、原型链实现继承

function SuperType(){
this.property=true;
}

SuperType.prototype.getSuperValue=function(){
return this.property;
};

function SubType(){
this.subproperty=false;
}

SubType.prototype=new SuperType();

SubType.prototype.getSubValue=function(){
return this.subproperty;
};

var instance=new SubType();

        二、使用构造函数实现继承
function SuperType(){
this.colors=["red","yellow"];
}

function SubType(){
SuperType.call(this);
}

var instance=new SubType();        三、使用原型链和构造函数实现组合继承
function SuperType(name){
this.name=name;
this.colors=["red","yellow"];
}

SuperType.prototype.sayName=function(){
return this.name
};

function SubType(name,age){
SuperType.call(this,name);
this.age=age;
}

SubType.protype=new SuperType();
SubType.protype.constructor=SubType;
SubType.protype.sayAge=function(){
return this.age;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息