Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    alternatives to the use of the eval function

    Hi - I wrote some code that uses the eval function. I am in the process of rewriting the code to improve on it where possible (a learning exercise). I have read that eval is evil because of security concerns and that the eval'd code is not compiled prior to runtime.

    I don't think my use of eval presents a security concern. However, I wonder if there is some way to replace the eval function with a more natural technique.

    I have done this using eval(); also using a new Function method. I have tried using a window[] method but I can't get that to work.

    OK, on the the actual code:

    The purpose of the javascript is to count keystrokes.

    I have a bunch of global counter variables declared in this fashion:
    Code:
    MyLib.CountQ=0;
    MyLib.CountW=0;
    MyLib.CountE=0;
    MyLib.CountR=0;
    MyLib.CountT=0;
    MyLib.CountY=0;
    MyLib.CountU=0;
    MyLib.CountI=0;
    MyLib.CountO=0;
    MyLib.CountP=0;
    When the Count function is called I want to update the appropriate counter. Thus: MyLib.Count'+varkeyname+'+=1; Where varkeyname is 'q' or 'w' or 'e' etc.




    Code:
    //MyLib is a global variable namespace.
    
    function Count()	{//updates counters
    var varkeyname= specialchars(MyLib.KeyName);//identity of the key pressed.
    var stra="Count"+varkeyname;// string used to change an html value.
    
    //var strb=eval("MyLib.Count"+varkeyname+"+=1");//original eval function that worked just fine.
    
    var strb='return MyLib.Count'+varkeyname+'+=1';//string to update a counter.
    
    var myfunc= new Function(strb);//Function intended to replace the eval function - this also works just fine.
    
    document.getElementById(stra).value = myfunc();
    
    ...
    You can see that I have replaced the eval function with another function - but since this is also not compiled prior to runtime I don't think this is an improvement. I have read about using the window[string] method but I can't get it to work.

    This is not a critical issue but I'd appreciate any comments.

    Thanks, Jim
    Last edited by lbjvg; 12-15-2009 at 05:53 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,548
    Thanks
    77
    Thanked 4,382 Times in 4,347 Posts
    Code:
    MyLib.Count = [];
    ...
    MyLib.Count["Q"] = 0;
    MyLib.Count["W"] = 0;
    ...
    function Count()
    {
        var varkeyname= specialchars(MyLib.KeyName);
        ++MyLib.Count[varkeyname];
        ...
    Or initialize it even simpler:
    Code:
    <script>
    var Count = { "Q": 0, "W": 0, "E": 0, "R": 0, "T": 0, "Y": 0 }
    
    Count["Y"] += 17;
    ++Count["Q"];
    var msg = "";
    for ( var c in Count )
    {
        msg += c + "::" + Count[c] + "\n";
    }
    alert(msg);
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    lbjvg (12-15-2009)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Function() is arguably worse than eval, forget about it...

    Code:
    eval("MyLib.Count"+varkeyname+"+=1")
    could be simply
    Code:
    MyLib["Count"+varkeyname]+=1;
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    lbjvg (12-15-2009)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,548
    Thanks
    77
    Thanked 4,382 Times in 4,347 Posts
    LOL! Right you are! Trust me to go around the mountain in the other direction.

    Well, I suspect mine would *barely* outperform that, but if he had to restructure other code clearly that's a winner.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Excellent replies.

    This advances my understanding of syntax. The simpler solution was what I was hoping for right now since I can use it as is; but I may eventually decide to put the counter variables into an array or a list to make the code more compact. Thanks! - Jim Gallagher


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •