Subj : Re: Calling a function before it's declared To : Brook Monroe From : Brendan Eich Date : Wed Nov 24 2004 05:07 pm Brook Monroe wrote: > Martin Honnen wrote: > >> >> >> Brook Monroe wrote: >> >> >>> 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. >> >> >> >> You can call a function before its declaration as program execution is >> defined to be a two pass execution where the first pass goes through >> the program and executes all function declarations and then the second >> pass executes the global statements (e.g function calls) in source order. >> > Same difference. Although that doesn't answer the question of why > JS_CompileFile() (which, according to that explanation, would "execute" > the function declarations) doesn't allow direct function calls through > JS_CallFunctionName() without going through JS_EvaluateScript() first. Because compiling in the JS API has nothing to do with executing ("according to that explanation" mixes apples and oranges -- JS API rules are apples, Martin's explanation of parts of ECMA-262 section 10 are the oranges). That's it, there isn't anything mysterious -- but it's deeper than you make out above. Consider how JS_CompileFile has no effect on the object passed as the second parameter. It would actually take extra code (specifically, a JS_ExecuteScriptPart(...PROLOG...) call after JS_CompileFile succeeded) to cause the declared functions and variables to be bound in the |obj| parameter. If there's a need for JS_EvaluateFile, we can add that easily, but it hasn't been a pressing issue in almost ten years. Much of the reason has to do with Unicode vs. locale- and file-dependent character set encodings. /be .