consistent-this
Enforce one name for variables that hold this
Code that saves this for a nested function calls the variable that, self, me or vm. If you use one name everywhere, you know what the variable holds without looking up its declaration.
The rule reports this assigned to any other name. It also reports the chosen name when it holds something other than this. The default name is that.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"consistent-this" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"consistent-this" = ["error", "that"]Examples#
example.js1 problem
function Timer() {
const self = thisUnexpected alias self for this.;
setInterval(function () {
self.tick();
}, 1000);
}example.js1 problem
const that = document.bodyDesignated alias that is not assigned to this.;example.jsNo problems
function Timer() {
const that = this;
setInterval(function () {
that.tick();
}, 1000);
}Destructuring isn't an alias, so it's allowed:
example.jsNo problems
function Timer() {
const { interval, tick } = this;
setInterval(tick, interval);
}Options#
The options are one or more names you allow:
bunfig.toml
[lint.rules]
consistent-this = ["error", "self", "vm"]example.jswith
"self", "vm"No problemsfunction Timer() {
const self = this;
setInterval(function () {
self.tick();
}, 1000);
}When not to use it#
An arrow function uses the surrounding code's this, so new code rarely needs an alias at all.