クラス インスタンス内の HTMLElement メンバーの onclick イベント ハンドラーを設定しようとしていますが、両方の試行で問題があります。
1: 使用できないキーワード
class ClassName {
div: HTMLElement;
constructor() {
this.div = document.createElement('div');
this.div.onclick = function(e) {
this._onclick(); // keyword 'this' is not the instance in this scope
}
}
_onclick() {
alert('I've been clicked!');
}
}
2: エラー:「'void' を (ev:FocusEvent) => any に変換できません。」
class ClassName {
div: HTMLElement;
constructor() {
this.div = document.createElement('div');
this.div.onclick = this._onclick(); // error
}
_onclick() {
alert('I've been clicked!');
}
}
これは、私の側の言語の理解が不足していることを示していると思います。誰かが解決策を明確にし、おそらく投稿していただければ幸いです。