Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.