bug fix for _.memoize when key is derived from prototype

This commit is contained in:
lifesinger
2011-01-17 14:57:02 +08:00
parent 52f66a807c
commit 840f19c102
2 changed files with 8 additions and 1 deletions

View File

@@ -52,6 +52,13 @@ $(document).ready(function() {
var fastFib = _.memoize(fib);
equals(fib(10), 55, 'a memoized version of fibonacci produces identical results');
equals(fastFib(10), 55, 'a memoized version of fibonacci produces identical results');
var o = function(str) {
return str;
};
var fastO = _.memoize(o);
equals(o('toString'), 'toString', 'checks hasOwnProperty');
equals(fastO('toString'), 'toString', 'checks hasOwnProperty');
});
asyncTest("functions: delay", 2, function() {

View File

@@ -426,7 +426,7 @@
hasher = hasher || _.identity;
return function() {
var key = hasher.apply(this, arguments);
return key in memo ? memo[key] : (memo[key] = func.apply(this, arguments));
return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
};
};