_ .cacheValues = function(func) {
var hash = {};
return function() {
var arg = JSON.stringify(arguments);
if (hash[arg] === undefined) {
hash[arg] = func.apply(this, arguments);
}
return hash[arg];
};
};
Greetings,
I have been exploring how to implement the cacheValues underscore function. One question I have relates to the use of JSON.stringify
.
Within the conditional statement that checks for the existence of an argument within the hash, why does utilizing JSON.stringify
enable this check? Without converting the input arguments array using JSON.stringify
, we would be unable to perform the check as passing an entire array directly wouldn't work. Could you please explain how JSON.stringify
makes this verification possible?