[Javascript] defining functions before they're called

Brian L. Matthews blmatthews at gmail.com
Thu Aug 20 14:26:59 CDT 2009


On 8/20/09 11:05 AM, Paul Novitski wrote:
> Can you think of a reason why one shouldn't call a function before
> it's declared in the source code? For example:
>
>           window.onload = initialize;
>
>           function initialize()
>           {
>                   ...
>           }
>
>    

First, you're not calling the function. Second, that won't work, 
JavaScript will complain that initialize is not defined. You have to 
define any identifier before you use it, except the case where you're 
assigning to it, in which case it's made a global if not already defined 
(and although it works, it's considered bad practice).

Brian



More information about the Javascript mailing list