Created
July 23, 2014 07:14
-
-
Save jitensachdeva/18cc6cca98d05caef3ec to your computer and use it in GitHub Desktop.
sample to show inheritance in js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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