One of the reasons for using the second form with an argument is that it isolates your code from other js code loaded later on the page (for example, other libraries or structure code), which can override the variable passed as an argument.
One common example: if the code in your self-tuning anonymous function depends on jQuery and wants to use the $ variable.
Other js frames also define the $ variable. If you code your function as:
(function($){
Then you can safely use $ for jQuery, even if you load another library that defines $.
I saw that it was used defensively with people passing in all the "global" variables that they needed inside their block, such as window, document, jQuery / $, etc.
Better safe than sorry, especially if you use many third-party widgets and plugins.
Update:
As others have pointed out, the circle of parentheses around a function is a closure. They are not strictly necessary many times when this template is used (@Rob W gives a good example where they are needed), but they say that you have a very long function body ... the outer parentheses tell others reading the code which the function is, probably executed independently.
The best explanation I've seen in this example is in the video of Paul of Ireland: http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/ , starting at about 1:30
This SO question also contains some informative answers: How do you explain this structure in JavaScript?
source share