Skip to content

Instantly share code, notes, and snippets.

@psaitu
Created August 18, 2014 11:49
Show Gist options
  • Select an option

  • Save psaitu/8dfb0c6364c24e736459 to your computer and use it in GitHub Desktop.

Select an option

Save psaitu/8dfb0c6364c24e736459 to your computer and use it in GitHub Desktop.
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