473,395 Members | 1,466 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,395 software developers and data experts.

Calling a function dynamically

Joe
I have 3 functions: ClientInfoA is doing something
ClientInfoB is doing something

SelectFunction2Run is a function to determine which function needed to
run based on the value of the variable Method2Run. If the clientType
is A, it would run ClientInfoA function. If it is clientType B, it
would run the ClientInfoB function. Based on the value of Method2Run,
how would I run the function dynamically? I know that there are many
ways not to do this. But I am creating this for easy maintenance.

Thanks all


function ClientInfoA ()
{
....
....
}

function ClientInfoB ()
{
....
....
}

Function SelectFunction2Run
var Method2Run
switch (clientType)
{
case "A": Method2Run = ClientInfoA
break;
case "B": Method2Run = ClientInfoB
break
}
// how do I run the Method2Run????
Run Method2Run (whatever the value)
Jul 23 '05 #1
2 1793
Ivo
"Joe" wrote
I have 3 functions: ClientInfoA is doing something
ClientInfoB is doing something

SelectFunction2Run is a function to determine which function needed to
run based on the value of the variable Method2Run. If the clientType
is A, it would run ClientInfoA function. If it is clientType B, it
would run the ClientInfoB function. Based on the value of Method2Run,
how would I run the function dynamically? I know that there are many
ways not to do this. But I am creating this for easy maintenance.

function ClientInfoA () {...}

function ClientInfoB () {...}

Function SelectFunction2Run
Strange line, this. A capital F, no brackets, no accolades...
var Method2Run
switch (clientType)
{
case "A": Method2Run = ClientInfoA
break;
case "B": Method2Run = ClientInfoB
break
}
// how do I run the Method2Run????
Run Method2Run (whatever the value)


According to this, the choice is made based on the value of a global
variable "clientType" which presumably gets set elsewhere. Then there is no
need for an extra variable "Method2Run". See this:

function SelectFunction2Run (){
if(clientType=='A') ClientInfoA(); else
if(clientType=='B') ClientInfoB(); else
alert('Sorry, could not determine the method to run.');
}

There are other ways. Some would use eval().
HTH
Ivo
Jul 23 '05 #2
Joe wrote:
I have 3 functions: ClientInfoA is doing something
ClientInfoB is doing something

SelectFunction2Run is a function to determine which function needed
to run based on the value of the variable Method2Run. If the
clientType is A, it would run ClientInfoA function. If it is
clientType B, it would run the ClientInfoB function.
Only 2 types of client?
Based on the value of Method2Run,
how would I run the function dynamically? I know that there are many
ways not to do this. But I am creating this for easy maintenance. <snip> function ClientInfoA ()
{
....
}

function ClientInfoB ()
{
....
}

Function SelectFunction2Run ^ ^^
An upper case - F - will not help. An arguments/parameters list and the
opening brace would probably also be a good idea.
var Method2Run
switch (clientType)
{
case "A": Method2Run = ClientInfoA
break;
case "B": Method2Run = ClientInfoB
break
}
Not having a -default: - in a switch statement is asking for trouble.
// how do I run the Method2Run????
Run Method2Run (whatever the value)


The literal answer would be:-

function SelectFunction2Run(){
var Method2Run
switch (clientType){
case "A": Method2Run = ClientInfoA;
break;
default: Method2Run = ClientInfoB;
break;
}
Method2Run();
}

But since - Method2Run - is a local variable and will go out of scope as
soon as the function exits you may as well just call one of the two
functions directly based on the - clientType - variable:-

function SelectFunction2Run(){
if(clientType == "A"){
ClientInfoA();
}else{
ClientInfoB();
}
}

- or slightly more compactly:-

function SelectFunction2Run(){
((clientType == "A")?ClientInfoA:ClientInfoB)();
}

But that leaves you calling - SelectFunction2Run - whenever you wanted
to call which ever of the two functions was to be used, and repeating
the test on each call. A more interesting approach is to define the
whole lot as one function, call that function each time you wanted the
facility but have the function re-configure itself on the first occasion
it was called. Thus subsequent testing is not necessary and code using
the function exclusively uses one identifier and has no idea that the
function re-configured itself the first time it was called (I am
assuming that - clientType - does not change while the script is
running):-

function callThisFunction(){
function ClInfoA(){
...
}
function ClInfoB(){
...
}
/* Replace this function with one of its two inner functions
so subsequent calls will execute that function directly
instead. And call whichever function is assigned as the
replacement so it can act for this initial function call:-
*/
(this.callThisFunction = ((clientType=="A")?ClInfoA:ClInfoB))();
}

Apart from the global - clientType - variable (which is almost certainly
a mistake and should be replaced with feature detection directly related
to the choice being made at this point), the result is self-contained,
easily portable and outwardly simple. All of which contribute towards
easy maintenance.

Richard.
Jul 23 '05 #3

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

Similar topics

4
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
5
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? ...
5
by: Simon Harris | last post by:
Hi All, I am trying to call a method in a web form, from an event fired in a user control. My user control displays a map, which has a link button to enlarge/shrink the map. When the user...
35
by: Michel Sanner | last post by:
Hello, One of the greatest feature of Python in my opinion is the way the interpreter can be used to integrate a wide variety of software packages by dynamically linking them. This approach has...
3
by: hardieca | last post by:
My apologies if this gets posted twice... I am trying to determine whether or not a class has a function, and if it does, build a reference to it dynamically which I can then use to access the...
5
by: sfeher | last post by:
Hi, Is there a way to know when a function is available for me to call it from a dynamically loaded a javascript? I use this code to load the include.js file and then I call testIncludeFn()...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.