Created
March 29, 2019 06:10
-
-
Save ZackFox/742bd974f62d32af6b519e868d21def2 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
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