no-invalid-this

禁止在 this 的值为 undefined 的上下文中使用 this

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

规则详情

此规则旨在标记在 this 的值为 undefined 的上下文中使用 this 关键字。

脚本中的顶级 this 始终被认为是有效的,因为无论严格模式如何,它都引用全局对象。

ECMAScript 模块中的顶级 this 总是被认为是无效的,因为它的值是 undefined

ZfVzfoGxX9FzbD+mjV8lPrrel3cF0GHJEefA+RUcb6Sf+dOjk7DYQqMWnKdW+J3rH67tAtERRuRdveLZQ3lKhJm7GPzA8iKLuQwiZba00WWjzgRvq0D82nH0NkevKNGUkfREumuTKzSfW2q/aAr9EtmhhrQPBZTg6uss8jhJToMK1vwUFbhARHFzKkLOKrdyBKNEGiiHfXxqDa/aq1M6zeeAmBlBujGn9GHnD4jRTapCKfbgF0l7G5Oux5t9gqA8DMECoAyLU0iLQP5nQpmD27kvgrj+zqBQb1KEIkP4xNfcu+892z84OFAhyKO/Os8jAgU1qqlzSIjues8qY45SkQ==

aAjsjiir1wvbJEf7gR7i4VPi0C9wNMu/sovAM48knwzHaCf/cGp0XhUArp2UeLZoIesx/i/ZjgTaDUANqwjfQA==

    3DdcKeg98st1FVktL03Kr9emve1HPy9gPlnR6BfEzrZvTlZL06kmGSAj1ZMI1741HK2dFTAuulIgsug0te6aKjs9zm0MT+9PNoFIpvJ/uZlut+mRtuZtma7ha93nxrAJDEf/MwYjQIJxu6f+0IvELQRJ73PCi0ziuQqWmZCjYJWApFf5mkNg4HiUWitQQblKZss9S4upWZygBFzv+ajLTQ==

d30X9hj1B3fYPo2iTn5Rb5p2icvOCEk8mJzbNXXIOIzsClDAEoEJrnx7l/zT4y6vxzXMdU/++fR3+WQqLTPMqA==

    FJR7Y+mYzdsK6G7y5xh/H4vTOTv/09sxzkkmR62X13kYfzi0gYMBpyUvnbMJNBj1i7Ukw9HQgAyK3DyrEvaSV86cpixgbDVIx0FVoP8uKE5Wv4ypjlGnRnufmxKPNSww2Dvo8N68mICz19NpuZCM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owVU1u+rYqaVARVZALwidnd01KWBLabDRX0pzhKVF+oDZ87mJWtQpCZOFqvLODTUZlC9v0miA==

c3L+n2iwQW1SvuMb69Y56xjUD4fqYW5RGJ+B474fuvBPvXI1tjIJ2fhP1mbsetOf47bPbyYCeL1wCkFrkMWhgg==

/*eslint no-invalid-this: "error"*/
/*eslint-env es6*/

"use strict";

(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);
});

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);
});

c3L+n2iwQW1SvuMb69Y563+RtUr+JFgVXZ/cxVMc3BHX3W59pY+JLwvGEGH/836cQQcj4lcYkBt4yXsvJLNGhA==

/*eslint no-invalid-this: "error"*/
/*eslint-env es6*/

"use strict";

this.a = 0;
baz(() => this);

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 {

    // OK, this is in a class field initializer.
    a = this.b;

    // OK, static initializers also have valid this.
    static a = this.b;

    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);
    }

    static {
        // OK, static blocks 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;
}

选项

9NHbribgvlAIiqeVNtZmFJBXFzE5ZrwC3cI4vwFctVVObI6cK97YuKXS4F2fbAYOGPLsxHUtItdbJxHbwkCFpw==

    ELYJdQjoA5lP2K3qaAvJl4RnZrOfX3kxX33bsipsQciyul8qu+ghg/70T3RsdxhDXC4HIMC3AJ+qYSjxNHTwO6MU9kzHk34cCYPXCzS+oreGiFo8ohLf5Y9iBnFdTcbwsjYmGi5bz7/3i1OtaegAy7KVwDWkHlN3OmGv1gBXnyOSgpin1cg3f8mPJmCNPfDx

capIsConstructor

Ba7IWdbGkE2Tnq20h5etKTVu8D5KaRVeP3mjtjyUBzxxQYoqNrPc7LQKh5bjSEv+uZmeqozDXFnipCK+lcxvz0kpbUTc1tZ+ISxacHmHBNColMlwQ0kRBfm8VRXZkU+K4JsiSqJLIRiM5Ffkbe5g/R3W/mtgcLXNyBgCTwdexrQ7MKodFLBIYrrnUA7v+DMOoeA0MFzRpZgnSTwkT1rZ5JNTtUgs3HRDS1dggKHV4eGM+vvhtPqta+iKYko9Bx6VTQPoN0FvzU4qROjhELkHjv2VFWKKOSrAD/SmkX2FwdQ=

oWwu2F4dW010vuuZXpQrkSSCN51BF5t8B/LumicvBCM4jGpPqlGwQo2XSmYiHDYZ51IsrYrCHXzGyWFxNBIfKGKl48B2+V3X3G1UR/9y42W9PGkmQIK2F8pwEXhveA3bh94BVw19jbvyGFQVOIY/iJRnJkq412reiNKLBgkGJ4E=

KIjVZ8jbyN1kWo441FXYIh3AmcQhCDgs/8OlFVGexGl4IhBzfx/Z0R417PapMCXdgb509eGnIT7K5jHPgbP5+OKdNEz1i9eQFOf3utwoGMw0uW2ILuPnvnM9zTuno8e1AivbDxzmSTNLinkhGvzaTg==

/*eslint no-invalid-this: ["error", { "capIsConstructor": false }]*/

"use strict";

function Foo() {
    this.a = 0;
}

var bar = function Foo() {
    this.a = 0;
}

var Bar = function() {
    this.a = 0;
};

Baz = function() {
    this.a = 0;
};

AoqXtTV6Q8irP1mRQNh34rATVScSSkZAU175f14ev63/QVPnbdJA+W2TmLkTTnPfk64ZgCZfvhMyBNZ6cfPt7bfpCnmIBmzTBmP3OHknPtELA0yTtcBhugO3m1sY1zOEN9RYa9y9NomxLLZMi+rWTg==

/*eslint no-invalid-this: ["error", { "capIsConstructor": false }]*/

"use strict";

obj.Foo = function Foo() {
    // OK, this is in a method.
    this.a = 0;
};

何时不使用

VUPoE9Nq6d7iihsKAqbqYzbCh3gyAL2YVohddcQfi+D1ZSSK9slXc9t0Q8RzGN/Mf1qEZvQT0tUClv+d3lhBMgIatF26GbQvzRKPdzHh7B6xjCSp2NsViu9Vjb/D/SxWdU9F0ya9w2KU3g9kVWxx8vmszs/vdGyqKbw90GemxKm4zqhKmyIr8zwW+2rDXwrB