views:

56

answers:

5

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}$" }); 
+1  A: 

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.

JasCav
+1  A: 

You have a handful of options - two basic ones include window[functionName]() or setTimeout(functionName, 0). Give those a shot.

Edit: if your variable is just the string name of a function, use those; otherwise, if you're able to actually assign a function reference to the variable (eg, var foo = function() {};), you could use the foo() notation, or foo.apply(this, []), etc.

Edit 2: To evaluate methodName in place prior to $(this).rules() firing, you'd just apply the first syntax above (using the window object), like so:

$(this).rules("add", {window[methodName](): ...});

I think that's what you're asking - if not, please clarify a bit more and I'll be happy to rewrite a solution.

mway
Nice, thanks for mentioning setTimeout, it should work.
vaibhav
Please see the edits.
vaibhav
Not sure what you mean. Are you saying that the regex there should be passed as an argument to the function? Or that it defines a template for nomenclature?
mway
@mway, dont consider regex. I just want to evaluate methodName value before being used.
vaibhav
See edit numero dos.
mway
+1  A: 
   var functionName="giveVote"; 

Assigns the String "giveVote" to the variable functionName. If giveVote is the name of the function, then you have to assign in one of the following ways:

If you know the function name at compile time, then you can

var functionName="giveVote"; 

But I think you want to dynamically assign the function and then call it. Then you have to say

   var functionName=window["giveVote"]; 

and then call it by

functionName();

Or directly

window["giveVote"]();

This is possible because any global funtion you declare in javascript (global as in not part of any other object), then it becomes part of the window object.

Nivas
A: 

There normally isn't a good reason for using a string to pass a function reference around. If you need to do this, just assign the function to a variable. Since functions are objects, it's really just as easy as treating them like any other variable.

var giveVote = function() { ... };

var someOtherFunction = function(callback) {
    callback();
};

someOtherFunction(giveVote);

On a related note, setTimeout(functionName, 0) only works because setTimeout will eval the first parameter if it is a string. It's best to avoid this for performance and security concerns.

Justin Johnson
+1  A: 

From what I understand you have the function's name stored in the variable functionName. Now this is different from holding a reference to the function itself. So, you cannot just use functionName(), because functionName refers to a String object here, not a function.

So, here's what you need to do, use the functionName to get the function object. As suggested above you can use window[functionName] if the function has been defined in the window scope. But in based on how your code is structured you would most probably have this function as a member of some other object, and you would need to use that object to access the function. For example, if variable objContainingFunction refers to an object that contains the function with the name defined in functionName, then you could call the function using

objContainingFunction[functionName]( ).

And regarding your edit -

$(this).rules("add", {mehtodName: "^[a-zA-Z'.\s]{1,40}$" });

What exactly is your requirement here? Because, as I mentioned earlier, methodName is a string here.

Kartik