Skip to content

noUselessThisAlias (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"complexity": {
"noUselessThisAlias": "error"
}
}
}
}

Disallow aliases for this when this can be used directly.

Arrow functions use this from their surrounding scope. In that situation, assigning this to another variable adds an unnecessary name without changing which value is used.

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