Created
April 16, 2010 16:45
-
-
Save remy/368657 to your computer and use it in GitHub Desktop.
For kicks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Nice.
What's wrong with function declarations though?
function addEvent() {...}
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 ?
@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
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.