473,405 Members | 2,282 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,405 software developers and data experts.

Using a string as a function name

Hi

I would like to use a string from an array to call a function. For
example, I have a list of functions called A.B,C,D etc and for various
situations I need to call a a different range of functions:

var myFunctions = ["A","C","G","B","E"];

for (i = 0; i < myFunctions.length; i++) {
errorcode = myFunctions[i](); // I want to call A(); B() etc
}
Is it possible with Javascript, and if so, how?

Regards
Andrew
Jul 20 '05 #1
3 19443
On 11 Aug 2003 09:29:23 -0700, an****@euperia.com (Euperia) wrote:
Hi

I would like to use a string from an array to call a function. For
example, I have a list of functions called A.B,C,D etc and for various
situations I need to call a a different range of functions:

var myFunctions = ["A","C","G","B","E"];
Change this line to
var myFunctions = [A,C,G,B,E];
so that you have an array of function references instead of an array
of strings.
for (i = 0; i < myFunctions.length; i++) {
errorcode = myFunctions[i](); // I want to call A(); B() etc
}
Is it possible with Javascript, and if so, how?


Regards,
Steve
Jul 20 '05 #2
> I would like to use a string from an array to call a function. For
example, I have a list of functions called A.B,C,D etc and for various
situations I need to call a a different range of functions:

var myFunctions = ["A","C","G","B","E"];

for (i = 0; i < myFunctions.length; i++) {
errorcode = myFunctions[i](); // I want to call A(); B() etc
}


Functions are values. You can use myFunctions to store the functions themselves,
not just their names.

var myFunctions = [A, C, G, B, E];

This assumes that function A et al have been defined.

You could go a step further and define the functions in the array literal.

var myFunctions = [function A() {
...
}, function C() {
...
}, function G() {
...
}, function B() {
...
}, function E() {
...
}];

http://www.crockford.com/

Jul 20 '05 #3

"Euperia" <an****@euperia.com> wrote in message
news:b1************************@posting.google.com ...
Hi

I would like to use a string from an array to call a function. For
example, I have a list of functions called A.B,C,D etc and for various
situations I need to call a a different range of functions:

var myFunctions = ["A","C","G","B","E"];

for (i = 0; i < myFunctions.length; i++) {
errorcode = myFunctions[i](); // I want to call A(); B() etc


for (i = 0; i < myFunctions.length; i++) {
errorcode = window[myFunctions[i]]();
}
Jul 20 '05 #4

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

Similar topics

10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
3
by: Rob | last post by:
Hi all, I am having trouble converting the code below (found on http://vbnet.mvps.org/index.html?code/core/sendmessage.htm) into a format that will work using vb .NET. Can anyone have a look...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
4
by: Tugrul HELVACI | last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ?? I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String;...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.