javascript 继承
2014-01-13 00:04
204 查看
function Shape(id) {
this.id = id;
}
Shape.prototype.name = "shape";
Shape.prototype.toString = function () {
var result = [];
if (this.constructor.parent) {
result[result.length] = this.constructor.parent.toString();
}
result[result.length] = this.name;
return result.join(', ');
};
function TwoDShape(id) {
TwoDShape.parent.constructor.call(this, id);
}
//TwoDShape.prototype = new Shape();
//TwoDShape.prototype = Shape.prototype;
var F = function () {
}
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.parent = Shape.prototype;
TwoDShape.prototype.name = "2D shape";
function Triangle(id, side, height) {
Triangle.parent.constructor.call(this, id);
this.side = side;
this.height = height;
}
//Triangle.prototype = new TwoDShape();
//Triangle.prototype = TwoDShape.prototype;
var F = function () {
}
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.parent = TwoDShape.prototype;
Triangle.prototype.name = "triangle";
Triangle.prototype.getArea = function () {
return this.side * this.height / 2;
}
// YUI
function extend(Child, Parent) {
var F = function () {
};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.parent = Parent.prototype;
}
function Person(name){
this.name = name;
}
Person.prototype.say = function(){
return "Hi, I' m " + this.name + ". ";
}
extend(Developer,Person);
function Developer(name, language){
this.language = language;
Developer.parent.constructor.call(this,name);
}
Developer.prototype.say = function(){
return Developer.parent.say.call(this) + "I love " + this.language + ".";
}
/*
var javaDeveloper = new Developer("A","Java");
var javascriptDeveloper = new Developer("B","Javascript");
alert(javaDeveloper.say());
alert(javascriptDeveloper.say());
*/
this.id = id;
}
Shape.prototype.name = "shape";
Shape.prototype.toString = function () {
var result = [];
if (this.constructor.parent) {
result[result.length] = this.constructor.parent.toString();
}
result[result.length] = this.name;
return result.join(', ');
};
function TwoDShape(id) {
TwoDShape.parent.constructor.call(this, id);
}
//TwoDShape.prototype = new Shape();
//TwoDShape.prototype = Shape.prototype;
var F = function () {
}
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.parent = Shape.prototype;
TwoDShape.prototype.name = "2D shape";
function Triangle(id, side, height) {
Triangle.parent.constructor.call(this, id);
this.side = side;
this.height = height;
}
//Triangle.prototype = new TwoDShape();
//Triangle.prototype = TwoDShape.prototype;
var F = function () {
}
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.parent = TwoDShape.prototype;
Triangle.prototype.name = "triangle";
Triangle.prototype.getArea = function () {
return this.side * this.height / 2;
}
// YUI
function extend(Child, Parent) {
var F = function () {
};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.parent = Parent.prototype;
}
function Person(name){
this.name = name;
}
Person.prototype.say = function(){
return "Hi, I' m " + this.name + ". ";
}
extend(Developer,Person);
function Developer(name, language){
this.language = language;
Developer.parent.constructor.call(this,name);
}
Developer.prototype.say = function(){
return Developer.parent.say.call(this) + "I love " + this.language + ".";
}
/*
var javaDeveloper = new Developer("A","Java");
var javascriptDeveloper = new Developer("B","Javascript");
alert(javaDeveloper.say());
alert(javascriptDeveloper.say());
*/
相关文章推荐
- javascript中各种继承方式的优缺点
- JavaScript五种继承方式[转]
- JavaScript语言精粹之继承篇
- Javascript继承机制的设计思想
- javascript模拟继承
- JavaScript面向对象编程(9)高速构建继承关系之整合原型链
- JavaScript 的原型与继承
- javascript 继承
- JavaScript 框架开发笔记 [三] 自定义HTML对像的扩展与继承
- 【JavaScript】【学习】对象的创建和继承
- 完美实现javascript继承
- 深入理解JavaScript中的继承:原型链篇
- 推荐JavaScript实现继承的最佳方式
- Javascript实现继承的理解
- javascript继承
- JavaScript中的对象和原型链,函数继承(转)
- 【JavaScript高级程序设计】读书笔记之二 —— 理解对象的原型、继承
- javascript继承
- JavaScript实现类与继承
- javascript 类继承1