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

Jeff Greenberg jeff at websciences.org
Tue Jan 20 13:05:52 CST 2004


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.
>>
>>Peter Brunone wrote:
>>
>>    
>>
>>>  No problem; just start a new thread!
>>>
>>>  What if you want to run a function based on a string?
>>>
>>>-Peter B.
>>>      
>>>
>
>
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript
>
>  
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20040120/6dc738cc/attachment.htm>


More information about the Javascript mailing list