Liudongge / JavaScript_Notes

learn and work by JS
0 stars 0 forks source link

JS函数式编程指南 #17

Open Liudongge opened 6 years ago

Liudongge commented 6 years ago

记录《mostly-adequate-guide》一书中的难点和需充分理解的点

Liudongge commented 6 years ago
var memoize = function(f) {
  var cache = {};

  return function() {
    var arg_str = JSON.stringify(arguments);
    cache[arg_str] = cache[arg_str] || f.apply(f, arguments);
    return cache[arg_str];
  };
};

var pureHttpCall = memoize(function(url, params){
  return function() { return $.getJSON(url, params); }
});

可以通过延迟执行的方式把不纯的函数转换为纯函数