Calling a function dynamically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    Calling a function dynamically

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

    SelectFunction2 Run 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 SelectFunction2 Run
    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)
  • Ivo

    #2
    Re: Calling a function dynamically

    "Joe" wrote[color=blue]
    > I have 3 functions: ClientInfoA is doing something
    > ClientInfoB is doing something
    >
    > SelectFunction2 Run 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 SelectFunction2 Run[/color]

    Strange line, this. A capital F, no brackets, no accolades...
    [color=blue]
    > 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)[/color]

    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 SelectFunction2 Run (){
    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


    Comment

    • Richard Cornford

      #3
      Re: Calling a function dynamically

      Joe wrote:[color=blue]
      > I have 3 functions: ClientInfoA is doing something
      > ClientInfoB is doing something
      >
      > SelectFunction2 Run 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.[/color]

      Only 2 types of client?
      [color=blue]
      > 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.[/color]
      <snip>[color=blue]
      > function ClientInfoA ()
      > {
      > ....
      > }
      >
      > function ClientInfoB ()
      > {
      > ....
      > }
      >
      > Function SelectFunction2 Run[/color]
      ^ ^^
      An upper case - F - will not help. An arguments/parameters list and the
      opening brace would probably also be a good idea.
      [color=blue]
      > var Method2Run
      > switch (clientType)
      > {
      > case "A": Method2Run = ClientInfoA
      > break;
      > case "B": Method2Run = ClientInfoB
      > break
      > }[/color]

      Not having a -default: - in a switch statement is asking for trouble.
      [color=blue]
      > // how do I run the Method2Run????
      > Run Method2Run (whatever the value)[/color]

      The literal answer would be:-

      function SelectFunction2 Run(){
      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 SelectFunction2 Run(){
      if(clientType == "A"){
      ClientInfoA();
      }else{
      ClientInfoB();
      }
      }

      - or slightly more compactly:-

      function SelectFunction2 Run(){
      ((clientType == "A")?ClientInfo A:ClientInfoB)( );
      }

      But that leaves you calling - SelectFunction2 Run - 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 callThisFunctio n(){
      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.callThisF unction = ((clientType==" A")?ClInfoA:ClI nfoB))();
      }

      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.


      Comment

      Working...