Skip to content

Instantly share code, notes, and snippets.

@mauriciosoares
Created June 11, 2014 19:26
Show Gist options
  • Save mauriciosoares/ef4f3b120c13c98b2047 to your computer and use it in GitHub Desktop.
Save mauriciosoares/ef4f3b120c13c98b2047 to your computer and use it in GitHub Desktop.
var CarMaker = function() {};
CarMaker.prototype.drive = function() {
return 'My car has ' + this.wheels + ' wheels';
};
CarMaker.factory = function(type) {
var car;
if(typeof CarMaker[type] !== 'function') {
throw new Error('The factory ' + type + ' does not exist');
}
car = new CarMaker[type]();
return car;
};
// small
CarMaker.small = function() {
this.wheels = 2;
};
CarMaker.small.prototype = Object.create(CarMaker.prototype);
// big
CarMaker.big = function() {
this.wheels = 4;
};
CarMaker.big.prototype = Object.create(CarMaker.prototype);
// instance
var celta = CarMaker.factory('small');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment