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

99
Views
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 answers
Answer question

0

That should work:

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

  let func = fns[inFunc]

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

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