diff --git a/test/functions.js b/test/functions.js index fc50d2990..598bf6fb1 100644 --- a/test/functions.js +++ b/test/functions.js @@ -127,6 +127,18 @@ }); hashed('yep'); deepEqual(hashed.cache, {'YEP': 'yep'}, 'takes a hasher'); + + // Test that the hash function can be used to swizzle the key. + var objCacher = _.memoize(function(value, key) { + return {key: key, value: value}; + }, function(value, key) { + return key; + }); + var myObj = objCacher('a', 'alpha'); + var myObjAlias = objCacher('b', 'alpha'); + notStrictEqual(myObj, undefined, 'object is created if second argument used as key'); + strictEqual(myObj, myObjAlias, 'object is cached if second argument used as key'); + strictEqual(myObj.value, 'a', 'object is not modified if second argument used as key'); }); asyncTest('delay', 2, function() { diff --git a/underscore.js b/underscore.js index c24c01d78..cd59b6cc1 100644 --- a/underscore.js +++ b/underscore.js @@ -709,7 +709,7 @@ var cache = memoize.cache; var address = hasher ? hasher.apply(this, arguments) : key; if (!_.has(cache, address)) cache[address] = func.apply(this, arguments); - return cache[key]; + return cache[address]; }; memoize.cache = {}; return memoize;