Created
August 18, 2014 11:49
-
-
Save psaitu/8dfb0c6364c24e736459 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 CarDoor(options) { | |
| this.color = options.color || "red"; | |
| this.side = options.side || "right"; | |
| this.hasPowerWindows = options.hasPowerWindows || true; | |
| } | |
| function CarSeat(options) { | |
| this.color = options.color || "gray"; | |
| this.material = options.material || "leather"; | |
| this.isReclinable = options.isReclinable || true; | |
| } | |
| function CarPartFactory() {} | |
| CarPartFactory.prototype.createPart = function(options) { | |
| var parentClass = null; | |
| if( options.partType === 'door' ) { | |
| parentClass = CarDoor; | |
| } else if( options.partType === 'seat' ) { | |
| parentClass = CarSeat; | |
| } | |
| if( parentClass === null ) { | |
| return false; | |
| } | |
| return new parentClass( options ); | |
| } | |
| var myPartFactory = new CarPartFactory(); | |
| var seat = myPartFactory.createPart( { | |
| partType : 'seat', | |
| material : 'leather', | |
| color : 'blue', | |
| isReclinable : false | |
| } ); | |
| // outputs: true | |
| console.log( seat instanceof CarSeat ); | |
| // outputs a CarSeat object with material "leather", color "blue", isReclinable "false" | |
| console.log( seat ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment