class-methods-use-this
强制类方法使用 this
如果一个类方法不使用 this
,有时可以做成静态函数。如果你确实将该方法转换为静态函数,则调用该特定方法的类的实例也必须转换为静态调用(MyClass.callStaticMethod()
)
¥If a class method does not use this
, it can sometimes be made into a static function. If you do convert the method into a static function, instances of the class that call that particular method have to be converted to a static call as well (MyClass.callStaticMethod()
)
可能有一个不使用 this
的类方法,例如:
¥It’s possible to have a class method which doesn’t use this
, such as:
class A {
constructor() {
this.a = "hi";
}
print() {
console.log(this.a);
}
sayHi() {
console.log("hi");
}
}
let a = new A();
a.sayHi(); // => "hi"
在上面的例子中,sayHi
方法没有使用 this
,所以我们可以把它变成一个静态方法:
¥In the example above, the sayHi
method doesn’t use this
, so we can make it a static method:
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();
),而不是使用类调用的实例方法
¥Also note in the above examples that if you switch a method to a static method, instances of the class that call the static method (let a = new A(); a.sayHi();
) have to be updated to being a static call (A.sayHi();
) instead of having the instance of the class call the method
规则详情
¥Rule Details
此规则旨在标记不使用 this
的类方法。
¥This rule is aimed to flag class methods that do not use this
.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint class-methods-use-this: "error"*/
class A {
foo() {
console.log("Hello World"); /*error Expected 'this' to be used by class method 'foo'.*/
}
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint class-methods-use-this: "error"*/
class A {
foo() {
this.bar = "Hello World"; // OK, this is used
}
}
class B {
constructor() {
// OK. constructor is exempt
}
}
class C {
static foo() {
// OK. static methods aren't expected to use this.
}
static {
// OK. static blocks are exempt.
}
}
选项
¥Options
此规则有两个选项:
¥This rule has two options:
-
"exceptMethods"
允许使用此规则忽略指定的方法名称。¥
"exceptMethods"
allows specified method names to be ignored with this rule. -
"enforceForClassFields"
强制用作实例字段初始值设定项的函数使用this
。(默认:true
)¥
"enforceForClassFields"
enforces that functions used as instance field initializers utilizethis
. (default:true
)
exceptMethods
"class-methods-use-this": [<enabled>, { "exceptMethods": [<...exceptions>] }]
"exceptMethods"
选项允许你传递要忽略警告的方法名称数组。例如,你可能有来自外部库的规范,要求你将方法覆盖为常规函数(而不是静态方法),并且不使用函数体内的 this
。在这种情况下,你可以添加该方法以在警告中忽略。
¥The "exceptMethods"
option allows you to pass an array of method names for which you would like to ignore warnings. For example, you might have a spec from an external library that requires you to overwrite a method as a regular function (and not as a static method) and does not use this
inside the function body. In this case, you can add that method to ignore in the warnings.
当不使用 "exceptMethods"
时,此规则的错误代码示例:
¥Examples of incorrect code for this rule when used without "exceptMethods"
:
/*eslint class-methods-use-this: "error"*/
class A {
foo() {
}
}
与 exceptMethods 一起使用时此规则的正确代码示例:
¥Examples of correct code for this rule when used with exceptMethods:
/*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo", "#bar"] }] */
class A {
foo() {
}
#bar() {
}
}
enforceForClassFields
"class-methods-use-this": [<enabled>, { "enforceForClassFields": true | false }]
enforceForClassFields
选项强制用作实例字段初始值设定项的箭头函数和函数表达式使用 this
。(默认:true
)
¥The enforceForClassFields
option enforces that arrow functions and function expressions used as instance field initializers utilize this
. (default: true
)
使用 { "enforceForClassFields": true }
选项(默认)的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "enforceForClassFields": true }
option (default):
/*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
class A {
foo = () => {}
}
使用 { "enforceForClassFields": true }
选项(默认)的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "enforceForClassFields": true }
option (default):
/*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
class A {
foo = () => {this;}
}
使用 { "enforceForClassFields": false }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "enforceForClassFields": false }
option:
/*eslint class-methods-use-this: ["error", { "enforceForClassFields": false }] */
class A {
foo = () => {}
}
版本
此规则是在 ESLint v3.4.0 中引入。