You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 carsfunctionCar(options){// some defaultsthis.doors=options.doors||4;this.state=options.state||"brand new";this.color=options.color||"silver";}// A constructor for defining new trucksfunctionTruck(options){this.state=options.state||"used";this.wheelSize=options.wheelSize||"large";this.color=options.color||"blue";}// FactoryExample.js// Define a skeleton vehicle factoryfunctionVehicleFactory(){}// Define the prototypes and utilities for this factory// Our default vehicleClass is CarVehicleFactory.prototype.vehicleClass=Car;// Our Factory method for creating new Vehicle instancesVehicleFactory.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)}returnnewthis.vehicleClass(options);};
The text was updated successfully, but these errors were encountered:
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 Carvf.createVehicle({vehicleClass: 'truck'});// Returns a Truckvf.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){letvehicleClass=this.vehicleClass;switch(options.vehicleType){case"car":
vehicleClass=Car;break;case"truck":
vehicleClass=Truck;// <------------------- THIS LINE /!\break;}returnnewvehicleClass(options);};
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.
The text was updated successfully, but these errors were encountered: