Mark Kennedy (2016-05-09T16:05:34.000Z)
mkay581 at gmail.com (2016-05-09T16:10:15.013Z)
Yes but what happens when you have multiple event targets using the same `type` of event? You're going to require a lot of extra conditionals in `onclick` method. Again this approach is cumbersome and, imo, not the most efficient. These may not be the best solutions but here are a few options I've thought of: 1. When an arrow function is used as the second parameter to `addEventListener`, the language can evaluate and use its scoped context when the same function is used with a subsequent `removeEventListener` call, so essentially the following code would remove the listener appropriately when calling destroy. ```js class MyClass { constructor () { someNode.addEventListener('click', (e) => this.onClick(e)) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', (e) => this.onClick(e)) } } ``` 2. Another solution would be if we could maybe pass a method string as the second parameter and then a context as the fourth parameter to `addEventListener` and `removeEventListener` as follows: ```js class MyClass { constructor () { someNode.addEventListener('click', 'onClick', {}, this) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', 'onClick', {}, this) } } ```
mkay581 at gmail.com (2016-05-09T16:09:36.222Z)
Yes but what happens when you have multiple event targets using the same `type` of event? You're going to require a lot of extra conditionals in `onclick` method. Again this approach is cumbersome and, imo, not the most efficient. These may not be the best solutions but here are a few options I've thought of: 1. When an arrow function is used as the second parameter to `addEventListener`, the language can evaluate and use its scoped context when the same function is used with a subsequent `removeEventListener` call, so essentially the following code would remove the listener appropriately when calling destroy. ```js class MyClass { constructor () { someNode.addEventListener('click', (e) => this.onClick(e)) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', (e) => this.onClick(e)) } } ``` 2. Another solution would be if we could maybe pass a method string as the second parameter and then an optional context as the fourth parameter to `addEventListener` and `removeEventListener` as follows: ```js class MyClass { constructor () { someNode.addEventListener('click', 'onClick', {}, this) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', 'onClick', {}, this) } } ```
mkay581 at gmail.com (2016-05-09T16:08:35.242Z)
Yes but what happens when you have multiple event targets using the same `type` of event? You're going to require a lot of extra conditionals in `onclick` method. Again this approach is cumbersome and, imo, not the most efficient. These may not be the best solutions but here are a few options I've thought of: 1. When an arrow function is used as the second parameter to `addEventListener`, the language can evaluate and use its scoped context when the same function is used with a subsequent `removeEventListener` call, so essentially the following code would work when calling destroy. ```js class MyClass { constructor () { someNode.addEventListener('click', (e) => this.onClick(e)) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', (e) => this.onClick(e)) } } ``` 2. Another solution would be if we could maybe pass a method string as the second parameter and then an optional context as the fourth parameter to `addEventListener` and `removeEventListener` as follows: ```js class MyClass { constructor () { someNode.addEventListener('click', 'onClick', {}, this) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', 'onClick', {}, this) } } ```
mkay581 at gmail.com (2016-05-09T16:07:39.147Z)
Yes but what happens when you have multiple event targets using the same `type` of event? You're going to require a lot of extra conditionals in `onclick` method. Again this approach is cumbersome and, imo, not the most efficient. These may not be the best solutions but here are a few options I've thought of: 1. When an arrow function is used as the second parameter to `addEventListener`, the language can evaluate and use its scoped context when the same function is used with a subsequent `removeEventListener call, so essentially the following code would work when calling destroy. ```js class MyClass { constructor () { someNode.addEventListener('click', (e) => this.onClick(e)) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', (e) => this.onClick(e)) } } ``` 2. Another solution would be if we could maybe pass a method string as the second parameter and then an optional context as the fourth parameter to addEventListener and removeEventListener as follows: ```js class MyClass { constructor () { someNode.addEventListener('click', 'onClick', {}, this) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', 'onClick', {}, this) } } ```
mkay581 at gmail.com (2016-05-09T16:06:38.900Z)
Yes but what happens when you have multiple event targets using the same `type` of event? You're going to require a lot of extra conditionals in `onclick` method. Again this approach is cumbersome and, imo, not the most efficient. These may not be the best solutions but here are a few options I've thought of: 1. When an arrow function is used as the second parameter to `addEventListener`, the language can evaluate and use its scoped context when the same function is used with a subsequent `removeEventListener call, so essentially the following code would work when calling destroy. ```js class MyClass { constructor () { someNode.addEventListener('click', (e) => this.onClick(e)) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', (e) => this.onClick(e)) } } ``` 2. Another solution would be if we could maybe pass a method string as the second parameter and then an optional context as the fourth parameter to addEventListener and removeEventListener as follows: ```js class MyClass { constructor () { someNode.addEventListener('click', 'onClick', {}, this) } onClick (e) { // do something here } destroy () { someNode.removeEventListener('click', 'onClick', {}, this) } } ```