0

this is an example function:

function processFanGrowth() {
    console.log('fanGrowth');
}

and an object "data" which has a property name "FanGrowth"

for(var property in data) {
      // here i'm trying to generate the function name.
      funcName = "process" + property;
      funcName();
}

i'm getting this error: Uncaught TypeError: string is not a function

tareq
  • 1,119
  • 6
  • 14
  • 28

2 Answers2

5

You can invoke the function as a property of the object whose scope it is defined in, e.g. for a function defined in global scope:

window['process' + property]();
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

This syntax should work:

window["functionName"](arguments);

In your case:

for(var property in data) {
      // here i'm trying to generate the function name.
      funcName = "process" + property;
      window["functionName"]();
}
Husman
  • 6,819
  • 9
  • 29
  • 47