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

104
Vistas
es6 module: is there a way to call a function by name?

I have a function where I want to pass in the name of the member function it should execute:

const gen = (N, inFunc) => {  //inFunc = 'func1' or 'func2'
  const func1 = () => {...}
  const func2 = () => {...}

  let func = this[inFunc]  // doesn't work; no 'this' in an ES6 module

  newFunc = func();  // nice try
}

Any solutions?

almost 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

That should work:

const gen = (N, inFunc) => {
  const fns = {
    func1: () => {...}
    func2: () => {...}
  }

  let func = fns[inFunc]

  newFunc = func();
}
almost 4 years ago · Santiago Trujillo Denunciar

0

  1. yep, this wasn't going to help with that ;-)

  2. Konrad Linkowski's answer is fine, but I went a different route (suggested by barmar):

    const gen = (N, inFunc) => {  //inFunc = 'func1' or 'func2'
      const func1 = () => {...}
      const func2 = () => {...}
    
      const funcs = {
        func1, func2 }  
    
      let newFunc = funcs[inFunc]();  
    }
almost 4 years ago · Santiago Trujillo 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