javascript inheritance
by Nabaraj Saha
HTML
<div id="test"></div>
<div id="test1"></div>
JavaScript
function Shape(){}
// augment prototype
Shape.prototype.name = 'shape';
Shape.prototype.toString = function() {return this.name;};
function TwoDShape(){}
// take care of inheritance
TwoDShape.prototype = Shape.prototype;
TwoDShape.prototype.constructor = TwoDShape;
// augment prototype
TwoDShape.prototype.name = '2D shape';
function Triangle(side, height) {
this.side = side;
this.height = height;
}
// take care of inheritance
Triangle.prototype = TwoDShape.prototype;
Triangle.prototype.constructor = Triangle;
// augment prototype
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){return this.side * this.height / 2;}
var my = new Triangle(5, 10);
var s = new Shape();
my.getArea();
writeIndiv("test", my.getArea());
writeIndiv("test1", s.name);
function writeIndiv(elmId, todis){
document.getElementById(elmId).innerHTML=todis;
}