Created
October 30, 2009 02:18
-
-
Save NV/222037 to your computer and use it in GitHub Desktop.
reinventing JS events API
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
// JS Events API sucks. addEventListener ugly. If I'll design this API from scratch, I've done this: | |
window.events // {} | |
window.events.load // [] | |
window.events.load.length // 0 | |
window.events.load.push(function(e){ | |
alert('window loaded') | |
}) | |
window.events.load.length // 1 | |
window.events.load.push(function(e){ | |
alert('Hello!') | |
}) | |
// After window load, alert fires 2 times: first 'window loaded' and second 'Hello!'. | |
window.events.load[0] = function(e){ | |
alert('YARRR!') | |
} | |
// Override first event. | |
delete window.events.load[1] | |
// Delete second event. | |
// Now output will be just 'YARRR!' | |
// What do you think? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment