Skip to content

Instantly share code, notes, and snippets.

@jagoda
Last active August 29, 2015 13:56
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 jagoda/9176185 to your computer and use it in GitHub Desktop.
Save jagoda/9176185 to your computer and use it in GitHub Desktop.
Factories and Constructors
function makeInstance (constructor, context) {
return context === global || context === undefined ?
Object.create(constructor.prototype) :
context;
}
function Rectangle (length, width) {
var rectangle = makeInstance(Rectangle, this);
rectangle.area = function () {
return length * width;
};
return rectangle;
}
function Square (side) {
var square = makeInstance(Square, this);
Rectangle.call(square, side, side);
return square;
}
Square.prototype = Object.create(Rectangle.prototype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment