Re: [DOM4] EventTarget as first class citizen

On Wed, Feb 29, 2012 at 11:40, Jonas Sicking <jonas@sicking.cc> wrote:
> I don't see how they could be. Where would the functions store the
> internal state such as the set of event listeners for example?

I don't know about SpiderMonkey but in V8 we can add properties to
Objects that are not visible to script. With ES6 the same thing can be
done with private names.

When you call addEventListener you check if `this` has the property
and if it doesn't you create a new property pointing to the list of
listeners. This is the way we are speccing things for ES6 which allows
us to subclass Date, Map, Set etc.

Another option is to add this hidden property in [[Call]] (or [[Construct]]).

In ES6 style code

let EventTarget;
{
  let listeners = Name.create();
  EventTarget = function() {};
  EventTarget.prototype = {
    addEventListener(type, listener, capture = false) {
      if (!this[listeners]) this[listeners] = ...
      ...
    }
    ...
  }
}

Received on Wednesday, 29 February 2012 20:00:45 UTC