【发布时间】:2015-06-27 17:45:29
【问题描述】:
answerKey[parameters] 是如何工作的? if Array.prototype.slice.call(参数) 返回一个数组[157, 687], answerKey[parameters] 是存储一个数组作为key吗?
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 它不会对它调用的数组进行字符串化 .toString()
-
@TomDDD:这就是我所说的“字符串化”。应该说“转换为字符串”。
-
@Bergi 我只是澄清了很多时间
stringify表示JSON.stringify会产生不同的字符串。