Eslint
规则 | Rules

no-invalid-this

禁止this在类或类对象之外的关键字。( no-invalid-this )

在严格模式下,this类或类对象之外的关键字可能会undefined引发并引发TypeError

规则细节

该规则旨在标记this类或类类对象之外的关键字的用法。

基本上,这个规则检查一个包含this关键字的函数是一个构造函数还是一个方法。

该规则从以下条件判断函数是否是构造函数:

  • 该函数的名称以大写字母开头。

  • 该函数被分配给一个以大写字母开头的变量。

  • 该函数是 ES2015 类的构造函数。

该规则从以下条件判断函数是否是方法:

  • 该函数位于对象文字上。

  • 该功能被分配给一个属性。

  • 该函数是 ES2015 类的方法/ getter / setter。(除了静态方法)

此规则允许this在以下函数中使用关键字:

  • call/apply/bind函数的方法直接调用。

  • 如果给出回调函数thisArg,则该函数是数组方法(如.forEach())。

  • 该函数@this在其 JSDoc 注释中包含标签。

否则被认为是问题。

此规则适用于在严格模式。随着"parserOptions": { "sourceType": "module" }在 ESLint 配置,你的代码是在严格模式下,即使没有一个"use strict"指令。

在严格模式下针对此规则的错误代码示例:

/*eslint no-invalid-this: "error"*/ /*eslint-env es6*/ "use strict"; this.a = 0; baz(() => this (function() { this.a = 0; baz(() => this })( function foo() { this.a = 0; baz(() => this } var foo = function() { this.a = 0; baz(() => this }; foo(function() { this.a = 0; baz(() => this } obj.foo = () => { // `this` of arrow functions is the outer scope's. this.a = 0; }; var obj = { aaa: function() { return function foo() { // There is in a method `aaa`, but `foo` is not a method. this.a = 0; baz(() => this }; } }; foo.forEach(function() { this.a = 0; baz(() => this }

严格模式下此规则的正确代码示例:

/*eslint no-invalid-this: "error"*/ /*eslint-env es6*/ "use strict"; function Foo() { // OK, this is in a legacy style constructor. this.a = 0; baz(() => this } class Foo { constructor() { // OK, this is in a constructor. this.a = 0; baz(() => this } } var obj = { foo: function foo() { // OK, this is in a method (this function is on object literal). this.a = 0; } }; var obj = { foo() { // OK, this is in a method (this function is on object literal). this.a = 0; } }; var obj = { get foo() { // OK, this is in a method (this function is on object literal). return this.a; } }; var obj = Object.create(null, { foo: {value: function foo() { // OK, this is in a method (this function is on object literal). this.a = 0; }} } Object.defineProperty(obj, "foo", { value: function foo() { // OK, this is in a method (this function is on object literal). this.a = 0; } } Object.defineProperties(obj, { foo: {value: function foo() { // OK, this is in a method (this function is on object literal). this.a = 0; }} } function Foo() { this.foo = function foo() { // OK, this is in a method (this function assigns to a property). this.a = 0; baz(() => this }; } obj.foo = function foo() { // OK, this is in a method (this function assigns to a property). this.a = 0; }; Foo.prototype.foo = function foo() { // OK, this is in a method (this function assigns to a property). this.a = 0; }; class Foo { foo() { // OK, this is in a method. this.a = 0; baz(() => this } static foo() { // OK, this is in a method (static methods also have valid this). this.a = 0; baz(() => this } } var foo = (function foo() { // OK, the `bind` method of this function is called directly. this.a = 0; }).bind(obj foo.forEach(function() { // OK, `thisArg` of `.forEach()` is given. this.a = 0; baz(() => this }, thisArg /** @this Foo */ function foo() { // OK, this function has a `@this` tag in its JSDoc comment. this.a = 0; }

何时不使用它

如果您不希望this在类或类对象之外收到关于关键字使用情况的通知,则可以安全地禁用此规则。

版本

该规则在 ESLint 1.0.0-rc-2 中引入。

资源