[Javascript] defining functions before they're called

Paul Novitski paul at juniperwebcraft.com
Thu Aug 20 15:58:51 CDT 2009


>On 8/20/09 11:05 AM, I rather sloppily 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()
> >           {
> >                   ...
> >           }
> >


Thanks for your comments. I apologize for saying 'call' when my 
example wasn't a call; that was the result of changing my example at 
the last second and not proofreading the supporting text. However, 
the example I gave still holds because my onload assignment did refer 
to a function not yet defined in the source. And the onload 
assignment example above runs just fine.

So does this (an actual call this time!):

         showme();

         function showme()
         {
                 alert('hi');
         }

I'm not getting a runtime error in either case, which means that the 
compiler, as I would expect, assembles a namespace of all the 
functions in the script before starting execution.


At 8/20/2009 12:26 PM, Brian L. Matthews wrote:
>First, you're not calling the function. Second, that won't work,
>JavaScript will complain that initialize is not defined.

Not in my browsers (Firefox 3.5 and IE 8). Do you know of a browser 
that gacks on it?


>You have to define any identifier before you use it

What exactly do you mean by "have to"? The following example (of 
referring to an undefined variable) doesn't faze the JavaScript interpreter:

         // first and only instance of 'something' in the script
         alert(something);

All it does is alert 'undefined'. The entire script is compiled and 
the entire script is executed without any abort, a clear indication 
that it's legal syntax.


>, except the case where you're
>assigning to it, in which case it's made a global if not already defined

Another exception appears to be function declarations.


>(and although it works, it's considered bad practice).

This is the very point I'm questioning. Why is it considered bad 
practice if it always works? Can you point to an example of it 
failing? Or can you find any JavaScript or ECMAScript documentation 
that cautions against it, and if so what's the rationale?

To emphasize my assertion that it's syntactically legal to call a 
function before it's declared in the source, I need only point out 
that we can run a script in which two functions call each other:

         function a() { b(); }
         function b() { a(); }

It wouldn't be possible even to compile such a script if every 
function had to be declared before it was called because one of those 
functions must occur before the other in the source code. (If written 
without limiting conditions, two mutually-calling functions could 
result in a stack overflow during execution, but that's beside the 
point. I'm not recommending such logic, I'm just saying it's legal syntax.)

And if it's not a requirement of the language interpreter, whose 
requirement is it? Is it possible that the proscription against 
calling-before-declaring originates in other languages in which doing 
so would result in aborted compilation or erroneous execution?


The reason I'm belaboring this point is that I'm writing a JavaScript 
language tutorial and I don't want to give bad advice. If anyone can 
demonstrate why source order matters I'll reconsider my position, 
which is currently that it doesn't matter and thus I'm free to 
promulgate my personal preference for top-down source order.

Thanks,

Paul
__________________________

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com 




More information about the Javascript mailing list