How to call a function that's name is stored as a string in a variable???

T

Tom Szabo

Hi,

If say there is "myfunc"

where

"myfunc(n){ return 2 * n;}"

and

cFuncName = "myfunc";

what is the syntax to call the function from the var cFuncName??

cFuncName(12);

doesn't seem to work

TIA,

Tom
 
L

Lee

Tom Szabo said:
Hi,

If say there is "myfunc"

where

"myfunc(n){ return 2 * n;}"

and

cFuncName = "myfunc";

what is the syntax to call the function from the var cFuncName??

cFuncName(12);

doesn't seem to work

Why would you do that?
Store a reference in a variable, instead of the function's name:

myFuncRef=myfunc;
myFuncRef(12);

If you must, the syntax is (assuming cFuncName is global):

window[cFuncName](12);
 
R

Randy Webb

Tom said:
Hi,

If say there is "myfunc"

where

"myfunc(n){ return 2 * n;}"

and

cFuncName = "myfunc";

what is the syntax to call the function from the var cFuncName??

cFuncName(12);

doesn't seem to work

window[cFuncName(12)]
 
T

Tom Szabo

Many thanks, got it!!!


Lee said:
Tom Szabo said:
Hi,

If say there is "myfunc"

where

"myfunc(n){ return 2 * n;}"

and

cFuncName = "myfunc";

what is the syntax to call the function from the var cFuncName??

cFuncName(12);

doesn't seem to work

Why would you do that?
Store a reference in a variable, instead of the function's name:

myFuncRef=myfunc;
myFuncRef(12);

If you must, the syntax is (assuming cFuncName is global):

window[cFuncName](12);
 
T

Tom Szabo

Thank you!

Randy Webb said:
Tom said:
Hi,

If say there is "myfunc"

where

"myfunc(n){ return 2 * n;}"

and

cFuncName = "myfunc";

what is the syntax to call the function from the var cFuncName??

cFuncName(12);

doesn't seem to work

window[cFuncName(12)]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top