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>] }]

jPPLrpbK91V3/Mg2PbG85/cvp9LzpPWpn6nwDOmh5tZJN72KlwZwaPJaOKATHhYIiNFVT6XI8SWRFdL+9a4U1fHFPorprnkNTjaodjmPYcjX/nfCCnYtz0ZKdXnrohIbJZ0fMC+pSObIEoAY1Kbz/TQV8R8u+Od+64L4f0e98WG+Mm2okaFsRYgvJ+l108AEnq4TapWUTUjyWgSrooPEa5L6UXQojXlT4971yfQe1LaGD8+WMBS7TsPjpHZtvL2ni6XQShgSmYfSjB2H6jC7I2l0MgnpoGQMfG0x3BAZJmhGVit3hqDtXgj6173+W3+gWEh6EL/XfjQDfC8pRdMAHlZ3RplUpU7yMBS49B3m5Ob9PQtQEEoVVI+78ibBWk0cJQ37VceVsC4sNVi+SbTYzh0WSHrS7puGuCUfvp3IdIQWhm9w/qGCsPumSygUQqGH

3Q1mfO3m9MDiFNdkPSX/VttCK/xeEfl47u5DwY+T+bVdL2zPwT8JHSIQOkZoESV353Bk6Gd9ZXDNH3WtLTG3FtLECABF/Y4VKUSy5ZWz55RpztPK7Dj23FEWbGzXPdOr

/*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 = () => {}
}