非常教程

Eslint参考手册

规则 | Rules

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 中引入。

资源

  • Rule source
  • Documentation source

规则 | Rules相关

1.accessor-pairs
2.array-bracket-newline
3.array-bracket-spacing
4.array-callback-return
5.array-element-newline
6.arrow-body-style
7.arrow-parens
8.arrow-spacing
9.block-scoped-var
10.block-spacing
11.brace-style
12.callback-return
13.camelcase
14.capitalized-comments
15.class-methods-use-this
16.comma-dangle
17.comma-spacing
18.comma-style
19.complexity
20.computed-property-spacing
21.consistent-return
22.consistent-this
23.constructor-super
24.curly
25.default-case
26.dot-location
27.dot-notation
28.eol-last
29.eqeqeq
30.for-direction
31.func-call-spacing
32.func-name-matching
33.func-names
34.func-style
35.function-paren-newline
36.generator-star
37.generator-star-spacing
38.getter-return
39.global-require
40.global-strict
41.guard-for-in
42.handle-callback-err
43.id-blacklist
44.id-length
45.id-match
46.implicit-arrow-linebreak
47.indent
48.indent-legacy
49.init-declarations
50.jsx-quotes
51.key-spacing
52.keyword-spacing
53.line-comment-position
54.linebreak-style
55.lines-around-comment
56.lines-around-directive
57.lines-between-class-members
58.max-depth
59.max-len
60.max-lines
61.max-nested-callbacks
62.max-params
63.max-statements
64.max-statements-per-line
65.multiline-comment-style
66.multiline-ternary
67.new-cap
68.new-parens
69.newline-after-var
70.newline-before-return
71.newline-per-chained-call
72.no-alert
73.no-array-constructor
74.no-arrow-condition
75.no-await-in-loop
76.no-bitwise
77.no-buffer-constructor
78.no-caller
79.no-case-declarations
80.no-catch-shadow
81.no-class-assign
82.no-comma-dangle
83.no-compare-neg-zero
84.no-cond-assign
85.no-confusing-arrow
86.no-console
87.no-const-assign
88.no-constant-condition
89.no-continue
90.no-control-regex
91.no-debugger
92.no-delete-var
93.no-div-regex
94.no-dupe-args
95.no-dupe-class-members
96.no-dupe-keys
97.no-duplicate-case
98.no-duplicate-imports
99.no-else-return
100.no-empty
Eslint

ESLint 是一个代码规范和错误检查工具,有以下几个特性。所有东西都是可以插拔的。你可以调用任意的 rule api 或者 formatter api 去打包或者定义 rule or formatter。任意的 rule 都是独立的。没有特定的 coding style,你可以自己配置。

主页 https://eslint.org/
源码 https://github.com/eslint/eslint
发布版本 4.12.0

Eslint目录

1.指南 | Guide
2.规则 | Rules