
Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [tlug] Variable scope in Javascript driving me nuts
> ... I don't even know why it works at all: You define the function
> "checkResult" after the function call where you use it..
> ...
>
> You might be wondering why I'm declaring the handler-function
> "inline" here. That's actually typical for JavaScript and called an
> "anonymous function". You'll find it in nearly all examples, like
> here: http://api.jquery.com/jQuery.post/ :)
Either way is fine, and it is mostly a style decision. Anonymous
functions are slightly more compact, which is why you see them in the
examples - javascript is a bit like Perl, people like to show off how
few bytes their script uses.
Personally (if my function has more than 1-2 statements) I prefer named
functions, because I can then put a comment block above them. (Comment
blocks can be stripped off automatically for a production version.)
(BTW, Dave's original code declaring a function inside another function,
but for use as a global function, makes me nervous. I'd prefer a nice
explicit global named function, which I know *without testing* will work
fine in all browsers.)
Darren
P.S. I may have my facts slightly wrong above, but this thread goes into
some more detail of why you'd choose one style over the other:
http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname
Including a link to a blog claiming that there is no performance
difference between the two:
http://blog.firsov.net/2010/01/js-local-functions-anonymous-vs-named.html
And this page is "Everything You Ever Wanted To Know But Were Afraid To
Ask":
http://yura.thinkweb2.com/named-function-expressions/
--
Darren Cook, Software Researcher/Developer
http://dcook.org/gobet/ (Shodan Go Bet - who will win?)
http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)
Home |
Main Index |
Thread Index