Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 960 Bytes

no-constructor-return.md

File metadata and controls

58 lines (42 loc) · 960 Bytes
规则名 规则类型
no-constructor-return
problem

In JavaScript, returning a value in the constructor of a class may be a mistake. Forbidding this pattern prevents mistakes resulting from unfamiliarity with the language or a copy-paste error.

规则详解

This rule disallows return statements in the constructor of a class. Note that returning nothing with flow control is allowed.

此规则的 错误 代码实例:

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

class A {
    constructor(a) {
        this.a = a;
        return a;
    }
}

class B {
    constructor(f) {
        if (!f) {
            return 'falsy';
        }
    }
}

此规则的 正确 代码实例:

::: correct

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

class C {
    constructor(c) {
        this.c = c;
    }
}

class D {
    constructor(f) {
        if (!f) {
            return;  // Flow control.
        }

        f();
    }
}