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. Clearly, such a misdirected usage could have unintended consequences, as well as being confusing to
maintainers. Instead, simply drop the this; it will have the same effect.
this.foo = 1; // Noncompliant
console.log(this.foo); // Noncompliant
function MyObj() {
this.foo = 1; // Compliant
}
MyObj.func1 = function() {
if (this.foo == 1) { // Compliant
// ...
}
}
foo = 1;
console.log(foo);
function MyObj() {
this.foo = 1;
}
MyObj.func1 = function() {
if (this.foo == 1) {
// ...
}
}