addyosmani / addyosmani/essential-js-design-patterns
Question about the vehicle factory example
- 主要語言
- HTML
- 星號
- 4.9k
- 分支
- 804
- PR 合併指標
- 30 天內沒有已合併 PR
描述
Won't [this line](https://github.com/addyosmani/essential-js-design-patterns/blob/master/book/index.html#L3038) 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.
```js
// 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 );
};
```
貢獻指南
這個儲存庫沒有索引到貢獻指南
評估
這個 Issue 還沒有評估資料。