Using a string as a function name

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

    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
  • Steve van Dongen

    #2
    Re: Using a string as a function name

    On 11 Aug 2003 09:29:23 -0700, andrew@euperia. com (Euperia) wrote:
    [color=blue]
    >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"];[/color]

    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.
    [color=blue]
    >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?[/color]

    Regards,
    Steve

    Comment

    • Douglas Crockford

      #3
      Re: Using a string as a function name

      > I would like to use a string from an array to call a function. For[color=blue]
      > 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
      > }[/color]

      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() {
      ...
      }];



      Comment

      • Vjekoslav Begovic

        #4
        Re: Using a string as a function name


        "Euperia" <andrew@euperia .com> wrote in message
        news:b1790405.0 308110829.1cd50 4@posting.googl e.com...[color=blue]
        > 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[/color]

        for (i = 0; i < myFunctions.len gth; i++) {
        errorcode = window[myFunctions[i]]();
        }


        Comment

        Working...