Skip to content

noThisOutsideOfClass (JavaScript)

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

Disallow this outside of classes.

this can make its value difficult to understand. The rule allows this in class members and in TypeScript functions with an explicit this parameter.

An arrow function uses this from the code around it. Therefore, this is allowed in an arrow function inside a class member or a TypeScript function with an explicit this parameter.

function Person(name) {
this.name = name;
}
code-block.js:2:5 lint/nursery/noThisOutsideOfClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use this outside of a class.

1 │ function Person(name) {
> 2 │ this.name = name;
^^^^
3 │ }
4 │

this depends on how a function is called and may refer to an unexpected object.

Use a class member, or declare an explicit this parameter in a TypeScript function.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const getName = function () {
return this.name;
};
code-block.js:2:12 lint/nursery/noThisOutsideOfClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use this outside of a class.

1 │ const getName = function () {
> 2 │ return this.name;
^^^^
3 │ };
4 │

this depends on how a function is called and may refer to an unexpected object.

Use a class member, or declare an explicit this parameter in a TypeScript function.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

(function () {
this.initialize();
})();
code-block.js:2:5 lint/nursery/noThisOutsideOfClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use this outside of a class.

1 │ (function () {
> 2 │ this.initialize();
^^^^
3 │ })();
4 │

this depends on how a function is called and may refer to an unexpected object.

Use a class member, or declare an explicit this parameter in a TypeScript function.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const person = {
getName() {
return this.name;
},
};
code-block.js:3:16 lint/nursery/noThisOutsideOfClass ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use this outside of a class.

1 │ const person = {
2 │ getName() {
> 3 │ return this.name;
^^^^
4 │ },
5 │ };

this depends on how a function is called and may refer to an unexpected object.

Use a class member, or declare an explicit this parameter in a TypeScript function.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

class Person {
constructor(name) {
this.name = name;
}
getName = () => this.name;
}
function getName(this: Person) {
return this.name;
}