# unicorn/no-this-assignment

> Disallow assigning `this` to a variable

`const self = this` exists to reach the outer `this` from a nested `function`. An arrow function keeps the `this` from the surrounding code, so you don't need the extra variable.

Only a plain variable is reported. Destructuring (`const { name } = this`) and assigning to a property (`cache.owner = this`) are fine.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/no-this-assignment" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
class Timer {
  start() {
    const self = this;
    setInterval(function () {
      self.tick();
    }, 1000);
  }
}
```

Not reported:

{/* good */}
```js
class Timer {
  start() {
    setInterval(() => {
      this.tick();
    }, 1000);
  }
}
```

{/* good */}
```js
class Timer {
  start() {
    const { interval } = this;
    registry.current = this;
    setInterval(tick, interval);
  }
}
```
