Created
July 8, 2020 06:29
-
-
Save gpDA/24543776092263b067c05e14e61ff06a 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
| // A constructor for defining new cars | |
| function Car (options) { | |
| // some defaults | |
| this.doors = options.doors || 4; | |
| this.state = options.state || "brand new"; | |
| this.color = options.color || "silver"; | |
| } | |
| // A constructor for defining new trucks | |
| function Truck (options) { | |
| // some defaults | |
| this.state = options.state || "used"; | |
| this.wheelSize = options.wheelSize || "large"; | |
| this.color = options.color || "blue"; | |
| } | |
| // Define a skeleton vehicle factory | |
| function VehicleFactory() {} | |
| // Default vehicleClass is Car | |
| VehicleFactory.prototype.vehicleClass = Car; | |
| // Factory method for creating new Vehicle instances | |
| VehicleFactory.prototype.createVehicle = function (options) { | |
| if (options.vehicleType === "car") { | |
| this.vehicleClass = Car; | |
| } else { | |
| this.vehicleClass = Truck; | |
| } | |
| return new this.vehicleClass(options); | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment