Mega Code Archive

 
Categories / JavaScript Tutorial / Object Oriented
 

Three level inheritance by using the Hybrid Method

function Shape(iSides) {     this.sides = iSides; } Shape.prototype.getArea = function (ss) {     return 0; }; function Triangle(iBase, iHeight) {     Shape.call(this, 3);     this.base = iBase;     this.height = iHeight; } Triangle.prototype = new Shape(); Triangle.prototype.getArea = function () {     return 0.5 * this.base * this.height; }; function Rectangle(iLength, iWidth) {     Shape.call(this, 4);     this.length = iLength;     this.width = iWidth; } Rectangle.prototype = new Shape(); Rectangle.prototype.getArea = function () {     return this.length * this.width; }; var triangle = new Triangle(12, 4); var rectangle = new Rectangle(22, 10); alert(triangle.sides);        //outputs "3" alert(triangle.getArea());    //outputs "24" alert(rectangle.sides);       //outputs "4" alert(rectangle.getArea());   //outputs "220"