Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created April 28, 2011 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseanpg/947190 to your computer and use it in GitHub Desktop.
Save joseanpg/947190 to your computer and use it in GitHub Desktop.
Memoizing Event Register Functions
/***********************************************************************
Initial Version
************************************************************************/
var eventOn = function(type,fn){ win.addEventListener ?
win.addEventListener(type,fn,false) :
win.attachEvent('on'+type,fn)},
eventOff = function(type,fn){ win.removeEventListener ?
win.removeEventListener(type,fn,false) :
win.detachEvent('on'+type,fn)},
/***********************************************************************
Memoization Version
************************************************************************/
var eventOn, eventOff;
if (win.addEventListener) {
eventOn = function(type,fn){win.addEventListener(type,fn,false)}
eventOff = function(type,fn){win.removeEventListener(type,fn,false)}
}
else {
eventOn = function(type,fn){win.attachEvent('on'+type,fn)};
eventOff = function(type,fn){win.detachEvent('on'+type,fn)};
}
@wavded
Copy link

wavded commented Apr 28, 2011

yeah i like this better, save a lookup step

@wavded
Copy link

wavded commented Apr 28, 2011

less compact but yeah

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