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
39k25 gold badges207 silver badges282 bronze badges
asked Feb 4, 2013 at 23:15
1
  • Note that the above only works if the function being called is in the global scope, or if you explicitly put it there. Commented Feb 4, 2013 at 23:23

1 Answer 1

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);
answered Feb 4, 2013 at 23:19
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.