记忆函数

function add(a,b,c){
return a + b
}
var memory = function(){
var cach = {};
return function(){
var key = arguments.length+JSON.stringify(Array.prototype.slice.call(arguments))
console.log(key)
if(key in cach){
console.log('缓存中')
return cach[key]
}else{
console.log('新的计算')
return cach[key] = add.apply(this,arguments)
}
}
}
var memoryadd = memory(add)
console.log(memoryadd(1,3))
console.log(memoryadd('1,3'))
console.log(memoryadd(7,3))
console.log(memoryadd(8,3))

猜你喜欢

转载自blog.csdn.net/wangyun_gogo/article/details/80581890