medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.73k stars 61 forks source link

Properties on functions not passed #66

Closed jdylanstewart closed 7 years ago

jdylanstewart commented 7 years ago
const newFunc = () => true;
newFunc.state = false;
const memoizedFunc = memoize(newFunc);

memoizedFunc does not retain the state property.

This should be as simple as iterating through keys and applying them to the memoized function.Object.keys(func).forEach((key) => { returnFunc[key] = newFunc[key] })

medikoo commented 7 years ago

In memoizee function is treated as nothing more than a function, and I would prefer not to extend library with logic as proposed.

Mainly to not introduce additional level of complexity, but it also may appear as not that straightforward as it seems, I can imagine cases when such behaviour may be unwanted.

Use cases are rare, and if you really need it, you can on your own introduce a decorator that will do it cleanly:

var memoizePlus = function (fn) {
  var memoized = memoize.apply(this, arguments);
  Object.keys(fn).forEach(function (key) { memoized[key] = fn[key]; });
  return memoized;
};

var memoized = memoizePlus(fn, ...); // does what you need