Subj : Re: Calling a function before it's declared To : netscape.public.mozilla.jseng From : Brook Monroe Date : Wed Nov 24 2004 09:55 am Egor Senin wrote: > Hi ! > > Is there any way to make the following code work in Rhino: > > funcThatIsDeclaredLater(); > function funcThatIsDeclaredLater(){ > // do something > } > > The problem is that the function is declared after it's called. > > Egor. You don't need to do that in JavaScript. Declaration order isn't critical. Calling functions that haven't been defined yet will work because the call resolution occurs at run-time, not compile time. The following is perfectly legal (and I tested it): function callit() { return callthem(); } function callme() { return callyou(); } function callyou() { return 2; } function callthem() { return callme(); } Calls to callit() will return 2. If this example doesn't work in Rhino, it's because there's a problem in Rhino. (And I don't think there would be, although I'm using SpiderMonkey. I have no reason to believe that Rhino would be different in this respect, because both are following ECMA.) So far as I know, ECMA-262 doesn't allow C-style function pre-declaration--but I haven't read the spec in over 2 years, and things change. .