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

428
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
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