Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Events with correct scope in TypeScript
class Tracker {
count = 0;
constructor() {
window.addEventListener("mousedown", this.mouseDown);
window.addEventListener("mouseup", this.mouseUp);
}
mouseDown = (ev: MouseEvent) => {
window.addEventListener("mousemove", this.mouseMove);
}
mouseUp = (ev: MouseEvent) => {
window.removeEventListener("mousemove", this.mouseMove);
}
mouseMove = (ev: MouseEvent) => {
this.count++;
console.log(this.count);
}
}
new Tracker();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.