Avoid referencing unbound methods which may cause unintentional scoping of
this
.
If your function does not access this
, you can annotate it with this: void
, or consider using an arrow function instead279 }
280
281 constructMouseUpEvent(tool1: any, tool2: any, tool3: any) {
282 return this.constructMouseEvent(tool1.up, tool2.up, tool3.up, this.__mouseMoveCallback);283 }
284
285 /**
Avoid referencing unbound methods which may cause unintentional scoping of
this
.
If your function does not access this
, you can annotate it with this: void
, or consider using an arrow function instead275 }
276
277 constructMouseMoveEvent(tool1: any, tool2: any, tool3: any) {
278 return this.constructMouseEvent(tool1.move, tool2.move, tool3.move, this.__mouseUpCallback);279 }
280
281 constructMouseUpEvent(tool1: any, tool2: any, tool3: any) {
Avoid referencing unbound methods which may cause unintentional scoping of
this
.
If your function does not access this
, you can annotate it with this: void
, or consider using an arrow function instead268 console.log("Tool1: ", tool1, "Tool2: ", tool2, "Tool3: ", tool3);
269 if (tool1 === tool3) {
270 console.log("Both right and left tool is the same");
271 return this.constructMouseEvent(tool1.down, tool2.down, tool3.rightdown, this.__mouseDownCallback);272 } else {
273 return this.constructMouseEvent(tool1.down, tool2.down, tool3.down);
274 }
Description
Warning is raised when a method is used outside of a method call.
Class functions don't preserve the class scope when passed as standalone variables.
Bad Practice
class MyClass {
public log(): void {
console.log(this);
}
}
const instance = new MyClass();
// This logs the global scope (`window`/`global`), not the class instance
const myLog = instance.log;
myLog();
// This log might later be called with an incorrect scope
const { log } = instance;
Recommended
class MyClass {
public logUnbound(): void {
console.log(this);
}
public logBound = () => console.log(this);
}
const instance = new MyClass();
// logBound will always be bound with the correct scope
const { logBound } = instance;
logBound();
// .bind and lambdas will also add a correct scope
const dotBindLog = instance.logBound.bind(instance);
const innerLog = () => instance.logBound();