no-constructor-return
禁止從建構子回傳值
在 JavaScript 中,於類別的建構子中回傳值可能是一個錯誤。禁止此模式可以預防因不熟悉該語言或複製貼上錯誤而造成的錯誤。
規則詳情
此規則禁止在類別的建構子中使用 return 語句。請注意,允許不回傳任何值。
此規則的錯誤程式碼範例
在遊樂場開啟
/*eslint no-constructor-return: "error"*/
class A {
constructor(a) {
this.a = a;
}
}
class B {
constructor(f) {
if (!f) {
}
}
}
此規則的正確程式碼範例
在遊樂場開啟
/*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 中引入。