Javascript: How to call function using the value stored in a variable?

I have a variable

var functionName="giveVote";
What I need to do is, I want to call function stored in var functionName. I tried using functionName(); . But its not working. Please help.
Edit Based on the same problem, I have
$(this).rules("add", {txtInf: "^[a-zA-Z'.\s]{1,40}$" }); 
rules is a predifined function which takes methodName:, here I have hardcoded txtInf. But I want to supply a javascript variable here, to make my code generic. var methodName="txtInf";
Here I want to evaluate methodName first before being used in rules function.
$(this).rules("add", {mehtodName: "^[a-zA-Z'.\s]{1,40}$" });
 
Answer is:
 
Why not just pass the string directly to where you want to call the function? Why store it first? That seems more confusing as it is another layer of indirection (and, ultimately, can make your code more difficult to debug).
For example:
$('.someSelector').click(giveVote);
I don't see a particular advantage to doing what you're trying to do.
  

0 comments: