Re: addEventListener naming

Jonas Sicking <jonas@sicking.cc> wrote:
> 2. If the long name is a significant problem, then it can easily be
> worked around in JS by using prototypes.

What's the proper way to do that so that it feels close to like it was  
implemented natively?

Would the following be close?

window.listen = Node.prototype.listen = function(type, func, capt) {
     if (typeof capt !== "bool") capt = false;
     this.addEventListener(type, func, capt);
};
window.unlisten = Node.prototype.unlisten = function(type, func, capt) {
     if (typeof capt !== "bool") capt = false;
     this.removeEventListener(type, func, capt);
};
window.listenOnce = Node.prototype.listenOnce = function(type, func, capt)  
{
     function wrapper(e) {
         func.apply(this, [e]);
         this.unlisten(type, wrapper, capt);
     }
     this.listen(type, wrapper, capt);
};

If so, even though that's not much code (especially if you don't need the  
3rd), I don't know if I'd bother.

If browsers implement listen() though, I would prefer it over  
addEventListener, as long as I didn't have to do if (foo.listen) {} else  
if(foo.addEventListener) {} everywhere.

-- 
Michael

Received on Sunday, 13 September 2009 04:14:19 UTC