Created
June 11, 2014 19:26
-
-
Save mauriciosoares/ef4f3b120c13c98b2047 to your computer and use it in GitHub Desktop.
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
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