More about the Proxy class introduced by ES6: providing an apply trap function in the handler passed to Proxy‘s constructor, we can intercept and modify call to the target function we pass to the same constructor.
Exploiting this feature, we can e.g. implement a very simple memoize function, which return a wrapper function that call the original function and caches its return value, avoiding recalculation - this can be useful when such calculation is time- o money-expensive.
1 | function memoize(fn) { |
The same approack works when the function we need do memoize is a member of an object, accessing siebling members of the object itself during the calculation:
1 | let container = { |