Skip to content

Instantly share code, notes, and snippets.

@NV
Created October 30, 2009 02:18
Show Gist options
  • Save NV/222037 to your computer and use it in GitHub Desktop.
Save NV/222037 to your computer and use it in GitHub Desktop.
reinventing JS events API
// 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