Skip to content

Instantly share code, notes, and snippets.

@jitensachdeva
Created July 23, 2014 07:14
Show Gist options
  • Save jitensachdeva/18cc6cca98d05caef3ec to your computer and use it in GitHub Desktop.
Save jitensachdeva/18cc6cca98d05caef3ec to your computer and use it in GitHub Desktop.
sample to show inheritance in js
var Polygon = function(){
this.sides = arguments;
}
Polygon.prototype.perimeter = function(){
var sum = 0;
for(var i=0 ; i < this.sides.length; i++) {
sum = sum + this.sides[i];
}
return sum;
}
var Rectangle = function(length, breadth){
Polygon.call(this, length, breadth, length, breadth);
}
Rectangle.prototype = new Polygon();
//var rectangle = new Rectangle(3, 4);
//console.log(rectangle.perimeter());
var Square = function(side){
this.side = side
Rectangle.call(this, side,side)
}
Square.prototype = new Rectangle();
var square = new Square(3);
console.log(square.perimeter());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment