Announcement

Collapse
No announcement yet.

ASlternative to eval() function??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ASlternative to eval() function??

    Hi,

    Is there a more efficient way [than eval()] to evaluate a string to its value?
    eval() is quite time consuming [calls take about three times longer] and I use it a lot.

    If there is no better way, is it possible to populate an array with function names? I can do that once, than call the function from the array instead.
    Ex:
    var abcMx = new Array();

    in main
    abcMX[0] = abcM0;
    abcMx[1] = abcM1;
    etc.

    where abcM0, abcM1 are function names, not real values.
    Should something like that work?

    Note: This is related to an earlier post, which it has been cancelled [before I implemented the eval thing].

    Thank you
    Mihai Buta

  • #2
    Re: ASlternative to eval() function??

    Hello Mihai,

    Originally posted by mbuta
    Hi,

    Is there a more efficient way [than eval()] to evaluate a string to its value?
    eval() is quite time consuming [calls take about three times longer] and I use it a lot.

    If there is no better way, is it possible to populate an array with function names? I can do that once, than call the function from the array instead.
    Ex:
    var abcMx = new Array();

    in main
    abcMX[0] = abcM0;
    abcMx[1] = abcM1;
    etc.

    where abcM0, abcM1 are function names, not real values.
    Should something like that work?
    To the best of my knowledge, eval() is the only solution to this routine. Even if the string is stored in an array, the string cannot be evaluated into a function call unless it is passed to the eval() function. It will not matter if the strings are stored in a list of number variables or an array.


    Note: This is related to an earlier post, which it has been cancelled [before I implemented the eval thing].

    Thank you
    You had edited the post you're referring to stating that you were trying to delete it so it was removed for you. If you'd like that post to be reactivated, just let me know.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Thank you Jason.
      Mihai Buta

      Comment


      • #4
        You're most welcome.
        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment


        • #5
          Re: ASlternative to eval() function??

          Hi Mihai,

          I put a demo efs together that will do what you want.

          Originally posted by mbuta
          Hi,

          Is there a more efficient way [than eval()] to evaluate a string to its value?
          eval() is quite time consuming [calls take about three times longer] and I use it a lot.

          If there is no better way, is it possible to populate an array with function names? I can do that once, than call the function from the array instead.
          For the purpose of calling functions, the string is eval'ed and saved in an array, then later called.


          PHP Code:
          var commandSeed= ["demo0","demo1","demo2","demo3","demo4"]; // shortcut to create an array
          var commands = new Array();

          for (var 
          i=0;i<commandSeed.length;i++){ // strings eval'ed to functions here...
           
          commands[i] = eval(commandSeed[i]);
          }

          for (var 
          i=0;i<commands.length;i++){ // functions called here...
            
          commands[i](i); 
          }

          function 
          demo0(t){
            
          debugPrintln("this is a test 0th demo " +typeof(commands[t])+" called from a stored eval'ed " +typeof(commandSeed[t])+" saved in an array");
          }
          function 
          demo1(t){
            
          debugPrintln("this is a test 1st demo " +typeof(commands[t])+" called from a stored eval'ed " +typeof(commandSeed[t])+" saved in an array");
          }
          function 
          demo2(t){
            
          debugPrintln("this is a test 2nd demo " +typeof(commands[t])+" called from a stored eval'ed " +typeof(commandSeed[t])+" saved in an array");
          }
          function 
          demo3(t){
            
          debugPrintln("this is a test 3rd demo " +typeof(commands[t])+" called from a stored eval'ed " +typeof(commandSeed[t])+" saved in an array");
          }
          function 
          demo4(t){
            
          debugPrintln("this is a test 4th demo " +typeof(commands[t])+" called from a stored eval'ed " +typeof(commandSeed[t])+" saved in an array");

          here is the output:

          this is a test 0th demo function called from a stored eval'ed string saved in an array
          this is a test 1st demo function called from a stored eval'ed string saved in an array
          this is a test 2nd demo function called from a stored eval'ed string saved in an array
          this is a test 3rd demo function called from a stored eval'ed string saved in an array
          this is a test 4th demo function called from a stored eval'ed string saved in an array


          I hope this helps out.
          Attached Files

          Comment

          Working...
          X