473,609 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.len gth; 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 19447
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.len gth; 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.len gth; 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.googl e.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.len gth; i++) {
errorcode = myFunctions[i](); // I want to call A(); B() etc


for (i = 0; i < myFunctions.len gth; 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
19341
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 able to fill that one out just fine. The second form is multipart/form-data. Unfortunately, I haven't been able to fill that out in a way that makes the server happy. I set up a copy of this form at my web site so that I could see exactly what a...
3
24018
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 tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
11
6582
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
1
4006
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 live databases on the network. Is there a way, via code, when I get back in-house from being on the road to click a button, and select the backends I want to link to? I would want to delete all the current links and link to the "live"
1
5047
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 with the ADSI NT Provider and only supported in the LDAP Provider. So given an UserId (UID) how can I read the First Name and Last Name using LDAP Provider. If anybody can help me with a C# sample code it would of great help. Thanks in advance.
7
17288
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 to get it to work in Visual Basic. I tried coding it once myself from scratch and then modified a class that I found on a newsgroup (referenced below). Both seem to be doing the same thing and neither works (or rather, they seem to work but the...
2
5035
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 folder structure and naming conventions and then send a Net send message to those users telling them to rectify. The information I want to get is when you select the file/folder and then: Properties -> Security Tab -> Advanced Button -> Owner Tab ->...
3
5324
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 at it and let me know what I need to change. I have tried changing the "hwnd" type into intptr's but there seem to be other problems too, like it won't allow "lParam As Any" to be declared.
3
6794
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> </Root> I would like to retreive "\Root\Customer\Name" out of it. Something like:
4
3298
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; fPersonSurName : String; fPersonAge : Integer; published property PersonName : String read fPersonName write fPersonName;
0
8145
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8556
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4037
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2541
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1690
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.