Skip to content

Instantly share code, notes, and snippets.

@thebuilder
Created October 31, 2013 12:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thebuilder/7248688 to your computer and use it in GitHub Desktop.
Save thebuilder/7248688 to your computer and use it in GitHub Desktop.
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