On May 24, 3:24 pm, Michael <mich...@michaelminella.com> wrote:
 However, in all of my scenarios, I've declared
> functions like this:
>
> var myFunction = function myFunction() {alert('hi');}
>
> and the calls to myFunction work just fine.  My question is...why does
> my way work?  According to the Prototype documentation, the local
> variable myFunction should be thrown away after the eval.  Any insight
> anyone can provide would be appreciated.  Thanks in advance!

According to Flanagan's book (section 8.1.2) the optional function-
name in a function literal is not assigned to a variable, but
apparently lives in a special namespace of its own that allows the
function to refer to itself. So unless the function is recursive, the
above is identical to

var myFunction = function() {alert('hi');}

and more or less identical to

function myFunction() {alert('hi')}
(which is a function *statement* not a function *literal*, and so has
different syntax, with the name being required.)

Crockford, in "Javascript: The Good Parts" describes function
*statements* as  one of the Bad Parts (appendix B), and recommends
avoiding them, in favour of the

var f = function() {}

form.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to