Skip to content

Instantly share code, notes, and snippets.

@remy
Created April 16, 2010 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save remy/368657 to your computer and use it in GitHub Desktop.
Save remy/368657 to your computer and use it in GitHub Desktop.
For kicks
function addEvent(el, type, fn) {
el.addEventListener(type, fn, false);
return function (type, fn) {
return addEvent(el, type, fn);
};
}
// So I can do this:
addEvent(document.getElementsByTagName('img')[0], 'dragend', logEvent)('dragstart', logEvent);
// for kicks.
@paulirish
Copy link

Chainable addEventListening! woot.

Resig will be proposing a new DOM api this weekend at JSConf that seeks to take this paradigm and apply it to all sorts of goodness.

@padolsey
Copy link

Nice.

What's wrong with function declarations though?

function addEvent() {...}

@dperini
Copy link

dperini commented Apr 16, 2010

Why is "arguments.callee" even needed ?

function addEvent(el, type, fn) {
    el.addEventListener(type, fn, false);
    return function (type, fn) {
      return addEvent(el, type, fn);
    };
}

the above looks easier and avoids unneeded code.
Am I missing something ?

@remy
Copy link
Author

remy commented Apr 16, 2010

@dperini - you're absolutely right (as I mentioned via Twitter), your version is much cleaner and the best way to do it. I think I was originally playing with an extension to the Function prototype that would unshift the arguments - but I figured I was overthinking the solution.

(note - I've now edited the code to match dperini's version - see the history for the original)

Cheer all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment