I'm trying to add an object method to an EventListener on an HTML element. But when I do that the this variable becomes the elements itself instead of the object.
class Foo {
constructor(element, data) {
this.data = data;
this.input = element;
this.input.oninput = this.update;
}
update() {
this.data; // The context has changed to the element
}
}
Here's my workaround:
class Foo {
constructor(element, data) {
this.data = data;
this.input = element;
this.input.Foo = this;
this.input.oninput = this.update;
}
update() {
this.Foo.data;
}
}
However, I feel like this isn't the most elegant way of formatting this. How do I program it in such a way that the object's method remembers the object it was apart of?