クラスで処理してイベントとクラス自身を両方funcに渡してしまえば?
javascript
1class Hoge{
2 constructor() {
3 this.hogehoge = 'fugafuga';
4 const self=this;
5 document.addEventListener('mousedown',e=>{self.func(e,self)});
6 };
7};
8Hoge.prototype.func = (...args)=>{
9 console.log(args[0].type);
10 console.log(args[1].hogehoge);
11};
12const fuga = new Hoge();
応用
javascript
1<script>
2class Hoge{
3 constructor() {
4 this.hogehoge = 'fugafuga';
5 const self=this;
6 document.querySelector('#btn').addEventListener('mousedown',e=>{
7 e.stopPropagation();
8 });
9 document.querySelector('#btn').addEventListener('click',e=>{
10 self.setHogehoge('piyopiyo');
11 });
12 document.addEventListener('mousedown',e=>{
13 self.myfunc(e,self);
14 });
15 };
16 setHogehoge(v){
17 this.hogehoge=v;
18 }
19};
20Hoge.prototype.myfunc = (...args)=>{
21 console.log(args[0].type);
22 console.log(args[1].hogehoge);
23};
24window.addEventListener('DOMContentLoaded', function(e){
25 const fuga = new Hoge();
26});
27</script>
28<input type="button" id="btn" value="hoge:fuga->piyo">
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/29 10:44 編集
2019/05/29 11:20 編集