Also addition to the @Daniel's answer, passing this
to the function is a common pattern to have a reference to the global object, for example:
(function(window){
})(this);
In browser scripting the global object has a property named window
which refers to the global object itself, in other environments there is no window
property.
Also, another thing that can be done is to specify an argument named undefined
, because the undefined
property is not described on the ECMAScript 3rd. Edition Standard (there is no guarantee that exist or not), and in some implementations the property is mutable, for example:
(function(window, undefined){
})(this);
In the above example, we have to local identifiers (which are a bit faster to resolve), window
and undefined
, only the first one is passed (this
, which always refers to the global object in Global code (code that is outside of any function)), and the second, will contain the primitive undefined
value, because we are not passing any value to it.
That pattern is used by some libraries like jQuery.