Mailing List Archive
tlug.jp Mailing List tlug archive tlug Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]Re: [tlug] Variable scope in Javascript driving me nuts
- Date: Fri, 27 Aug 2010 08:53:11 +0900
- From: Darren Cook <darren@example.com>
- Subject: Re: [tlug] Variable scope in Javascript driving me nuts
- References: <4C769657.5040705@example.com> <0A1A4EBC-F40B-44A9-AB82-EEC7E6117BFD@example.com>
- User-agent: Thunderbird 2.0.0.24 (X11/20100317)
> ... 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)
- References:
- [tlug] Variable scope in Javascript driving me nuts
- From: Dave M G
- Re: [tlug] Variable scope in Javascript driving me nuts
- From: Philipp Wollermann
Home | Main Index | Thread Index
- Prev by Date: Re: [tlug] Variable scope in Javascript driving me nuts
- Next by Date: [tlug] .bashrc not being source correctly
- Previous by thread: Re: [tlug] Variable scope in Javascript driving me nuts
- Next by thread: Re: [tlug] Variable scope in Javascript driving me nuts [SOLVED]
- Index(es):
Home Page Mailing List Linux and Japan TLUG Members Links