Skip to content

Instantly share code, notes, and snippets.

@gpDA
Created July 8, 2020 06:29
Show Gist options
  • Save gpDA/24543776092263b067c05e14e61ff06a to your computer and use it in GitHub Desktop.
Save gpDA/24543776092263b067c05e14e61ff06a to your computer and use it in GitHub Desktop.
// 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