Re: [whatwg/dom] Removing event listeners through an identifier (#208)

Despite the purpose of this proposal which might be useful for some, I'd like to comment on some statement like the following one:

> Right now they are having to keep a side-table of function references so they can remove them later.

This is because most of them chose so, ignoring EventListener interface, where for the last 17 years they could have done the following:

```js
const handler = {handleEvent(e){ console.log(e.currentTarget); }};
const group = ['blur', 'click', 'focus'];

// add them all
group.forEach(
  function (type) {
    this.addEventListener(type, handler);
  },
  document,querySelector('.the-target')
);

// remove them all
group.forEach(
  function (type) {
    this.removeEventListener(type, handler);
  },
  document,querySelector('.the-target')
);
```

This is a common pattern that is already compatible with every browser.

The group is represented by the handler. It is unique and only its owner can use it to add or remove event listeners.

The `handleEvent` simply has to delegate whatever operation it needs to do and, as a method of an interface, it can be inherited so it plays well with prototypal inheritaance and classes.



-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/208#issuecomment-311273959

Received on Tuesday, 27 June 2017 07:17:34 UTC