no-useless-constructor
禁止不必要的构造函数
此规则报告的一些问题可通过编辑器建议手动修复
如果未指定,ES2015 会提供默认的类构造函数。因此,没有必要提供一个空的构造函数或一个简单地委托给其父类的构造函数,如下例所示:
¥ES2015 provides a default class constructor if one is not specified. As such, it is unnecessary to provide an empty constructor or one that simply delegates into its parent class, as in the following examples:
class A {
constructor () {
}
}
class B extends A {
constructor (value) {
super(value);
}
}
规则详情
¥Rule Details
此规则标记可以安全删除而不更改类的工作方式的类构造函数。
¥This rule flags class constructors that can be safely removed without changing how the class works.
示例
¥Examples
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-useless-constructor: "error"*/
class A {
constructor () {
}
}
class B extends A {
constructor (...args) {
super(...args);
}
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-useless-constructor: "error"*/
class A { }
class B {
constructor () {
doSomething();
}
}
class C extends A {
constructor() {
super('foo');
}
}
class D extends A {
constructor() {
super();
doSomething();
}
}
何时不使用
¥When Not To Use It
如果你不想收到有关不必要构造函数的通知,你可以安全地禁用此规则。
¥If you don’t want to be notified about unnecessary constructors, you can safely disable this rule.
版本
此规则是在 ESLint v2.0.0-beta.1 中引入。