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

I have a variable

var functionName="giveVote"; 

What I need to do, I want to call a function stored in var functionName . I tried using functionName () ;, but this does not work. Please, help.

Change For the same issue, I have

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

rules is a differentiated function that takes the Name: method, here I have hardcoded txtInf. But I want to specify a javascript variable here to make my code shared. var methodName = "txtInf";

Here I want to first evaluate the name of a method before using it in a rule function.

 $(this).rules("add", {mehtodName: "^[a-zA-Z'.\s]{1,40}$" }); 
+6
javascript function jquery
source share
6 answers

You have several options: the two main ones include window[functionName]() or setTimeout(functionName, 0) . Make it a shot.

Edit: if your variable is just the name of a function string, use it; otherwise, if you really can assign a function reference to a variable (for example, var foo = function() {}; ), you can use the notation foo() or foo.apply(this, []) , etc.

Edit 2: To evaluate methodName in place to $(this).rules() , you simply apply the first syntax above (using the window object), for example:

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

I think you are asking - if not, please clarify a little more, and I will be happy to rewrite the solution.

+10
source share
  var functionName="giveVote"; 

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

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

 var functionName="giveVote"; 

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

  var functionName=window["giveVote"]; 

and then call him

 functionName(); 

Or directly

 window["giveVote"](); 

This is possible because any global function that you declare in JavaScript (global, like not part of any other object), then becomes part of the window object.

+7
source share

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

So, here is what you need to do, use the Name function to get the function object. As suggested above, you can use window [functionName] if the function is defined in the window area. But depending on how your code is structured, you will most likely have this function as part of some other object, and you will need to use this object to access the function. For example, if the objContainingFunction variable refers to an object that contains a function with the name defined in functionName, then you can call the function using

objContainingFunction [functionName] ().

And regarding your editing -

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

What is your requirement here? Because, as I mentioned earlier, the Name method is the line here.

+2
source share

Why just pass the string directly to where you want to call the function? Why keep it first? This 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 do not see much advantage in doing what you are trying to do.

+1
source share

As a rule, there is no good reason to use a string to pass a function reference. If you need to do this, just assign the function to a variable. Since functions are objects, it really is as simple as treating them like any other variable.

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

In the corresponding note, setTimeout(functionName, 0) only works because setTimeout will be the eval first parameter if it is a string. Better to avoid this for performance and security issues.

0
source share

I agree with the solution Justin is proposing by adding another scenario.

You can even take the name of the callback function stored in some object and then call it.

eg.

 var callback = this.objName.getHandlerName() ; //we get the current function to call from some logic callback(); //actual call to the function 
-one
source share

All Articles