[Javascript] Good vs. Eval (was Form validation)

Peter Brunone peter at brunone.com
Wed Jan 21 09:36:59 CST 2004


	Thanks for the input; you all have inspired me to take a
different approach.

-Peter

-----Original Message-----
From: javascript-bounces at LaTech.edu On Behalf Of Håkan Magnusson

Of course, another argument would be that eval is insanely heavy on the 
parser, and mr Greenberg provided the de facto solution for your 
particular problem. We are using the exact same method to register 
functions for certain objects (with an object id in the array of
functions).

I, personally, know that the function IS useful in some areas. I've 
written debuggers and CM-like systems in pure JavaScript and in such 
applications it is sometimes necessary to pass a string of javascript 
code to the parser, making eval the only alternative.

BUT HOWEVER, the performance impact of using eval (run a couple of tests

if you doubt me) and the number of "correct" ways of doing things 
instead of just pissing it all out through the eval function clearly 
tells us that if you are not absolutely, totally and indisputably sure 
that there is no other way known to mankind to fix your problem, don't 
use the function.

 > Peter Brunone wrote:
 >   IMO, this is a great discussion; keep it coming!

I agree, great thread.

Hakan

Jeff Greenberg wrote:

> You refer to the function object without the parentheses:
> 
> function myFunction()
> {
>     //do something here
> }
> 
> functionList=new Array();
> 
> functionList["myFunction"]=myFunction;
> 
> Then to execute the function, you use the parentheses:
> 
> functionList["myFunction"]();
> 
> 
> Peter Brunone wrote:
> 
>>   But how do you get the array to point to the *execution* of the 
>> function rather than the return value of the function?  That is, if I

>> want to set functionList["myFunction"] equal to myFunction(), how do 
>> I get it in there so that I can use it?
>>
>>   IMO, this is a great discussion; keep it coming!
>>
>>Original Message:
>>>From: Jeff Greenberg <jeff at websciences.org>
>>
>>>One of the easiest ways to call a function based on a string is to 
>>>index
>>>into an array or an object like so:
>>>
>>>functionList["myFunction"]();
>>>
>>>eval is not inherently bad or inefficient, it is just often used
>>>improperly or in cases where more thought should go into the design
and 
>>>architecture of the code. It is, in fact, indispensible if you are 
>>>trying to extend the core javascript language or work with
dynamically 
>>>generated/modified code. It should not be used in "every day" code,
and 
>>>if you find yourself using it in a small bit of code, it is likely
you 
>>>are misusing it. Nothing is set in stone of course, and this is just
a 
>>>general guideline.





More information about the Javascript mailing list