Skip to content

Instantly share code, notes, and snippets.

@julsfelic
Created April 20, 2013 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julsfelic/5426779 to your computer and use it in GitHub Desktop.
Save julsfelic/5426779 to your computer and use it in GitHub Desktop.
Scope safe constructor pattern w/ prototype chaining
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