Selecting a version will take you to the chosen version of the ESLint docs.

no-this-before-super

Disallow this/super before calling super() in constructors

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

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

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

规则细节

这条规则的目的是在调用 super() 之前标记 this/super 关键字。

示例

使用此规则的错误示例:

Open in Playground
/*eslint no-this-before-super: "error"*/
/*eslint-env es6*/

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

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

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

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

使用此规则的正确示例:

Open in Playground
/*eslint no-this-before-super: "error"*/
/*eslint-env es6*/

class A {
    constructor() {
        this.a = 0; // OK, this class doesn't have an `extends` clause.
    }
}

class A extends B {
    constructor() {
        super();
        this.a = 0; // OK, this is after `super()`.
    }
}

class A extends B {
    foo() {
        this.a = 0; // OK. this is not in a constructor.
    }
}

何时不用

如果你不想被通知在构造函数的 super() 前使用 this/super,你可以安全地禁用这一规则。

Handled by TypeScript

It is safe to disable this rule when using TypeScript because TypeScript's compiler enforces this check.

Version

This rule was introduced in ESLint v0.24.0.

Resources

主题切换

切换到你所选择语言版本对应的 ESLint 网站。