1 # Disallow `this` keywords outside of classes or class-like objects. (no-invalid-this)
3 Under the strict mode, `this` keywords outside of classes or class-like objects might be `undefined` and raise a `TypeError`.
7 This rule aims to flag usage of `this` keywords outside of classes or class-like objects.
9 Basically, this rule checks whether or not a function containing `this` keyword is a constructor or a method.
11 This rule judges from following conditions whether or not the function is a constructor:
13 * The name of the function starts with uppercase.
14 * The function is assigned to a variable which starts with an uppercase letter.
15 * The function is a constructor of ES2015 Classes.
17 This rule judges from following conditions whether or not the function is a method:
19 * The function is on an object literal.
20 * The function is assigned to a property.
21 * The function is a method/getter/setter of ES2015 Classes.
23 And this rule allows `this` keywords in functions below:
25 * The `call/apply/bind` method of the function is called directly.
26 * The function is a callback of array methods (such as `.forEach()`) if `thisArg` is given.
27 * The function has `@this` tag in its JSDoc comment.
29 And this rule always allows `this` keywords in the following contexts:
31 * In class field initializers.
32 * In class static blocks.
34 Otherwise are considered problems.
36 This rule applies **only** in strict mode.
37 With `"parserOptions": { "sourceType": "module" }` in the ESLint configuration, your code is in strict mode even without a `"use strict"` directive.
39 Examples of **incorrect** code for this rule in strict mode:
42 /*eslint no-invalid-this: "error"*/
60 var foo = function() {
71 // `this` of arrow functions is the outer scope's.
77 return function foo() {
78 // There is in a method `aaa`, but `foo` is not a method.
85 foo.forEach(function() {
91 Examples of **correct** code for this rule in strict mode:
94 /*eslint no-invalid-this: "error"*/
100 // OK, this is in a legacy style constructor.
107 // OK, this is in a constructor.
114 foo: function foo() {
115 // OK, this is in a method (this function is on object literal).
122 // OK, this is in a method (this function is on object literal).
129 // OK, this is in a method (this function is on object literal).
134 var obj = Object.create(null, {
135 foo: {value: function foo() {
136 // OK, this is in a method (this function is on object literal).
141 Object.defineProperty(obj, "foo", {
142 value: function foo() {
143 // OK, this is in a method (this function is on object literal).
148 Object.defineProperties(obj, {
149 foo: {value: function foo() {
150 // OK, this is in a method (this function is on object literal).
156 this.foo = function foo() {
157 // OK, this is in a method (this function assigns to a property).
163 obj.foo = function foo() {
164 // OK, this is in a method (this function assigns to a property).
168 Foo.prototype.foo = function foo() {
169 // OK, this is in a method (this function assigns to a property).
175 // OK, this is in a class field initializer.
178 // OK, static initializers also have valid this.
182 // OK, this is in a method.
188 // OK, this is in a method (static methods also have valid this).
194 // OK, static blocks also have valid this.
200 var foo = (function foo() {
201 // OK, the `bind` method of this function is called directly.
205 foo.forEach(function() {
206 // OK, `thisArg` of `.forEach()` is given.
213 // OK, this function has a `@this` tag in its JSDoc comment.
220 This rule has an object option, with one option:
222 * `"capIsConstructor": false` (default `true`) disables the assumption that a function which name starts with an uppercase is a constructor.
226 By default, this rule always allows the use of `this` in functions which name starts with an uppercase and anonymous functions that are assigned to a variable which name starts with an uppercase, assuming that those functions are used as constructor functions.
228 Set `"capIsConstructor"` to `false` if you want those functions to be treated as 'regular' functions.
230 Examples of **incorrect** code for this rule with `"capIsConstructor"` option set to `false`:
233 /*eslint no-invalid-this: ["error", { "capIsConstructor": false }]*/
241 var bar = function Foo() {
245 var Bar = function() {
254 Examples of **correct** code for this rule with `"capIsConstructor"` option set to `false`:
257 /*eslint no-invalid-this: ["error", { "capIsConstructor": false }]*/
261 obj.Foo = function Foo() {
262 // OK, this is in a method.
267 ## When Not To Use It
269 If you don't want to be notified about usage of `this` keyword outside of classes or class-like objects, you can safely disable this rule.