sneakyimp wrote:I guess I was wondering why the anonymous function.
Oh, that would be for scoping purposes: all vars declared within the anonymous function are local to the function; the alternative being to clutter your global namespace with all sorts of dross (variables and functions alike).
The bonus of using an anonymous function for the job is that it doesn't have a name - by definition - so won't touch the namespace; and as soon as it has completed executing it is inaccessible. So not only do its locals get garbage collected, but the function itself is a candidate for removal. All that would remain afterwards are things like event handlers that have been bound to things outside the function.
Libraries will need to leave something behind, of course - the library's functionality. The practice is to declare a single object to contain the entire library, make it a global property, and put the public interface in there.
Incidentally, in JavaScript, the global environment is itself an object - namely, "window": hence the "identifier protection" trick.
I think JavaScript - although it has been cursed with an evilly misleading syntax (among other issues) - is actually quite a clever language. As an OO language, "prototypical" inheritance model is arguably superior to other languages' "classical" inheritance in that it's relatively easy to implement the latter in the former (witness how pretty much every JavaScript framework does just that), but somewhat harder to do the opposite. A shame really, because the latter's dominance is one of the things that makes JavaScript look "weird".
And that's even before recognising it as a functional language (meaning functions are first-class citizens), and that "methods" are actually variables that just happen to contain functions ([font=monospace]foo.bar()[/font] is interpreted as "get the object in [font=monospace]foo.bar[/font] and - assuming said object is a function - call it".)