class-methods-use-this

强制类方法使用 this

如果一个类方法不使用this,有时可以做成静态函数。如果您确实将该方法转换为静态函数,则调用该特定方法的类的实例也必须转换为静态调用(MyClass.callStaticMethod()

可能有一个不使用 this 的类方法,例如:

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

    print() {
        console.log(this.a);
    }

    sayHi() {
        console.log("hi");
    }
}

let a = new A();
a.sayHi(); // => "hi"

在上面的例子中,sayHi 方法没有使用 this,所以我们可以把它变成一个静态方法:

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

    print() {
        console.log(this.a);
    }

    static sayHi() {
        console.log("hi");
    }
}

A.sayHi(); // => "hi"

还要注意,在上面的示例中,如果将方法切换为静态方法,则必须将调用静态方法 (let a = new A(); a.sayHi();) 的类的实例更新为静态调用 (A.sayHi();),而不是使用类调用的实例方法

规则详情

3dsZZEPTUaYeW/Kcxp+Iutri1VoLUEs7QMZRvT35nvEUQfEdk1/7yJL3RhLChxGWYG8TA+WfhHUS+kq71bVqTBXM1VAzVlkg7yzqBpUr7no=

53qbJC6xNcTpfWzDOxH3FbxQniXPcALMzc0838bz9bxwfaPpKG0ZV7pRGyxgA1aA

/*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/

class A {
    foo() {
        console.log("Hello World");     /*error Expected 'this' to be used by class method 'foo'.*/
    }
}

x8vS/D2xVZSiiHLs1PwUl6/TVEXV0uticb8dGcMm9rxiwxh8yRqnjwkHP0i+2Pam

/*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/
class A {
    foo() {
        this.bar = "Hello World"; // OK, this is used
    }
}

class A {
    constructor() {
        // OK. constructor is exempt
    }
}

class A {
    static foo() {
        // OK. static methods aren't expected to use this.
    }

    static {
        // OK. static blocks are exempt.
    }
}

选项

TVpOO4HtNdf6TdkvgmN0q7MpmuUjO8A3wLHkIiuG/Z0=

    Cwrp/fqRr97kszAn5laf9A3mZD22k11p5lvjlqZDzR4TFVGC4hxWem6mbrg/AwsI7tHcyT8/S4vlO/CN1rRsllm2hs16mdkA7O+Njcshsd43vsnv/lpomeZZhDpXf3hvqRNmZWcWSPNzBct3Lqik/TyvbThYh5lWUCgzrZzG4U3d73v7AhCbY38T1eVzonjKIrOV1BAuIuah16ZBN1R9XYQe34OOrV2qou2xf43CXw3/h/IFGjTj+AlSxl3wVSIIjJyCuk7T5hjZUYJ4Z171XJ69y1I3+vCxIxLqJW8DqV4mfi2KIb1EOBp2a/F7t0g7t7/58LMKNLBePYRD6zwPmQ==

exceptMethods

"class-methods-use-this": [<enabled>, { "exceptMethods": [<...exceptions>] }]

ZnCcVSXrH/uDP1sFgu/41bBYL1yBU57frVqtUFONyRlgGmUraAv6OAhRDEByzNe31gIdhV7SZtWXZnBQziAjiQeCSaTJaye6vY+7GoYNpiicUjyq6mmMnMfNe8uXQf6GIfi0KNyEf5GbOmIXGpyAB9XpLZE71NZ/Ujo2tIq7Wymu+owNb/UZuV1yxewIIY64Jb/vKKTq0HFJAGO1CR4f/DHfKd1WLFHPoB5IjRgFRnPt6YNyb8LSB4ijKjeKhtrZ0g+V2pggY5EfZ0bqDghI8dFd0mAoMugApqPEMMdSIEtB9Ewk99NUQAZ7eogGFJSPdgxSKW4NI2z1++SLT1OhIMCznFV57hlBE91oCS06Xl4klWdySqFUj69PuKsf1AEa39ldgYpuSeS93uTolOyYwUu2zY7Mldg1CEh55Ezpe5oX9Zsskmyf0eOSjKurFNDP

9CvoG7v8js/TZNKtpB9RtvHYPyWW95HPmqyIO1sH5n7XowX+CrrQIgiz4+yThyr+vsrSssWc485tnljx+JQObT1jP45gEVDlzseF/dbwWdA=

/*eslint class-methods-use-this: "error"*/

class A {
    foo() {
    }
}

oP9wV7VZfDtTU7GomINpuYfbcrC5mjN3grsiVK3rvR7w5NIMwazS0HhomeogOmE036HZ2bLdLikn248Bws1xtYHnz9kzeIw/8qvhFb4ykwU=

/*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo", "#bar"] }] */

class A {
    foo() {
    }
    #bar() {
    }
}

enforceForClassFields

"class-methods-use-this": [<enabled>, { "enforceForClassFields": true | false }]

J9aez3+OcTE6jEC2dKc0Jgf9BnshELAiVyRiDIU97VGhVm138NuJQKsgKG5bSuXf+AECArpWXaQDbdjxYmoQhtzFlpcEEiWugOMbzdbdNcviC5IkRn6OKi/Gb6ikH05c2UM3mP4EPHKvVEacVtVEvWr0UuMHh+GN8wt83xBfPrbYdBskT8lZ7bbOaWy1Qr2aiC23Ks/5qvY+Ko4YtGR5uclI2JzoLKU7a/rNxgCYtpk=

eUTYSZPuyabOkJ11PsjO9KQ6Yhy/t/nsB1DsfR98+Jz2co5eolcbLUyvBfdFF6cffvIxc5Zc5cXEZMi1KInRd3/FRKGptkeXzh41wPkpFtOYy8ScfEyt4mu+lIMvEMkvZ5A+rA+gPr8vXiu/vvTCcg==

/*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */

class A {
    foo = () => {}
}

eUTYSZPuyabOkJ11PsjO9KQ6Yhy/t/nsB1DsfR98+Jz2co5eolcbLUyvBfdFF6cffvIxc5Zc5cXEZMi1KInRd3/FRKGptkeXzh41wPkpFtNdAotlcIoy/Ltk48Xdr58tRKyxCEGiqKK6+btZinVpdQ==

/*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */

class A {
    foo = () => {this;}
}

5nEv4RCYCaBiSjFxJ4F/mIQGMu+6kmVdXYflrU3DMWJKrgIPNycKUk9X7OZs7gWwgHaZ8BsuwCtMZaQ+Xq4nOBHB9vw6x7mTBrU86i6tRsMUb+mhvMtOCzkBo3DC7gyx

/*eslint class-methods-use-this: ["error", { "enforceForClassFields": false }] */

class A {
    foo = () => {}
}