[Javascript] defining functions before they're called

Peter Brunone peter at brunone.com
Thu Aug 20 13:53:03 CDT 2009


Hi Paul,

    In the code below, you are not calling the function before it is 
defined.  You're simply assigning a pointer to it from an event handler; 
this event won't fire before the function is loaded, so you should never 
see a problem.

    However, if your code looked like this:

initialize();

function initialize() {
...
}

...then I'd be willing to bet five bucks that you'll find a JavaScript 
error at run time.

Cheers,

Peter

----------------------------------------

From: "Paul Novitski" <paul at juniperwebcraft.com>
Sent: Thursday, August 20, 2009 1:38 PM
To: "JavaScript List" <javascript at lists.evolt.org>
Subject: [Javascript] defining functions before they're called 

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()
{
...
}

I can certainly understand why one might choose, stylistically, to 
always define functions before calling them. However, I have never 
experienced any problem using JavaScript by putting the call before 
the function being called. It's my personal preference to put the 
call first because I like to read scripts top-down, seeing how a 
function is used before reading the details of exactly what the 
function does. But I'm curious to know if any of you has ever 
encountered a problem doing so.

Regards,

Paul
__________________________

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

_______________________________________________
Javascript mailing list
Javascript at lists.evolt.org
http://lists.evolt.org/mailman/listinfo/javascript

 



More information about the Javascript mailing list