Is it possible to solve this task using static methods? It is possible not to solve it) it is just interesting. But if there is a desire and time, I will be glad
Create a makeCaching (f) decorator that takes the function f and returns a wrapper that caches its results.
In this problem, the function f has only one argument, and it is a number.
The first time the wrapper is called with a specific argument, it calls f and remembers the value. For the second and subsequent calls with the same argument, the memorized value is returned.
function f(x) { return Math.random()*x; } function makeCaching(f) { var cache = {}; return function(x) { if (!(x in cache)) { cache[x] = f.call(this, x); } return cache[x]; }; } f = makeCaching(f); var a = f(1); var b = f(1); alert( a == b ); // true (значение закешировано) b = f(2); alert( a == b ); // false, другой аргумент => другое значение
f.call(this, x);- ?? - Igoraandbare numbers. Kashiruyu, do not kashiruyu, they will still be the same for the sameх. - Igor.call(this? - Igor