i have JS class that has method like this:
setupComponent() {
document.getElementById("message-input").addEventListener("keydown", this._onKeyPress);
document.getElementById("send-button").addEventListener("click", this._onSend);
}
Right under it, there are two methods:
_onKeyPress(event) {
if (event.code === "Enter") {
this._onSend();
}
}
_onSend() {
console.log("send");
}
My question is: why direct call from "click" eventListener works (it logs message), and this._onSend()
method, called in _onKeyPress
returns this._onSend is not a function
.
Also using this._onSend()
inside setupComponent()
works.
Is this some kind of magic associated with eventListeners?