1

Suppose there is a function (from a module) named fiveStar and I want to call it using a variable,

For e.g using window, I could do this:

const num = 'five';
const functionName = `${num}Star';
window[functionName]();

Is there any other way I could do this without using window?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
kob003
  • 2,206
  • 2
  • 12
  • 19

2 Answers2

1

You could use an object to group functions together.

const myModule = {
   fiveStar: ()=>{}
   //other methods...
};
const num = 'five';
const functionName = `${num}Star`;
myModule[functionName]();
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

should work on any object with a function declared in it

Example:

let obj = {
  fiveStar: () => {
    console.log('called me')
  }
}

let str = 'fiveStar'
obj[str]()
abc123
  • 17,855
  • 7
  • 52
  • 82