|
| 1 | +import { VehicleFactory, VehicleType } from '../src/VehicleFactory.js' |
| 2 | + |
| 3 | +export function test() { |
| 4 | + const vehicleFactory = new VehicleFactory() |
| 5 | + |
| 6 | + // 获取Bus对象,并调用它的 run 方法 |
| 7 | + const bus = vehicleFactory.getVehicle(VehicleType.BUS) |
| 8 | + bus.run() |
| 9 | + |
| 10 | + // 获取Car对象,并调用它的 run 方法 |
| 11 | + const car = vehicleFactory.getVehicle(VehicleType.CAR) |
| 12 | + car.run() |
| 13 | + |
| 14 | + // 获取Motorcycle对象,并调用它的 run 方法 |
| 15 | + const motorcycle = vehicleFactory.getVehicle(VehicleType.MOTORCYCLE) |
| 16 | + motorcycle.run() |
| 17 | + |
| 18 | + // 获取Motorcycle对象,并调用它的 run 方法 |
| 19 | + const van = vehicleFactory.getVehicle(VehicleType.VAN) |
| 20 | + van.run() |
| 21 | +} |
| 22 | + |
| 23 | +// 执行测试 |
| 24 | +;(function () { |
| 25 | + console.log('test start:') |
| 26 | + test() |
| 27 | +})() |
| 28 | +/** |
| 29 | +jarry@jarrys-MacBook-Pro js % node --version |
| 30 | +v16.15.1 |
| 31 | + */ |
| 32 | +/* |
| 33 | +// node run |
| 34 | +jarry@jarrys-MacBook-Pro js % node -v |
| 35 | +v16.15.1 |
| 36 | +jarry@jarrys-MacBook-Pro js % node test/test.js |
| 37 | +test start: |
| 38 | +Bus::run(). |
| 39 | +Car::run(). |
| 40 | +Motorcycle::run(). |
| 41 | +Van::run(). |
| 42 | +*/ |
| 43 | + |
| 44 | +/* |
| 45 | +// npm run |
| 46 | +jarry@jarrys-MacBook-Pro js % npm run test |
| 47 | +
|
| 48 | + |
| 49 | +> node test/test.js |
| 50 | +
|
| 51 | +test start: |
| 52 | +Bus::run(). |
| 53 | +Car::run(). |
| 54 | +Motorcycle::run(). |
| 55 | +Van::run(). |
| 56 | +*/ |
| 57 | + |
| 58 | +/** |
| 59 | +// n run |
| 60 | +jarry@jarrys-MacBook-Pro js % n run 14.17.0 test/test.js |
| 61 | +test start: |
| 62 | +Bus::run(). |
| 63 | +Car::run(). |
| 64 | +Motorcycle::run(). |
| 65 | +Van::run(). |
| 66 | +*/ |
| 67 | + |
| 68 | +/* |
| 69 | +// node 10.24.1 isn't support es module |
| 70 | +// Node 13.2.0 can support ES Modules |
| 71 | +jarry@jarrys-MacBook-Pro js % n run 10.24.1 test/test.js |
| 72 | +/Users/jarry/github/design-pattern/factory-pattern/js/test/test.js:1 |
| 73 | +import { VehicleFactory, VehicleType } from '../src/VehicleFactroy.js' |
| 74 | + ^ |
| 75 | +
|
| 76 | +SyntaxError: Unexpected token { |
| 77 | + at Module._compile (internal/modules/cjs/loader.js:723:23) |
| 78 | + at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) |
| 79 | + at Module.load (internal/modules/cjs/loader.js:653:32) |
| 80 | + at tryModuleLoad (internal/modules/cjs/loader.js:593:12) |
| 81 | + at Function.Module._load (internal/modules/cjs/loader.js:585:3) |
| 82 | + at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) |
| 83 | + at startup (internal/bootstrap/node.js:283:19) |
| 84 | + at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3) |
| 85 | +jarry@jarrys-MacBook-Pro js % |
| 86 | +*/ |
0 commit comments