Inheritance in Javascript
by Biswa R
JavaScript
var rectangle = function(len, wid){
this.type = "rectangle";
this.len = len;
this.wid = wid;
}
rectangle.prototype.getArea = function(){
console.log("i m in Rectangle");
return (this.len*this.wid);
}
var square = function(len){
rectangle.call(this,len,len);
}
square.prototype = Object.create(rectangle.prototype);
square.prototype.constructor = square;
square.prototype.getArea = function(){
console.log("i m in square");
return (this.len*this.len);
}
var rectangleObj = new rectangle(2,3);
console.log(rectangleObj.len);
console.log(rectangleObj.wid);
console.log(rectangleObj.type);
var squareObj = new square(2);
console.log(squareObj.len);
console.log(squareObj.wid);
console.log(squareObj.type);
console.log(rectangleObj.getArea());
console.log(squareObj.getArea());