Estoy tratando de entender el flujo de ejecución en este código. Como puede ver en el fragmento, hay una función lenta y una función cachingDecorator que toma una función como parámetro. Aquí estamos pasando la función slow . Después de pasar la función lento, el slow es igual
slow = function (x) { if (cache.has(x)) { // if there's such key in cache return cache.get(x); // read the result from it } let result = func(x); // otherwise call func cache.set(x, result); // and cache (remember) the result return result; }; Ahora lo que no entiendo es que cuando llamamos slow(1) estamos pasando el 1 a esta función lenta
function slow(x) { // there can be a heavy CPU-intensive job here console.log(`Called with ${x}`); return x; }o en este
slow = function (x) { if (cache.has(x)) { // if there's such key in cache return cache.get(x); // read the result from it } let result = func(x); // otherwise call func cache.set(x, result); // and cache (remember) the result return result; }; function slow(x) { // there can be a heavy CPU-intensive job here console.log(`Called with ${x}`); return x; } function cachingDecorator(func) { let cache = new Map(); return function(x) { if (cache.has(x)) { // if there's such key in cache return cache.get(x); // read the result from it } let result = func(x); // otherwise call func cache.set(x, result); // and cache (remember) the result return result; }; } slow = cachingDecorator(slow); console.log(slow); console.log(slow(1));