I understand that a number of generic memoized approaches rely on stringifying the argument list and using that as a key. E.g. as in:
Function.prototype.memoized = function() {
this._values = this.values || {};
var fn = this;
return function() {
var key = JSON.stringify( Array.prototype.slice.call(arguments) );
if (fn._values[key]===undefined) {
fn._values[key]=fn.apply(this, arguments);
}
return fn._values[key];
};
};
This obviously fails when one tries to memoize a "member function" since one would have to also JSON-stringify the context as well, i.e. treat it as an implicitly passed parameter. But that wouldn't work very well when the context is the global object or something equally deep or subject to change in various ways not related with the function itself.
But even if we stick to non-"member functions" it may not always be possible to complete strigify the passed arguments list, right?
Three questions:
- do I understand correctly that memoizing member functions in a generic way is non-sensical?
- do I understand correctly that memoizing even non-member functions in a generic way is also impossible due to the inability / impracticability to fully stringify any conceivable argument list?
- if 2 holds, then why do so many books and blogs try to define a generic
memoizefunction in Function.prototype ? What's the point?