Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74793

How to prevent "this" from being overwritten? [duplicate]

$
0
0

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.


Viewing all articles
Browse latest Browse all 74793

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>