When the keyword this
is used outside of an object, it refers to the global this
object, which is the same thing as the
window
object in a standard web page. Such uses could be confusing to maintainers. Instead, simply drop the this
, or replace
it with window
; it will have the same effect and be more readable.
Noncompliant Code Example
this.foo = 1; // Noncompliant
console.log(this.foo); // Noncompliant
function MyObj() {
this.foo = 1; // Compliant
}
MyObj.func1 = function() {
if (this.foo == 1) { // Compliant
// ...
}
}
Compliant Solution
foo = 1;
console.log(foo);
function MyObj() {
this.foo = 1;
}
MyObj.func1 = function() {
if (this.foo == 1) {
// ...
}
}