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
  • 46,070
  • 7
  • 44
  • 60
kob003
  • 810
  • 1
  • 8
  • 14

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
  • 46,070
  • 7
  • 44
  • 60
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,161
  • 6
  • 49
  • 78