Javascript es6类处理错误

Javascript es6类处理错误,javascript,class,error-handling,Javascript,Class,Error Handling,我已经创建了一个JS类。以下是代码: export default class Service { constructor( serviceId, serviceName, serviceDescription, serviceImageName, categoryId, servicePrice, currencyCode, acceptPayment,

我已经创建了一个JS类。以下是代码:

export default class Service {
    constructor(
        serviceId,
        serviceName,
        serviceDescription,
        serviceImageName,
        categoryId,
        servicePrice,
        currencyCode,
        acceptPayment,
        serviceDuration,
        multipleBookingPerSlot,
        mode,
        tzSupport,
        minOptionCount
    ) {
        try{
            this.id = serviceId;
            this.title = serviceName;
            this.subTitle = serviceDescription;
            this.imageUrl = serviceImageName;
            this.categoryId = categoryId;
            this.price = servicePrice;
            this.currencyCode = currencyCode;
            this.acceptPayment = acceptPayment;
            this.meetingDuration = serviceDuration;
            this.multipleBookingPerSlot = multipleBookingPerSlot;
            this.serviceName = serviceName;
            this.mode = mode;
            this.tzSupport = tzSupport;
            this.session = minOptionCount
        } catch(e){
            if(e instanceof ReferenceError){
                console.error("Service data missing.")
            }
        }

    }
}

我的目标是每当
服务的新对象创建时,如
新服务('1')
,如果任何键丢失,代码应抛出错误并停止执行。如何实现这一点?

正如我已经说过的,将未定义的赋值给对象属性是完全有效的。解决方案可能是对照未定义的参数检查Arraylike的值:

constructor(a,b,c){
   if(arguments.length!=3){//check length
     return;
   }
   for(var a=0;a<arguments.length;a++){
      if(arguments[a]===undefined){//check against undefined
          return;
       }
    }
  //your code
}
构造函数(a、b、c){
如果(arguments.length!=3){//检查长度
返回;
}

对于(var a=0;a

您不会得到
ReferenceError
如果调用者没有提供足够的参数,您只会在参数中看到
undefined

您有13个参数(太多了)。您可以使用蛮力:

if (arguments.length < 13) {
    throw new Error("Missing arguments");
}
用法:

let s = new Service({id: 1, title: "foo", /*...etc...*/});
这样,调用者就不会迷失在参数的海洋中


然而如果验证参数值很重要,那么验证它们的值不是也很重要吗?没有什么可以阻止我使用13个完全无效的参数调用
新服务
未定义
重复13次)

因此,我可能会使用一个选项对象(因为调用者更容易使用),并结合参数分解,然后进行单独验证,例如:

export default class Service {
    constructor({                 // <== Notice the {
        id,
        name,
        decription,
        imageUrl,
        categoryId,
        price,
        currencyCode,
        acceptPayment,
        meetingDuration,
        multipleBookingPerSlot,
        mode,
        tzSupport,
        minOptionCount
    }) {                          // <== And the }
        this.id = validate.positiveNumber(id);
        this.title = validate.nonBlank(name);
        this.subTitle = validate.nonBlank(description);
        this.imageUrl = validate.URL(imageUrl);
        this.categoryId = validate.positiveNumber(categoryId);
        this.price = validate.price(price);
        this.currencyCode = validate.currencyCode(currencyCode);
        this.acceptPayment = validate.boolean(acceptPayment);
        this.meetingDuration = validate.duration(meetingDuration);
        this.multipleBookingPerSlot = validate.boolean(multipleBookingPerSlot);
        this.serviceName = this.title; // Already validated
        this.mode = validate.mode(mode);
        this.tzSupport = validate.tzSupport(tzSupport);
        this.session = validate.whateverThisIs(minOptionCount);
    }
}

如果(arguments.length!=13){return}此.id=undefined;完全有效且不会引发任何错误…如果
0
false
NaN
null
,或
undefined
是一个或多个选项的有效值怎么办?
!参数[a]
如果参数为
false
0
'
等,则为true。我如何在此处使用
bulider模式?
export default class Service {
    constructor({                 // <== Notice the {
        id,
        name,
        decription,
        imageUrl,
        categoryId,
        price,
        currencyCode,
        acceptPayment,
        meetingDuration,
        multipleBookingPerSlot,
        mode,
        tzSupport,
        minOptionCount
    }) {                          // <== And the }
        this.id = validate.positiveNumber(id);
        this.title = validate.nonBlank(name);
        this.subTitle = validate.nonBlank(description);
        this.imageUrl = validate.URL(imageUrl);
        this.categoryId = validate.positiveNumber(categoryId);
        this.price = validate.price(price);
        this.currencyCode = validate.currencyCode(currencyCode);
        this.acceptPayment = validate.boolean(acceptPayment);
        this.meetingDuration = validate.duration(meetingDuration);
        this.multipleBookingPerSlot = validate.boolean(multipleBookingPerSlot);
        this.serviceName = this.title; // Already validated
        this.mode = validate.mode(mode);
        this.tzSupport = validate.tzSupport(tzSupport);
        this.session = validate.whateverThisIs(minOptionCount);
    }
}
let s = new Service({id: 1, title: "foo", /*...etc...*/});