class TestClass {
constructor(word) {
this.word = word;
window.addEventListener("keypress", this.logCharCodeAndWord);
window.addEventListener("click", this.logWord)
}
logCharCodeAndWord(e) {
console.log(e.charCode);
console.log(this.word)
}
logWord() {
console.log(this.word)
}
}
var testObject = new TestClass("banana");
I don't even know how to ask this question, but here's my problem...
console.log(this.word)
This logs "undefined" to the console, because this
refers to window
instead of the TestClass. I want this.word
to refer to "banana", and I would like to be able to use the e.charCode
part at the same time.
How would I do that?