Skip to content

Instantly share code, notes, and snippets.

@ZenCocoon
Created September 30, 2010 16:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZenCocoon/604830 to your computer and use it in GitHub Desktop.
Save ZenCocoon/604830 to your computer and use it in GitHub Desktop.
var Math2 = function() {
function sin(num) {
console.log('Compute');
return Math.sin(num);
}
return {
sin: sin
}
}();
function noArgs() {
return 'no args';
}
function lotOfArgs(arg1, arg2, arg3) {
return 'args: ' + arg1 + ' ' + arg2 + ' ' + arg3;
}
Function.prototype.cached = function() {
var originalFunction = this, args = arguments;
return (function() {
if (args.length != 1)
return originalFunction();
if (originalFunction.cache == undefined)
originalFunction.cache = {};
var arg = args[0];
if (originalFunction.cache[arg] == null)
originalFunction.cache[arg] = originalFunction(arg);
return originalFunction.cache[arg];
})();
}
console.log(Math2.sin.cached(1));
console.log(Math2.sin.cached(0.5));
console.log(Math2.sin.cached(1));
console.log(noArgs());
console.log(lotOfArgs(1, 2, 3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment