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

221
Visualizações
How do recursive functions have access to info stored in a local memo object?

I'm trying to understand this memoized / recursive version of fibonacci -

 function fib(n, memo = {}) {
    if (n in memo) return memo[n];
    if (n === 1 || n === 2) return 1;

    memo[n] = fib(n - 1, memo) + fib(n - 2, memo); 
    return memo[n]
 }

What I don't understand is how the information saved to a local memo object from inner recursive calls / scopes can be accessed by outer scopes. I.e. if a function calls itself, and then saves some info to the memo, how can the memo object in the higher scope (from where the recursive call was made) be updated if the update was made in a different scope?

When calling a function inside of another function, the former function doesn't have access to the latter function's local variables. When a function is called recursively, I like to think of it as though a function is calling a completely different function. This way of thinking has been helpful, but perhaps in this case it's misleading / wrong.

Thanks for the help in advance frens!

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