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. (excepts static methods)
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 Otherwise are considered problems.
31 This rule applies **only** in strict mode.
32 With `"parserOptions": { "sourceType": "module" }` in the ESLint configuration, your code is in strict mode even without a `"use strict"` directive.
34 Examples of **incorrect** code for this rule in strict mode:
37 /*eslint no-invalid-this: "error"*/
55 var foo = function() {
66 // `this` of arrow functions is the outer scope's.
72 return function foo() {
73 // There is in a method `aaa`, but `foo` is not a method.
80 foo.forEach(function() {
86 Examples of **correct** code for this rule in strict mode:
89 /*eslint no-invalid-this: "error"*/
95 // OK, this is in a legacy style constructor.
102 // OK, this is in a constructor.
109 foo: function foo() {
110 // OK, this is in a method (this function is on object literal).
117 // OK, this is in a method (this function is on object literal).
124 // OK, this is in a method (this function is on object literal).
129 var obj = Object.create(null, {
130 foo: {value: function foo() {
131 // OK, this is in a method (this function is on object literal).
136 Object.defineProperty(obj, "foo", {
137 value: function foo() {
138 // OK, this is in a method (this function is on object literal).
143 Object.defineProperties(obj, {
144 foo: {value: function foo() {
145 // OK, this is in a method (this function is on object literal).
151 this.foo = function foo() {
152 // OK, this is in a method (this function assigns to a property).
158 obj.foo = function foo() {
159 // OK, this is in a method (this function assigns to a property).
163 Foo.prototype.foo = function foo() {
164 // OK, this is in a method (this function assigns to a property).
170 // OK, this is in a method.
176 // OK, this is in a method (static methods also have valid this).
182 var foo = (function foo() {
183 // OK, the `bind` method of this function is called directly.
187 foo.forEach(function() {
188 // OK, `thisArg` of `.forEach()` is given.
195 // OK, this function has a `@this` tag in its JSDoc comment.
202 This rule has an object option, with one option:
204 * `"capIsConstructor": false` (default `true`) disables the assumption that a function which name starts with an uppercase is a constructor.
208 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.
210 Set `"capIsConstructor"` to `false` if you want those functions to be treated as 'regular' functions.
212 Examples of **incorrect** code for this rule with `"capIsConstructor"` option set to `false`:
215 /*eslint no-invalid-this: ["error", { "capIsConstructor": false }]*/
223 var bar = function Foo() {
227 var Bar = function() {
236 Examples of **correct** code for this rule with `"capIsConstructor"` option set to `false`:
239 /*eslint no-invalid-this: ["error", { "capIsConstructor": false }]*/
243 obj.Foo = function Foo() {
244 // OK, this is in a method.
249 ## When Not To Use It
251 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.