0
HTML要素のEventListenerにオブジェクトメソッドを追加しようとしています。しかし、私がそうすると、this
変数はオブジェクトではなく要素そのものになります。ここでオブジェクトメソッドをhtml要素リスナーにリンクする
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
}
}
は私の回避策です:これは、これをフォーマットする最もエレガントな方法ではないよう
class Foo {
constructor(element, data) {
this.data = data;
this.input = element;
this.input.Foo = this;
this.input.oninput = this.update;
}
update() {
this.Foo.data;
}
}
しかし、私は感じています。オブジェクトのメソッドがそれが離れていたオブジェクトを覚えているように、それをどのようにプログラムするのですか?
正確に何が必要なのですか。 – George