Index

no-constructor-return

不允许从构造函数返回值

在 JavaScript 中,在类的构造函数中返回一个值可能是一个错误。禁止这种模式可以防止由于对语言不熟悉或复制粘贴错误而导致的错误。

🌐 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.

规则详情

🌐 Rule Details

此规则不允许在类的构造函数中使用 return 语句。请注意,返回空值是允许的。

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

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-constructor-return: "error"*/

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

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

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-constructor-return: "error"*/

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

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

        f();
    }
}

class E {
    constructor() {
        return;
    }
}

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

版本

此规则是在 ESLint v6.7.0 中引入。

资源