answerKey [參數]如何工作?如果Array.prototype.slice.call(參數) 返回一個數組[157,687],是answerKey [parameters]將數組存儲爲key?麻煩理解memoize實現
function memoize(mathProblem) {
var answerKey = {};
return function(){
var parameters = Array.prototype.slice.call(arguments);
if (answerKey[parameters]) {
console.log('returning cached');
return answerKey[parameters];
} else {
answerKey[parameters] = mathProblem.apply(this, arguments);
return answerKey[parameters]
}
}
};
var multiply = function(a, b){
return a*b;
}
var memoMultiply = memoize(multiply);
console.log(memoMultiply(157, 687));
=>
107859
console.log(memoMultiply(157, 687))
=>
returning cached
107859
不,它將數組字符串化爲使用它作爲屬性名稱 – Bergi
@Bergi它不會將其調用的數組串起來.toString() – t3dodson
@TomDDD:這就是我所說的「字符串化」。應該說「鑄造成一個字符串」。 – Bergi