0

Possible Duplicate:
Dynamic function name in javascript?

I want to achieve the below result .Can someone tell me how.Basically name is dynamic.

 var name =  'tab'+ ID;    
name(tab);

So far tried stuff which works fine.Is it a good way of doing or else somebody can suggest another way.

var function_name  =  't'+ ID; 
if (typeof(window[function_name]) === "function")
{
    window[function_name](tab);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user505210
  • 1,362
  • 11
  • 28
  • 50
  • Note that the above only works if the function being called is in the global scope, or if you explicitly put it there. – alexp Feb 04 '13 at 23:23

1 Answers1

2

I'd first create an object to store your functions:

var funcs = {};

Then you can just use bracket notation:

var id = '001';

funcs['tab'+ id] = function(tab) {

};

funcs.tab001(tab);
// or if you have special chars...
funcs['tab001'](tab);
elclanrs
  • 92,861
  • 21
  • 134
  • 171