Re: Better event listeners‏

François REMY at
Mon Jan 7 08:42:18 PST 2013

Hi Anne,

Thanks for bringing the case of event registration, this has been a pain in 
DOM/JavaScript for some time already and it's quite clear we can do better 
in this area.

However, I've a few issues with your latest proposal:

    - how do you register more than one handlers for an event?
    - how do you unregister an handler with this proposal?
    - how do you specify properties for your handlers (like shouldCapture)?
    - isn't creating an object bag a too high cost for an event 
    - how do you deal with private names and inherited properties?

What about resurrecting .{} instead?


Even if .{} isn't resurrected, I think an 'on' proxy still leaves you with a 
fairly good syntax:


If we define "element.on" as a proxy, we can event make it support unknown 
event names by returning an "event handler object" for the event whose name 
as been specified as property name.

That would leave us with two new WebIDL interfaces, and an updated one:

    interface EventTarget {
        readonly EventRegistrationHub on;

    interface EventRegistrationHub {
        readonly EventTarget target;
        /* proxy, returns an event handler for unknown properties whose 
target is 'target' and whose 'name' is the required property name */

    interface EventHandler {
        readonly DOMString name; // the name of the event
        readonly EventTarget target; // the target of the event
        add(callback, options...) // ...
        remove(callback...) // ...

Another benefit is that you could pass an event to a function (now, you have 
to pass the target and the name, and use 
target.addEventListener/removeEventListenee(name, ...)).

Another benefit could be that now you can check for the existence of an 
event by doing if("EventName" in target.on), if we configure the proxy 
correctly (ie return true only if the event is builtin). New events always 
come with an uppercase first-letter name so that we can be sure there will 
be no conflict with future Object.prototype.* builtin properties (if there's 
not one already with the current names, which I believe is not the case).

Last but not least, you can get intellisense (autocompletion) once you typed 
"element.on." in any sufficiently good IDE/dev tool.

Best regards,

-----Message d'origine----- 
From: Anne van Kesteren
Sent: Monday, January 07, 2013 2:25 PM
To: es-discuss
Subject: Object iteration order

In "DOM-land" we're designing new APIs that take objects as arguments.
Unless objects meet the criteria in
invocation of the API method might result in implementation-specific
behavior. See also

Given the convenience of being able to do something like

  element.on({click: callback1, resize: callback2})

we're probably going to run with it, but I thought I'd give a heads
up. Maybe someone here has a better idea or maybe iteration order will
finally be settled on? :-)

es-discuss mailing list
es-discuss at 

More information about the es-discuss mailing list