Mark Kennedy (2016-05-09T16:37:02.000Z)
mkay581 at gmail.com (2016-05-09T16:44:26.091Z)
Sorry maybe I'm not explaining this the right way. My original intent is to lessen the code that I would have to write to achieve multiple event handlers which do different things on multiple DOM elements when constructing a class and also REMOVING the event listeners manually. I am assuming this is a scenario where the DOM elements are outliving their event listeners (for single page web applications for instance) so they must be removed manually. Let's say I have a few buttons on a page and I want them all to do something different when they are clicked. ```html <button id="button1">button 1</button> <button id="button2">button 2</button> <button id="button3">button 3</button> ``` With your approach, I would have to do this, right? ```js class SomeClass { constructor() { let button1 = document.getElementById('button1'); button1.addEventListener('click', this); let button2 = document.getElementById('button2'); button2.addEventListener('click', this); let button3 = document.getElementById('button3'); button3.addEventListener('mouseover', this); } onclick(e) { // if e.target is button1, // show modal // if e.target is button2, // navigate backwards // i f e.target is button 3, // do something else } onmouseover (e) { // if e.target is button 3 // do something else different from the above } handleEvent(e) { this['on' + e.type](e); } ``` It may just be me confused here, but the above code is much more overwhelming and much less intuitive than this. ```js class MyClass { constructor () { this.button1 = document.getElementById('button1'); this.button1.addEventListener('click', this.showModal); this.button2 = document.getElementById('button2'); this.button2.addEventListener('click', this.navigateBack); this.button3 = document.getElementById('button3'); this.button3.addEventListener('mouseover', this.logSomething); } showModal (e) { // do something here } navigateBack () { // navigate back } logSomething () { // log something } destroy () { this.button1.removeEventListener('click', this.showModal); this.button2.removeEventListener('click', this.navigateBack); this.button3.removeEventListener('mouseover', this.logSomething); } } ``` Hopefully this helps clarify a few things.
mkay581 at gmail.com (2016-05-09T16:39:53.219Z)
Sorry maybe I'm not explaining this the right way. My original intent is to lessen the code that I would have to write to achieve multiple event handlers which do different things on multiple DOM elements when constructing a class and also REMOVING the event listeners manually. I am assuming this is a scenario where the DOM elements are outliving their event listeners (for single page web applications for instance) so they must be removed manually. Let's say I have a few buttons on a page and I want them all to do something different when they are clicked. ```html <button id="button1">button 1</button> <button id="button2">button 2</button> <button id="button3">button 3</button> ``` With your approach, I would have to do this, right? ```js class SomeClass { constructor() { let button1 = document.getElementById('button1'); button1.addEventListener('click', this); let button2 = document.getElementById('button2'); button2.addEventListener('click', this); let button3 = document.getElementById('button3'); button3.addEventListener('mouseover', this); } onclick(e) { // if e.target is button1, // show modal // if e.target is button2, // navigate backwards // i f e.target is button 3, // do something else } onmouseover (e) { // if e.target is button 3 // do something else different from the above } handleEvent(e) { this['on' + e.type](e); } ``` It may just be me confused here, but the above code is much more overwhelming and much less intuitive than the solutions I've proposed above. Hopefully this helps clarify a few things.
mkay581 at gmail.com (2016-05-09T16:38:05.343Z)
Sorry maybe I'm not explaining this the right way. My original intent is to lessen the code that I would have to write to achieve multiple event handlers which do different things on multiple DOM elements when constructing a class and also REMOVING the event listeners manually. I am assuming this is a scenario where the DOM elements are outliving their event listeners (for single page web applications for instance) so they must be removed manually. Let's say I have a few buttons on a page and I want them all to do something different when they are clicked. ```html <button id="button1">button 1</button> <button id="button2">button 2</button> <button id="button3">button 3</button> ``` With your approach, I would have to do this, right? ```js class SomeClass { constructor() { let button1 = document.getElementById('button1'); button1.addEventListener('click', this); let button2 = document.getElementById('button2'); button2.addEventListener('click', this); let button3 = document.getElementById('button3'); button3.addEventListener('mouseover', this); }, onclick(e) { // if e.target is button1, // show modal // if e.target is button2, // navigate backwards // i f e.target is button 3, // do something else }, onmouseover (e) { // if e.target is button 3 // do something else different from the above } handleEvent(e) { this['on' + e.type](e); } ``` It may just be me confused here, but the above code is much more overwhelming and much less intuitive than the solutions I've proposed above. Hopefully this helps clarify a few things.