noInvalidThis (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/noInvalidThis - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-invalid-this
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noInvalidThis": "error" } } }}Description
Section titled “Description”Disallow this in places where it is undefined.
Inside a function, the value of this depends on how the function is called. When the
function is called as a method, as in obj.foo(), this is obj. When it’s called on its
own, as in foo(), this is undefined in modules and classes. At the top level of a
module, this is always undefined. Reading a property of undefined throws an error, so
using this in these places is usually a mistake.
This rule reports this at the top level of a module and in functions that are unlikely to
be called as a method. It allows this in:
- class members and object methods;
- functions assigned to an object property, such as
obj.foo = function () {}; - functions whose name starts with an uppercase letter, and functions without a name that
are assigned to a variable whose name starts with an uppercase letter, because they’re
likely constructors called with
new; - functions called with
.call(),.apply(), or.bind()and an object to use asthis, such as(function () {}).call(obj); - functions passed to
Reflect.apply(),Array.from(), or array methods such as.forEach()together with an object to use asthis; - functions that declare the type of
thiswith a TypeScriptthisparameter, such asfunction foo(this: Obj) {}; - functions in CommonJS files (
.cjs) that don’t start with"use strict", becausethisis the global object there.
Arrow functions don’t have their own this. They use the this of the code around them,
so the rule checks them against that code.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”this.a = 0;code-block.js:1:1 lint/nursery/noInvalidThis ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected this at the top level of a module.
> 1 │ this.a = 0;
│ ^^^^
2 │
ℹ this is always undefined at the top level of a module.
ℹ Use globalThis to refer to the global object.
ℹ 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 foo() { this.a = 0;}code-block.js:2:5 lint/nursery/noInvalidThis ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected this in a function that isn't a method or a constructor.
1 │ function foo() {
> 2 │ this.a = 0;
│ ^^^^
3 │ }
4 │
ℹ When a function is called on its own instead of as a method, this is undefined, and reading its properties throws an error.
ℹ Pass the value as a parameter instead, or turn the function into a method. In TypeScript, if callers provide this, declare its type with a this parameter.
ℹ 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.
foo(function () { this.a = 0;});code-block.js:2:5 lint/nursery/noInvalidThis ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected this in a function that isn't a method or a constructor.
1 │ foo(function () {
> 2 │ this.a = 0;
│ ^^^^
3 │ });
4 │
ℹ When a function is called on its own instead of as a method, this is undefined, and reading its properties throws an error.
ℹ Pass the value as a parameter instead, or turn the function into a method. In TypeScript, if callers provide this, declare its type with a this parameter.
ℹ 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 foo = () => { this.a = 0;};code-block.js:2:5 lint/nursery/noInvalidThis ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected this at the top level of a module.
1 │ const foo = () => {
> 2 │ this.a = 0;
│ ^^^^
3 │ };
4 │
ℹ this is always undefined at the top level of a module.
ℹ Use globalThis to refer to the global object.
ℹ 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 Foo() { this.a = 0;}
const Bar = function () { this.a = 0;};
const obj = { foo() { this.a = 0; }, bar: function () { this.a = 0; },};
obj.baz = function () { this.a = 0;};
class Baz { a = this;
constructor() { this.a = 0; }}
list.forEach(function () { this.a = 0;}, obj);
(function () { this.a = 0;}).call(obj);
function qux(this: Obj) { this.a = 0;}See Also
Section titled “See Also”Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.