SHARE
TWEET

Untitled

a guest Mar 20th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cacheProxyFactory(fn) {
  2.   var cache = {}
  3.   return function() {
  4.     var args = [].join.call(arguments, ',')
  5.     if (args in cache) {
  6.       return cache[args]
  7.     }
  8.     return cache[args] = fn.apply(this, arguments)
  9.   }
  10. }
  11.  
  12. function mult() {
  13.   console.log('开始计算乘积')
  14.   var a = 1
  15.   for (var i = 0, l = arguments.length; i < l; i++) {
  16.     a = a * arguments[i]
  17.   }
  18.   return a
  19. }
  20.  
  21. // 缓存每次计算的乘积
  22. var proxyMult = cacheProxyFactory(mult)
  23.  
  24. console.log(proxyMult(1, 2));
  25. console.log(proxyMult(1, 2, 3));
RAW Paste Data
Top