[英]Javascript es6 class handling error
我创建了一个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.")
}
}
}
}
我的目标是,如果Service
新对象创建时像new Service('1')
如果缺少任何键,应该抛出错误并停止执行。 我怎样才能做到这一点?
如果调用者没有提供足够的参数,则不会收到ReferenceError
,您只会在参数中看到undefined
。
您有13个参数(实在太多了)。 您可以做蛮力的事情:
if (arguments.length < 13) {
throw new Error("Missing arguments");
}
但是,我建议改为使用构建器模式或选项对象,而不要使用13个离散参数。 超过三个参数很难管理。
例如,带有选项对象:
export default class Service {
constructor(
options
) {
["id", "title", "subTitle", "imageUrl", "categoryId", "price", "currencyCode",
"acceptPayment", "meetingDuration", "multipleBookingPerSlot", "serviceName",
"mode", "tzSupport", "session"].forEach(name => {
if (!options.hasOwnProperty(name)) {
throw new Error(name + " is a required option");
}
});
Object.assign(this, options);
}
}
用法:
let s = new Service({id: 1, title: "foo", /*...etc...*/});
这样,调用者就不会迷失在众多的参数中。
但是 ,如果验证参数值是否存在很重要,验证参数值是否也很重要? 没有什么可以阻止我使用13个完全无效的参数(例如, undefined
重复13次)来调用new Service
。
因此,我可能会使用一个options对象(因为对调用者而言它要容易得多)与参数解构然后进行单独验证相结合,例如:
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);
}
}
... validate
是一组可重复使用的验证。 用法与上面相同:
let s = new Service({id: 1, title: "foo", /*...etc...*/});
正如我已经评论过的,将undefined分配给objects属性是完全有效的。 解决方案可能是针对undefined检查参数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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.