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.
f()
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
View with compliant examples side by sidefunction example1() {
function foo() {
'use strict';
this.a = 1; // 'TypeError' occurs because 'this' is undefined.
}
foo(); // STRICT_MODE_INVALID_THIS alarm
}
function example2() {
function foo() {
'use strict';
this.a = 1; // 'TypeError' occurs because 'this' is null.
}
foo.call(null); // STRICT_MODE_INVALID_THIS alarm
}
Compliant Code Example
View with noncompliant examples side by sidefunction example1() {
function foo() {
'use strict';
this.a = 1;
}
var obj = { foo: foo };
obj.foo();
}
function example2() {
function foo() {
'use strict';
this.a = 1;
}
var obj = new foo();
foo.call(obj);
}
Version
This rule was introduced in DeepScan 1.0.0-alpha.