跳转到内容

noUselessThisAlias

Diagnostic Category: lint/complexity/noUselessThisAlias

Since: v1.0.0

Sources:

Disallow useless this aliasing.

Arrow functions inherits this from their enclosing scope; this makes this aliasing useless in this situation.

class A {
method() {
const self = this;
return () => {
return self;
}
}
}
code-block.js:3:15 lint/complexity/noUselessThisAlias  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This aliasing of this is unnecessary.

1 │ class A {
2 │ method() {
> 3 │ const self = this;
^^^^^^^^^^^
4 │ return () => {
5 │ return self;

Arrow functions inherits this from their enclosing scope.

Safe fix: Use this instead of an alias.

1 1 class A {
2 2 method() {
3 - ········const·self·=·this;
4 3 return () => {
5 - ············return·self;
4+ ············return·this;
6 5 }
7 6 }

class A {
method() {
const self = this;
return function() {
this.g();
return self;
}
}
}