473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
Jul 23 '05 #1
4 1223
Tom Szabo wrote:
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)]

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
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);

Jul 23 '05 #3
Many thanks, got it!!!
"Lee" <RE**************@cox.net> wrote in message
news:co*********@drn.newsguy.com...
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);

Jul 23 '05 #4
Thank you!

"Randy Webb" <Hi************@aol.com> wrote in message
news:9s********************@comcast.com...
Tom Szabo wrote:
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)]

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: schaf | last post by:
Hi ! I'm writing a new xsl:function, which uses two other functions. But by the call of the first function, it would be abort just after the call. Not even the xsl:param would be set. I don't...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
5
by: Matt Clepper | last post by:
Any way to do this? I need to call functions based on a variable. Do I actually have to make a case statement and call each funciton explicitly, or is there any way to call a function where the...
3
by: Chris | last post by:
Hello everyone I want to call a DB2 7.2 stored procedure via ADO in VC++ 7.1 It has 4 parameters, the 1st and the 4th are OUTPUT and the others are INPUT params. My code looks like this:...
5
by: Alex | last post by:
I have a load of function names stored in a table which I want to use to decide which function to run. I can select the function name but I cannot work out how to call the function if I have the...
3
by: XenofeX | last post by:
How can i call to object from string variable ? For instance string x = "textBox1"; i want to call the object which name is stored in the x string variable. The most important thing is the...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
3
by: aroldao | last post by:
Greetings Everyone, In php it's possible to create a new object based on a name stored in variable, i.e: $className = "CSome$name"; $newclass = new $className(); However is it possible to...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.