Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
módulo es6: ¿hay alguna forma de llamar a una función por su nombre?

Tengo una función a la que quiero pasar el nombre de la función miembro que debe ejecutar:

 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
}

¿Alguna solución?

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Eso debería funcionar:

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

 let func = fns[inFunc]

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

0

  1. sí, esto no iba a ayudar con eso ;-)

  2. La respuesta de Konrad Linkowski está bien, pero tomé una ruta diferente (sugerida por 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!