Strict mode function should not be called with invalid this access

  • STRICT_MODE_INVALID_THIS
  • Error
  • High
  • No tags

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.

For example,

  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.

Noncompliant Code Example

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

function b() { // STRICT_MODE_INVALID_THIS alarm
    'use strict';
    this.a = 1; // TypeError 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