Skip to content

Instantly share code, notes, and snippets.

@kylebakerio
Created July 17, 2015 20:32
Show Gist options
  • Save kylebakerio/0cbcc4058b1906a1780c to your computer and use it in GitHub Desktop.
Save kylebakerio/0cbcc4058b1906a1780c to your computer and use it in GitHub Desktop.
var memoize3 = function(func) {
var memoized = function(key) {
if (!memoized.cache.hasOwnProperty(key)) memoized.cache[key] = func.apply(this, arguments);
return memoized.cache[key]
}
memoized.cache = {};
return memoized;
}
//this works. But, I feel disingenuous using ".apply", because I don't fully understand it yet.
//vs, what I was trying to do:
var memoize3 = function(func) {
var memoized = function(key) {
var funcStore = func;
if (!memoized.cache.hasOwnProperty(key)) memoized.cache[key] = funcStore(key);
return memoized.cache[key]
}
memoized.cache = {};
return memoized;
}
/*------*/
Is there some rule against defining a function inside a function, and then running it inside of that function as a subroutine when called? Some syntax error I'm making?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment