简体   繁体   English

当错过重要的构造函数参数时会发生什么?

[英]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.

相关问题 在angular2路由应用中单击“后退”按钮会发生什么? - What should happen when clicking the back button in an angular2 routing app? 如果关键字后面的表达式不符合承诺,`await`会发生什么? - What should happen with `await` when the expression after the keyword does not evaluate to promise? 如果从此代码中漏掉了[[]],那意味着什么? - What does it mean when the `[]` are missed out from this code? ..a原型构造函数或对象构造函数本身应该是什么构造函数属性值 - What should be the constructor properties value ..a prototypes constructor or the objects constructor itself 当脚本被加载为“预加载”/“模块预加载”时会发生什么? - What happen when a script is loaded as “preload”/“modulepreload”? 当替换元素时,事件和数据会发生什么 - When replace element, what happen on the events and data 如果您用另一个承诺解决一个承诺,应该怎么办? - What should happen if you resolve a promise with another promise? 应该!重要的是在这里使用? - Should !important be used here? InputEvent 构造函数的 typeArg 参数的有效字符串是什么? - What are the valid strings for the typeArg argument of the InputEvent constructor? 滚动条在不使用时淡出。 这不应该发生 - Scrollbar fades out when not in use. This should not happen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM