Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
Decorator function flow in javascript

I am trying to understand the flow of execution in this code. As you can see in the snippet, there is a function slow and a cachingDecorator function which takes a function as its parameter. Here we are passing function slow in it. After passing the function slow, the slow equals

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;
};

Now what I don't understand is that when we call slow(1) are we passing the 1 to this slow function

function slow(x) {
  // there can be a heavy CPU-intensive job here
  console.log(`Called with ${x}`);
  return x;
}

or in this

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));

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda