Created
April 20, 2013 17:39
-
-
Save julsfelic/5426779 to your computer and use it in GitHub Desktop.
Scope safe constructor pattern w/ prototype chaining
This file contains 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
function Polygon(sides) { | |
if (this instanceof Polygon) { | |
this.sides = sides; | |
this.getArea = function() { | |
return 0; | |
}; | |
} else { | |
return new Polygon(sides); | |
} | |
} | |
function Rectangle(width, height) { | |
Polygon.call(this, 2); | |
this.width = width; | |
this.height = height; | |
this.getArea = function() { | |
return this.width * this.height; | |
}; | |
} | |
Rectangle.prototype = new Polygon(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment