no-this-before-super
禁止在建構子中呼叫 super()
之前使用 this
/super
✅ 建議
在設定檔中使用來自 @eslint/js
的 recommended
設定會啟用此規則
在衍生類別的建構子中,如果在呼叫 super()
之前使用 this
/super
,會引發參考錯誤。
此規則檢查建構子中的 this
/super
關鍵字,然後回報那些在 super()
之前的。
規則詳情
此規則旨在標記在 super()
呼叫之前的 this
/super
關鍵字。
範例
此規則的 錯誤 程式碼範例
在 Playground 中開啟
/*eslint no-this-before-super: "error"*/
class A1 extends B {
constructor() {
.a = 0;
super();
}
}
class A2 extends B {
constructor() {
.foo();
super();
}
}
class A3 extends B {
constructor() {
.foo();
super();
}
}
class A4 extends B {
constructor() {
super(.foo());
}
}
此規則的 正確 程式碼範例
在 Playground 中開啟
/*eslint no-this-before-super: "error"*/
class A1 {
constructor() {
this.a = 0; // OK, this class doesn't have an `extends` clause.
}
}
class A2 extends B {
constructor() {
super();
this.a = 0; // OK, this is after `super()`.
}
}
class A3 extends B {
foo() {
this.a = 0; // OK. this is not in a constructor.
}
}
何時不該使用
如果您不希望收到關於在建構子中於 super()
之前使用 this
/super
的通知,您可以安全地停用此規則。
由 TypeScript 處理
當使用 TypeScript 時,停用此規則是安全的,因為 TypeScript 的編譯器會強制執行此檢查。
版本
此規則在 ESLint v0.24.0 中引入。