标签:
function Shape(){ this.name = 'shape'; this.toString = function(){ return this.name; } } function TwoDShape(){ this.name = '2D shape'; } function Triangle(side,height){ this.name = 'Triangle'; this.side = side; this.height = height; this.getArea = function(){ return this.side*this.height/2; }; } /* inheritance */ TwoDShape.prototype = new Shape(); Triangle.prototype = new TwoDShape();
TwoDShape.prototype.constructor = TwoDShape; Triangle.prototype.constructor = Triangle;
<span style="font-family:Microsoft YaHei;font-size:14px;color:#333333;">function Shape(){} Shape.prototype.name = 'shape'; Shape.prototype.toString = function(){ return this.name; } function TwoDShape(){} TwoDShape.prototype = new Shape(); TwoDShape.prototype.constructor = TwoDShape; TwoDShape.prototype.name = '2d shape'; function Triangle(side,height){ this.side = side; this.height = height; } Triangle.prototype = new TwoDShape; Triangle.prototype.constructor = Triangle; Triangle.prototype.name = 'Triangle'; Triangle.prototype.getArea = function(){ return this.side*this.height/2; }</span>再改写(引用传递而不是值传递):
<span style="font-size:14px;color:#333333;">function Shape(){} Shape.prototype.name = 'shape'; Shape.prototype.toString = function(){ return this.name; } function TwoDShape(){} TwoDShape.prototype = Shape.prototype; TwoDShape.prototype.constructor = TwoDShape; TwoDShape.prototype.name = '2d shape'; function Triangle(side,height){ this.side = side; this.height = height; } Triangle.prototype = TwoDShape.prototype; Triangle.prototype.constructor = Triangle; Triangle.prototype.name = 'Triangle'; Triangle.prototype.getArea = function(){ return this.side*this.height/2; }</span>
<span style="font-size:14px;color:#333333;">function Shape(){} Shape.prototype.name = 'shape'; Shape.prototype.toString = function(){ return this.name; } function TwoDShape(){} var F = function(){} F.prototype = Shape.prototype; TwoDShape.prototype = new F(); TwoDShape.prototype.constructor = TwoDShape; TwoDShape.prototype.name = '2d shape'; function Triangle(side,height){ this.side = side; this.height = height; } F.prototype = TwoDShape.prototype; Triangle.prototype = new F(); Triangle.prototype.constructor = Triangle; Triangle.prototype.name = 'Triangle'; Triangle.prototype.getArea = function(){ return this.side*this.height/2; }</span>
标签:
原文地址:http://blog.csdn.net/wuweitiandian/article/details/42716789