导出代码块而不只是功能

时间:2019-02-22 22:14:12

标签: javascript export

当前在我的代码中,我有VehicleFactory个单元测试对象。 VehicleFactory的{​​{1}}是在函数声明后分配的:

prototype

以我的笑话:

    export function VehicleFactory() {

    }

    VehicleFactory.prototype.vehicleClass = Car;

    VehicleFactory.prototype.createNewVehicle = function(options) {
        if( options.vehicleType === 'Car') {
            this.vehicleClass = Car;
        }
        else if( options.vehicleType === 'Truck') {
            this.vehicleClass = Truck;
        }

        return new this.vehicleClass(options);
    }

    var factory = new VehicleFactory();
    var car = factory.createNewVehicle( {
        vehicleType: "car",
        color: "yellow",
        doors: 6 } );

function Car(options) {
    if( options.brand != undefined)
        this.brand = options.brand;
    else
        this.brand = "Jeep";

    if( options.color != undefined)
        this.color = options.color;
    else
        this.color = "White";
}

function Truck(options) {
    /// ...
}


    console.log(car);

错误显示:

import VehicleFactory from '../VehicleFactory'
test('vehicleFactory_withCarOptions_AlwaysReturnsCar', () => {
  var factory = new VehicleFactory();
  var car = factory.createNewVehicle( {
    vehicleType: "car",
    color: "yellow",
    doors: 6 } );
  expect(car).toEqual({color: "yellow",
  doors: 6});
});

我的猜测是TypeError: _VehicleFactory.default is not a constructor 20 | 21 | test('vehicleFactory_withCarOptions_AlwaysReturnsCar', () => { > 22 | var factory = new VehicleFactory(); | ^ 23 | var car = factory.createNewVehicle( { 24 | vehicleType: "car", 25 | color: "yellow", 仅导出空函数,而不导出以下原型赋值?如何解决?

1 个答案:

答案 0 :(得分:0)

问题是我忘记了context.MapRoute( "User_default", "User/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ).DataTokens["area"] = AreaName; 中的括号

代替import

应为import VehicleFactory from '../VehicleFactory'

对于命名导出,我应该使用方括号

对于默认导出,我不需要括号,因为一个模块只有一个默认导出。