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;
};
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;
};
相关文章推荐
- JavaScript学习笔记——继承
- JavaScript学习笔记(5)---继承2
- JavaScript 学习笔记八 继承与引用
- JavaScript继承学习笔记
- JavaScript中的继承学习笔记(1):Crockford uber方法中的陷阱
- javascript面向对象学习笔记(一)——继承
- JavaScript继承学习笔记【新手必看】
- 《JavaScript高级程序设计 第三版》学习笔记 (五) 继承详解
- javascript学习笔记 (五) -继承和Closures
- JavaScript ES6新的类继承特性学习笔记
- 学习笔记:javascript中的六种继承
- javascript之对象学习笔记(二)--对象原型,继承
- javascript学习笔记(九)javascript中的原型(prototype)及原型链的继承方式
- JavaScript学习笔记-原型继承
- JavaScript学习笔记(三十二) 经典继承模式二-借用构造方法
- Javascript学习笔记九——prototype封装继承
- javascript学习笔记(二)--继承
- javascript学习笔记(二)--继承
- js学习笔记——理解javascript的对象与继承
- Javascript 设计模式学习笔记(2) - 继承(Inheritance) (下)