[whatwg] Exposing EventTarget to JavaScript

Boris Zbarsky bzbarsky at MIT.EDU
Fri Apr 24 11:46:07 PDT 2009

Erik Arvidsson wrote:
> To help out with this scenario
> it would be good if an implementation of the EventTarget interface
> could be exposed to JavaScript.

Why do you want the eventTarget interface as opposed to a sane callback 
function registration setup?

> The next and more important step is to allow a JavaScript "class" to
> extend an EventTarget. For example:
> function MyClass() {
>   EventTarget.call(this);
>   ...
> }
> MyClass.prototype = new EventTarget; // *[1]

So this already works, no?

> One more thing needs to be mentioned and that is how event propagation
> should work in scenario. If the object has a "parentNode" property
> then the event dispatching mechanism will do the right thing.

What, precisely, is the use case for this in general?  In the DOM, 
propagating events to parents makes sense (esp. because parents are 
unique).  What would be the use case in a general object graph?

> There is one more thing that needs to be done to make this work
> without a hitch and that is to allow "new Event('foo')" to work.
> Without that we would still have to do "var $tmp =
> document.createEvent('Event'); $tmp.initEvent('foo')" which of course
> is very verbose and requires a document.

Possibly for good reasons?  In some implementations the document is in 
fact baked into the event for various security purposes.

> I see this as a small step to make JS and DOM work better together and
> I hope that "this is the beginning of a beautiful friendship".

It's not really clear to me what the benefits of using the (rather 
suboptimal, from the JS point of view) DOM EventTarget API for generic 
JS callback dispatch are.


More information about the whatwg mailing list