So I am working with the ES6 class stuff, which I've not had a chance to mess around with before, and event handlers, and I'm encountering an issue with the value of my this
.
Javascript:
class Chat {
constructor() {
/*code*/
}
handleOnKeyDown() {
console.log("A key was pressed somewhere on the page!")
console.log(this)
}
}
HTML:
let chat = new Chat()
chat.handleOnKeyDown() //this === [Chat Object]
document.onkeydown = chat.handleOnKeyDown //this === #document
How is my this
being overwritten, and how can I prevent it from doing so? I don't particularly need the this from the event handler, since it's just literally the entire document, and isn't giving me any extra context.