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

111
Visualizações
Javascript closure explanation for newbie

So i came across some closure examples, and i would like to know how does this work. I solved it by mistake and after a few hours of trying. but i am still trying to understand how the values are passed. I understand that once i create a local variable (func) the value associated with it doesnt really change, so i can keep referencing that initial value. My question is how when i return the inner function i am able to transfer the values in logArea to the callback function. can someone explain like step by step?

const wrapLog = function(callback, name) {
  let func = callback;
  return function(...param) {
    return func(...param);
  }
};

const area = function(x, y) {
  return x * y;
};

const logArea = wrapLog(area, "area");

console.log(logArea(5, 3)); // area(5, 3) => 15
console.log(logArea(3, 2)); // area(3, 2) => 6

const volume = function(x, y, z) {
  return x * y * z;
};

const logVolume = wrapLog(volume, "volume");

console.log(logVolume(5, 3, 2)); // volume(5, 3, 2) => 30
console.log(logVolume(3, 2, 4)); // volume(3, 2, 4) => 24

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