Disallow unnecessary constructor (no-useless-constructor)

禁用不必要的构造函数 (no-useless-constructor)

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:

ES2015 为没有指定构造函数的类提供了默认构造函数。因此,没有必要提供一个空的构造函数或只是简单的调用父类这样的构造函数,如下面的列子所示:

class A {
    constructor () {
    }
}

class A extends B {
    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"*/
/*eslint-env es6*/

class A {
    constructor () {
    }
}

class A extends B {
    constructor (...args) {
      super(...args);
    }
}

Examples of correct code for this rule:

正确 代码示例:

/*eslint no-useless-constructor: "error"*/

class A { }

class A {
    constructor () {
        doSomething();
    }
}

class A extends B {
    constructor() {
        super('foo');
    }
}

class A extends B {
    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.

如果你不想收到关于不必要的构造函数的通知,你也可以禁用此规则。

Version

This rule was introduced in ESLint 2.0.0-beta.1.

该规则在 ESLint 2.0.0-beta.1 中被引入。

Resources