I have a custom button using this Custom Element class:
class Button extends HTMLElement {
connectedCallback() {
document.addEventListener('click', this.reusableClickHandler)
}
disconnectedCallback() {
document.removeEventListener('click', this.reusableClickHandler)
}
reusableClickHandler() {
console.log(this) // how to get `this` to equal the button instance
}
}
The custom button listens for clicks on document
, and removes the stale event handlers when the button is removed from the DOM.
Is it possible for me to keep this
within reusableClickHandler()
equal to the button instance? I'm not sure how to write it.
Also I know that a button created this way is not optimal, it's just to demonstrate the problem. Thanks