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
user505210
1,43211 gold badges30 silver badges50 bronze badges
-
Note that the above only works if the function being called is in the global scope, or if you explicitly put it there.alexp– alexp2013年02月04日 23:23:45 +00:00Commented Feb 4, 2013 at 23:23
1 Answer 1
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
elclanrs
94.2k21 gold badges137 silver badges171 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js