版本

no-constructor-return

禁止從建構函式傳回值

在 JavaScript 中,在類別的建構函式中傳回值可能是錯誤。禁止這種模式可以防止因不熟悉語言或複製貼上錯誤而導致的錯誤。

規則詳情

此規則禁止在類別的建構函式中使用 return 語句。請注意,允許不傳回任何值。

此規則的錯誤程式碼範例

在遊樂場中開啟
/*eslint no-constructor-return: "error"*/

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

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

此規則的正確程式碼範例

在遊樂場中開啟
/*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;
    }
}

版本

此規則在 ESLint v6.7.0 中引入。

資源

變更語言