Index

no-this-before-super

在构造函数中调用 super() 之前禁止 this/super

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

在派生类的构造函数中,如果在调用 super() 之前使用 this/super,会引发引用错误。

🌐 In the constructor of derived classes, if this/super are used before super() calls, it raises a reference error.

此规则检查构造函数中的 this/super 关键字,然后报告位于 super() 之前的那些关键字。

🌐 This rule checks this/super keywords in constructors, then reports those that are before super().

规则详情

🌐 Rule Details

此规则旨在在 super() 调用之前标记 this/super 关键字。

🌐 This rule is aimed to flag this/super keywords before super() callings.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-this-before-super: "error"*/

class A1 extends B {
    constructor() {
        this.a = 0;
        super();
    }
}

class A2 extends B {
    constructor() {
        this.foo();
        super();
    }
}

class A3 extends B {
    constructor() {
        super.foo();
        super();
    }
}

class A4 extends B {
    constructor() {
        super(this.foo());
    }
}

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

🌐 Examples of correct code for this rule:

在线运行
/*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.
    }
}

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 When Not To Use It

如果你不想在构造函数中在 super() 之前使用 this/super 时收到通知,你可以安全地禁用此规则。

🌐 If you don’t want to be notified about using this/super before super() in constructors, you can safely disable this rule.

由 TypeScript 处理

使用 TypeScript 时禁用此规则是安全的,因为 TypeScript 的编译器强制执行此检查。

版本

此规则是在 ESLint v0.24.0 中引入。

资源