On Jun 21, 9:19 am, Jonathan Fine <jf...@pytex.or gwrote:
VK wrote:
Yes, this behavior is by design and cross-browser. Do not forget that
function declarations are not a part of the program execution flow.
Thanks for this. I guess there may be a difference between function
declaration and function definition. For example
f = function f(){print('hi') }
is not always equivalent to
function f(){print('hi') }
I was expecting the latter to have the actual meaning of the former!
They are essentially equivalent, the difference is that function (and
variable) declarations are processed before any code is executed.
Function expressions, e.g.
var foo = function() {}
are evaluated when the code is run. In the above, when execution
begins, foo has a value of undefined until the line assigning it the
anonymous function is executed.
>
I am not ready to give an exact reference. I assume you can find
relevant text in ECMA-262 3rd ed. in the description of parser and
tokenizer algorithms.
[explanation snipped]
I'd really appreciate a reference that gives all these not-so-obscure
gotchas in JavaScript language, so that I know where to look the next
time I come across a gotcha like this.
It's not a "gotcha", it is a well-known feature of the language and is
not unique to ECMAScript. Try section 10 Execution Contexts and more
specifically, section 10.1.3 Variable Instantiation.
>
With a description of your actual intent a regular coding solution
could be suggested.
I wanted to understand why SpiderMonkey was not doing what I expected.
I wrote:
// Dummy definition
if (typeof fn == 'undefined') function fn(){}
// Lots of lines like ...
fn(123)
// Real definition of fn
function fn(){
print(arguments )
//...
}
I thought this would allow me to structure the code in the way I wanted
to, and was surprised that the dummy def'n of fn was never used.
Because before the code was executed, the function declaration had
been processed as a component of variable instantiation and fn was a
function object.
To get the effect you expected:
var fn;
// Dummy definition
if (typeof fn == 'undefined') function fn(){}
// Lots of lines like ...
fn(123)
// Real definition of fn
fn = function (){
print(arguments )
//...
}
--
Rob