Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2009, 12:49 AM   PM User | #1
lbjvg
New to the CF scene

 
Join Date: Dec 2009
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
lbjvg is an unknown quantity at this point
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..
lbjvg is offline   Reply With Quote
Old 12-15-2009, 01:23 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
lbjvg (12-15-2009)
Old 12-15-2009, 02:29 AM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
lbjvg (12-15-2009)
Old 12-15-2009, 02:52 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-15-2009, 05:51 AM   PM User | #5
lbjvg
New to the CF scene

 
Join Date: Dec 2009
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
lbjvg is an unknown quantity at this point
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
lbjvg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:32 AM.


Advertisement
Log in to turn off these ads.