Created
August 3, 2009 17:35
-
-
Save RStankov/160704 to your computer and use it in GitHub Desktop.
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
// Lazy loading function | |
Function.prototype.lazyLoad = function(){ | |
var __method = this; | |
function loaded(){ | |
loaded = __method(); | |
delete(__method); | |
return loaded.apply(this, arguments); | |
}; | |
return function(){ | |
return loaded.apply(this, arguments); | |
}; | |
}; | |
var observe = function(){ | |
if (document.addEventListener) | |
return function(element, eventName, callback){ | |
element.addEventListener(eventName, callback, false); | |
}; | |
if (element.attachEvent){ | |
return function(element, eventName, callback){ | |
element.attachEvent("on" + eventName, callback); | |
}; | |
return function(element, eventName, callback){ | |
element["on" + eventName] = callback; | |
}; | |
}.lazyLoad(); | |
observe(document, 'load', function(){ /* ... */ }); // -> zazy load here, compiled function | |
observe(document, 'load', function(){ /* ... */ }); // -> compiled function | |
observe(document, 'load', function(){ /* ... */ }); // -> compiled function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment