Skip to content

Instantly share code, notes, and snippets.

@RStankov
Created August 3, 2009 17:35
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 RStankov/160704 to your computer and use it in GitHub Desktop.
Save RStankov/160704 to your computer and use it in GitHub Desktop.
// 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