Re: [DOM4] EventTarget as first class citizen

On Wednesday, February 29, 2012 at 2:41 PM, Anne van Kesteren wrote:

> On Wed, 29 Feb 2012 15:34:22 +0100, Marcos Caceres <w3c@marcosc.com (mailto:w3c@marcosc.com)> wrote:
> > It's exactly the same as we have now. It's just a tree with no nodes  
> > (i.e., they don't participate in a tree, initially at least). This works  
> > fine in the example I gave, the only difference being that one would not  
> > need to create a fake HTML element to get the desired behavior.
>  
>  
>  
> That is not what Jonas was proposing and what you said "It would be great  
> to see something like the above." about.  

This is true, but I said "something like". I was hoping other people would jump into the discussion. I presented the use case I have (and what I have used for a number of years). All I know is that it would be nice, for the use cases I have (and from the little evidence I gathered from the Web), to either:
  
  1. Have an object inherits EventTarget.  

  2. Have a simple object that implements event target.   


For 1, I don't know what that would look like, but something like ECMAScript's Object.create() would be a nice pattern:

var et = EventTarget.create(someObject);  

For 2, EventTarget with no constructor would be nice enough:

var et = new EventTarget();   

…this could then be used like:  

function MyThingo(){
   var et = new EventTarget();
     
   this.on = function(event, cb){
        et.addEventListener(évent, cb);  
   }  
… and so on...
}

var thing = new MyThingo();  
thing.on("event", function(){…teehhooo…})
   
> Giving EventTarget() a  
> constructor with no arguments could work, but what is the advantage over  
> simply creating an element  

You are right, there isn't a practical/technical advantage to creating an element; but it "feels" wrong to create a fake HTML element to achieve something so fundamental to the platform (hence, it is relegating EventTarget as a second class citizen… this is a shame, because custom events are a first class citizen, in that they can be constructed by developers).  

> or some other object that inherits from it?
>  

If this ("other object that inherits from it") can be done today, I would like to know how.   

  
--  
Marcos Caceres

Received on Wednesday, 29 February 2012 15:05:55 UTC