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

195
Visualizações
Adding logs within a wrapped function

I have created a function wrapper to help give me various print statements:

function fnWrapper(fn, verbosity=false) {
    return function(...rest) {
        if (verbosity) console.log(`Calling "${fn.name}(${rest})"`)
        const res = fn(...rest); // possible to add debugging **within** this wrapped function?
        if (verbosity) console.log('==>', res)
        return res;
    }
}
function add(x,y) {
     return x===0 ? y : add(x-1, y+1);
}
const add2 = fnWrapper(add, true);

add2(2,3);
// Calling "add(2,3)"
// ==> 5

Is it possible to add debugging within the function itself, for example, at its most basic to translate a function such as the following:

function something(x,y) {
    console.log(arguments); // add this in
    ...
}

So for the above function it would make the add become:

function add(x,y) {
    console.log(arguments);
    return x===0 ? y : add(x-1, y+1);
}

If so, how could that be done?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

No, this is not possible, you cannot change what a function does.

The problem is that add calls add, and add2 also calls add only. You can however simply replace add:

function fnWrapper(fn, verbosity=false) {
    return function(...rest) {
        if (verbosity) console.log(`Calling "${fn.name}(${rest})"`)
        const res = fn.call(this, ...rest);
        if (verbosity) console.log('==>', res)
        return res;
    }
}
function add(x,y) {
     return x===0 ? y : add(x-1, y+1);
}
add = fnWrapper(add, true); /*
^^^ */

add(2,3);

about 4 years ago · Juan Pablo Isaza Relatório
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