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,

  1. this is evaluated as global object when the function is bound with null or undefined value e.g. f();
  2. this is evaluated as wrapper object when the function is bound with primitive value e.g. f.apply(1);

But in the strict mode function, this is evaluated as the same value as bound. Therefore, when accessing this on 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