Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about the vehicle factory example #224

Open
alvarocastro opened this issue Aug 23, 2019 · 2 comments
Open

Question about the vehicle factory example #224

alvarocastro opened this issue Aug 23, 2019 · 2 comments

Comments

@alvarocastro
Copy link

alvarocastro commented Aug 23, 2019

Won't this line of the example change the default vehicle class for the next createVehicle call?

Here is the code

Brought the code here. I put a comment to point the line I'm taling about, it's near the end.

// 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){
 
  this.state = options.state || "used";
  this.wheelSize = options.wheelSize || "large";
  this.color = options.color || "blue";
}
 
 
// FactoryExample.js
 
// Define a skeleton vehicle factory
function VehicleFactory() {}
 
// Define the prototypes and utilities for this factory
 
// Our default vehicleClass is Car
VehicleFactory.prototype.vehicleClass = Car;
 
// Our Factory method for creating new Vehicle instances
VehicleFactory.prototype.createVehicle = function ( options ) {
 
  switch(options.vehicleType){
    case "car":
      this.vehicleClass = Car;
      break;
    case "truck":
      this.vehicleClass = Truck; // <------------------- THIS LINE /!\
      break;
    //defaults to VehicleFactory.prototype.vehicleClass (Car)
  }
 
  return new this.vehicleClass( options );
};

@nitink4472
Copy link

nitink4472 commented Nov 14, 2019

The default statement under switch can't be under comment (//).

@alvarocastro
Copy link
Author

alvarocastro commented Nov 14, 2019

What I meant is that if I don't specify the vehicleType in the options object, I get a new vehicle of the tipe it was last created, and not of the defaulted type, becase in each createVehicle call, the this.vehicleClass is changed (if a type is provided)

vf.createVehicle(); // Returns a Car
vf.createVehicle({ vehicleClass: 'truck'}); // Returns a Truck
vf.createVehicle(); // Returns a Truck

What confuses me is: why change the value of this.vehicleClass and don't use it as a read-only?

Wouldn't this be more clear/predictive and less messy?

VehicleFactory.prototype.createVehicle = function ( options ) {
  let vehicleClass = this.vehicleClass;

  switch(options.vehicleType){
    case "car":
      vehicleClass = Car;
      break;
    case "truck":
      vehicleClass = Truck; // <------------------- THIS LINE /!\
      break;
  }
 
  return new vehicleClass( options );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants