[英]What should happen when an important constructor argument is missed?
I've created a class with es6 that requires a property to be defined in the options
argument. 我用es6创建了一个类,需要在
options
参数中定义一个属性。
class MyClass{
constructor(options){
if(typeof options.name === "undefined")
console.error("MyClass.constructor: options.name is not defined");
return this;
}
}
I can log an error, but I don't want the user of this class to continue. 我可以记录错误,但我不希望这个类的用户继续。 What do I return?
我该怎么回事? Should I still return the instance of the class?
我还应该返回班级的实例吗?
I would throw an error in this case. 在这种情况下我会抛出一个错误。 I'd do something like this:
我会做这样的事情:
class MyClass{
constructor(options){
if(typeof options.name === "undefined")
throw new Error("MyClass.constructor: options.name is not defined");
return this;
}
}
You can read more about throw here and here . 你可以在这里和这里阅读更多关于throw的内容。 This is a similar SO Q&A.
这是一个类似的问答。
If you are going to throw then you should use the appropriate error type so that the interface user can take the correct action. 如果您要抛出,则应使用相应的错误类型,以便界面用户可以采取正确的操作。 Include the filename and as much details as you can.
包括文件名和尽可能多的详细信息。 You want to help the API user as much as possible.
您希望尽可能地帮助API用户。
class MyClass{
constructor ( options ) {
if(options === undefined || options === null) {
throw new ReferenceError("MyClass constructor missing required argument `options`.", "filename.js");
} else
if(options.name === undefined) {
throw new ReferenceError("MyClass constructor missing required property `options.name`.", "filename.js");
} else
if( typeof options.name !== "string") {
throw new TypeError("Argument `options.name` should be a string.","filename.js");
} else
if( options.name === "" || options.name.trim() === ""){
throw new RangeError("Argument `options.name` has an invalid value. It requiers one or more alphaNumeric chanracters.","filename.js");
} else
if( options.name.length < 5 ){
console.warning("MyClass constructor. It is recogmended that 'options.name' has more than 5 characters");
}
... all good continue construction
}
}
This way the programmer has a choice of what to do. 这样程序员可以选择做什么。 It may require additional client input (a mistyped form) allowing the app to try again.
它可能需要额外的客户端输入(错误的表单),允许应用程序再次尝试。 Or the error can be handed on to reporting interfaces that can log the more serious problems.
或者可以将错误传递给可以记录更严重问题的报告界面。 We must always provide every opportunity to resolve the problems, and provide as much information as possible.
我们必须始终提供一切机会来解决问题,并提供尽可能多的信息。 There is nothing worse than non descriptive generic errors, the last thing someone using your API wants to do is have to go into it and find out what is going on and how to get past the error if there is a way.
没有什么比非描述性的通用错误更糟糕了,有人使用你的API想要做的最后一件事是必须进入它并找出发生了什么以及如果有办法如何通过错误。
If there aren't too many properties in the options I would probably define a default so I don't have to throw an error or prevent the code from continuing. 如果选项中没有太多属性,我可能会定义一个默认值,所以我不必抛出错误或阻止代码继续。
class A {
constructor({ name = 'Andy', age }) {
console.log(name, age) // Andy, 20
}
}
var a = new A({ age: 20 });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.