Web Analytics Made Easy -
StatCounter eval + execute evaluated string ? - CodingForum

Announcement

Collapse
No announcement yet.

eval + execute evaluated string ?

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

  • eval + execute evaluated string ?

    lets say that I m sending to some func what to execute
    lets say that I have an object 'obj' having methods 'doIt','doIt1', ....


    Code:
    execute('obj.doIt');
    
    function execute(param)
    {
       // here could be something like... but this is ugly and lots of work.
       if (param == 'obj.doIt')
       {
          obj.doIt;      
       }
       
       if (param == 'obj.doIt1')
       {
          obj.doIt1;      
       }
    
       // instead there I would like to have something like ....
       evalAndExecute(param);
    
       // so: one line instead of milions (-:
       // can this be done somehow ?
    
    
    
    }
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Yes, sure, ina single code line, based on the fact that a function is in fact a window's property. Is this what you need?:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function myFunc1(){
    alert('here I am, Function1!')
    }
    function myFunc2(){
    alert('here I am, Function2!')
    }
    function callFunctions(which){
    window[which]();
    }
    </script>
    </head>
    <body>
    <span onclick="callFunctions('myFunc1')">call the function 1</span>
    <br>
    <br>
    <span onclick="callFunctions('myFunc2')">call the function 2</span>
    </body>
    </html>
    Last edited by Kor; Sep 23, 2006, 12:03 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      Or, - probably this is what you need - following the same pattern (but I don't know how your methods are written as):

      Code:
      function execute(param)
      {
      obj[param];
      }
      KOR
      Offshore programming
      -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

      Comment


      • #4
        yeah, great tip, but why does the second example not working --> window[which] is not a function

        Code:
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Untitled Document</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <script type="text/javascript">
        function myFunc()
        {
          alert('here I am!')
        }
        
        
        function obj1()
        {
          this.method1 = function()
          {
           alert('here I am too!');
          }; 
          
        }
        
        function callFunction(which)
        {
          window[which]();
        }
        </script>
        </head>
        <body>
        <DIV onclick="callFunction('myFunc')">call the function</DIV>
        <DIV onclick="callFunction('obj1.method1')">call the function1</DIV>
        </body>
        </html>
        Found a flower or bug and don't know what it is ?
        agrozoo.net galery
        if you don't spot search button at once, there is search form:
        agrozoo.net galery search

        Comment


        • #5
          correcting code from before, there was no 'new', but still doesn't work...
          Code:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <title>Untitled Document</title>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <meta http-equiv="Content-Style-Type" content="text/css">
          <meta http-equiv="Content-Script-Type" content="text/javascript">
          <script type="text/javascript">
          var obj1;
          
          function myFunc()
          {
            alert('here I am!')
          }
          
          
          function Obj1()
          {
            this.method1 = function()
            {
             alert('here I am too!');
            }; 
            
          }
          
          function callFunction(which)
          {
            window[which]();
          }
          
          function callFunction1(which)
          {
            window[which]();
          }
          
          obj1 = new Obj1();
          
          </script>
          </head>
          <body>
          <DIV onclick="callFunction('myFunc')">call the function</DIV>
          <DIV onclick="callFunction1('obj1.method1')">call the function1</DIV>
          </body>
          </html>
          Found a flower or bug and don't know what it is ?
          agrozoo.net galery
          if you don't spot search button at once, there is search form:
          agrozoo.net galery search

          Comment


          • #6
            hmm, I managed to do some stuff, but this is still not it:

            http://86.61.66.17:8084/AgroZoo/testHTML/testHTML.html
            Found a flower or bug and don't know what it is ?
            agrozoo.net galery
            if you don't spot search button at once, there is search form:
            agrozoo.net galery search

            Comment


            • #7
              You have to define your "method". What do you mean by "method", anyway? Which is your final goal, after all?
              -----
              You see... everything on a web page can be handle as an object, and every object (except the window, which is the basic root) can be treated at the same time as somebody's "superior" property. Functions belong to the window, global variables to the window, as well, local to the functions which nested them, attributes and methods (I mean javascript native methods) to the elements. Prototypes are also properties of a native element or of a new created object...
              Last edited by Kor; Sep 23, 2006, 02:20 PM.
              KOR
              Offshore programming
              -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

              Comment


              • #8
                see also
                JSON
                KOR
                Offshore programming
                -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                Comment


                • #9
                  for all interested, here is how this can be done.

                  Code:
                  function Obj1()
                  {
                    this.method1 = function()
                    {
                     alert('here I am too!');
                    }; 
                    
                  }
                  
                  
                  
                  function callFunction1(obj,method)
                  {
                    window[obj][method]();
                  }
                  
                  <DIV onclick="callFunction1('obj1','method1')">call the function1</DIV>
                  Found a flower or bug and don't know what it is ?
                  agrozoo.net galery
                  if you don't spot search button at once, there is search form:
                  agrozoo.net galery search

                  Comment

                  Working...
                  X