Skip to content

Instantly share code, notes, and snippets.

@EdCharbeneau
Last active April 17, 2020 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdCharbeneau/c4af5cbbf0dad1e350c8ba285b6239b7 to your computer and use it in GitHub Desktop.
Save EdCharbeneau/c4af5cbbf0dad1e350c8ba285b6239b7 to your computer and use it in GitHub Desktop.
TypeWtf
export MyClass {
private foo: string = "hello";
public addHandler() {
window.addEventListener("resize", this.logResize);
}
public logResize() {
console.log(this); // Expected: MyClass | Actual: Window
console.log(foo); // Expected: "hello" | Actual: Undefined
}
//fixed
public logResize = () => {
console.log(this); // Expected: MyClass | Actual: Window
console.log(foo); // Expected: "hello" | Actual: Undefined
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment