Skip to content

Instantly share code, notes, and snippets.

@ZackFox
Created March 29, 2019 06:10
Show Gist options
  • Save ZackFox/742bd974f62d32af6b519e868d21def2 to your computer and use it in GitHub Desktop.
Save ZackFox/742bd974f62d32af6b519e868d21def2 to your computer and use it in GitHub Desktop.
function CarFactory (){}
CarFactory.prototype.drive = function(){
console.log(`i have ${this.doors} doors`);
}
CarFactory.make = function(type){
if(typeof CarFactory[type] !== "function" ){
throw Error("Car not found");
}
CarFactory[type].prototype = new CarFactory();
return new CarFactory[type]();
}
CarFactory.Compact = function () {
this.doors = 4;
};
CarFactory.Convertible = function () {
this.doors = 2;
};
CarFactory.SUV = function () {
this.doors = 24;
};
const test1 = CarFactory.make("Convertible");
const test2 = CarFactory.make("Compact")
test1.drive();
test2.drive();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment