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

429
Visualizações
how to Pass a JavaScript function name as parameter and replace function name like parameter

Its just an experiment

I can pass function name ass parameter but it's called the same name function.

function refreshContactList(id) {
    console.log(`Hello World ${id}`);
}

function addContact(id, refreshCallback) {
    refreshCallback(id);
}

addContact(1, refreshContactList);

like that.


but I want to change the function name, so its changes the name of the function like that ( image )

function addContact(id, refreshCallback) {
    
    function refreshCallback(id) {
        console.log(`Hello World ${id}`);
    }
    
}

addContact(1, refreshContactList);

is there any way to achieve that?

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

0

Is Nested Functions you want?

function addContact(id) {
    function anotherThing() {
        console.log(`Hello World ${id}`);
    }
    
    return { anotherThing };

}

addContact(1).anotherThing();
about 4 years ago · Juan Pablo Isaza Relatório

0

In your picture, you pass not name of function but the reference to this function. If you really want to call the top-level function by the given name (that is, by the string with the name of this function), you can use a cross-platform method that works equally well in node.js, browsers (traditionally, except for Internet Explorer) and other platforms: globalThis:

function a() {
  console.log('a')
}

function b() {
  console.log('b')
}

function callByName(name) {
  globalThis[name]
}

callByName(a)
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