【发布时间】:2016-05-14 18:22:23
【问题描述】:
我用 es6 创建了一个类,它需要在 options 参数中定义一个属性。
class MyClass{
constructor(options){
if(typeof options.name === "undefined")
console.error("MyClass.constructor: options.name is not defined");
return this;
}
}
我可以记录错误,但我不希望此类用户继续。我要返回什么?我还应该返回类的实例吗?
【问题讨论】:
-
这是否意味着创建此类的每个人都需要将其包装在 try/catch 中?
-
没有。 JS 异常是未经检查的,就像 java 中的 RuntimeExceptions。
-
我会退出程序并尝试修复代码,我是认真的 x-)
-
我的意思是,在这种情况下可能存在设计问题。
标签: javascript