By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Can JavaScript use a function before it's defined?

P: n/a
As subject. By 'before' I mean before in the file, not in time.

Here's a file
===
$ cat wierd.js
f()
function f(){print('hi')}
f()
function f(){print('ho')}
f()
===

And when I run SpiderMonkey on it
$ js wierd.js
I get - well, what do you think I get?

OK. So I get
===
$ js wierd.js
ho
ho
ho
===

So it seems that SpiderMonkey is picking up the LAST definition of f()
and executing it three times. Not at all what I expected.

I didn't find any relevant information in
http://developer.mozilla.org/en/docs...ning_functions

I'd like to read about this. Any pointers? In particular, does anyone
know of a JavaScript book that documents this behaviour? And is it
reliably cross-platform?

--
Jonathan
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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!
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.
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.

Thank you for your help in understanding some of the intricacies of
JavaScript.

--
Jonathan
Jun 27 '08 #2

P: n/a
On Jun 21, 9:19 am, Jonathan Fine <jf...@pytex.orgwrote:
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
Jun 27 '08 #3

P: n/a
Jonathan Fine <jf***@pytex.orgwrites:
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!
Well, javascript is kind of a hack - but then, many popular languages
are. In any case, as a rule of thumb, "function name() {}" is
evaluated at the earliest possible time, while "name = function(){}"
is evaluated at the latest possible time.

All of the above is a convenient lie.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.