Strict mode function should not be called with invalid this access

  • STRICT_MODE_INVALID_THIS
  • Error
  • High
  • es6

This rule applies when strict mode function is called with invalid this access.

In non-strict mode function, this is evaluated as global object when it is bound with null or undefined value as follows.

  1. f()
  2. f.call(null)

But in the strict mode function, this is evaluated as the same value as bound. Therefore, when accessing this in strict mode function with the above two cases, a TypeError exception is thrown.

Note: A function in ES6 module is implicitly in strict mode. So it is checked by this rule even if it does not have use strict statement.

Noncompliant Code Example

function f() { // STRICT_MODE_INVALID_THIS alarm
    'use strict';
    this.a = 1; // 'TypeError' occurs because 'this' is undefined.
}
f();

function b() { // STRICT_MODE_INVALID_THIS alarm
    'use strict';
    this.a = 1; // 'TypeError' occurs because 'this' is null.
}
b.call(null);

Compliant Code Example

function f() {
    'use strict';
    this.a = 1;
}
var obj = { f: f };
obj.f();

function b() {
    'use strict';
    this.a = 1;
}
var bb = new b();
b.call(bb);

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See