Created
September 30, 2010 16:07
-
-
Save ZenCocoon/604830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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