talideon.com

My own quiet rebellion...

April 8, 2005 at 4:55PM I have some really strange old code.

I found this sitting amongst some old JavaScript libraries of mine:

function _DefaultHandler() {
    return true;
}

function AttachEventHandler(obj, evtName, fn) {
    var oldHandler = obj["on" + evtName] || _DefaultHandler;
    obj["on" + evtName] = function(evt) {
        evt = evt || window.event;
        return oldHandler(evt) && fn(evt);
    }
}

A few thoughts:

  1. This is rather clever! Obviously I wrote this under the influence of alcohol. [wink]
  2. Was I doped up on Perl when I wrote this?
  3. When did attachEvent() and addEventListener() become mainstream? And why didn’t I use them?
  4. A bit closure-happy, isn’t it?

In case anybody’s wondering, this code isn’t quite what I first found: the original code used eval() to get the originally assigned handler and again to assign the new one. Urgh!

Technorati Search Technorati Search Irish Bloggers

Comments

No comments.

Post a comment

All form information is optional, but it’s a good idea to fill in your name and email address if you want me to take your comment seriously.

Spammers, don’t bother posting crap down here. The site is set up so that legitimate search engines (Google, for instance) won’t index pages with comments on them. Posting crud here only means you’re wasting my time and patience. Shoo!

Real names, please. Please include!
Won’t be displayed. Please include!
Displayed, if present.