Wednesday, March 3, 2010

JS Snippets


function myFunc(param){
if (!myFunc.cache) {
myFunc.cache = {};
}
if (!myFunc.cache[param]) {
var result = {}; // ...
myFunc.cache[param] = result;
}
return myFunc.cache[param];
}



function lazy() {
var result = 2 + 2;
lazy = function() {
return result;
};
return lazy();
}

lazy(); // 4
lazy(); // 4

No comments: