public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.