Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
Functions as variable in Javascript

Following is my code of memoization which is working fine, output "Long Time" once only -

Code -

function memoAdd80() {
  let cache = {};
  return (n) => {
    if(n in cache) {
      return cache[n];
    }
    console.log('Long Time');
    const answer = n + 80;
    cache[n] = answer;
    return answer; 
  }
}

const memoized80 = memoAdd80();
console.log(memoized80(85)); // Long Time 165
console.log(memoized80(85)); // 165
console.log(memoized80(85)); // 165

Though, when writing like this - memoAdd80()(85) this caching doesn't works.

console.log(memoAdd80()(85)); // Long Time 165
console.log(memoAdd80()(85)); // Long Time 165
console.log(memoAdd80()(85)); // Long Time 165

Let me know how can I directly use memoAdd80 instead of creating a memoized variable first and then passing a value to the function.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda