These are good questions. Let’s see what answers we can find.
The main reason why an anonymous function isn’t used, is so that the variables for window/document/undefined can be passed in to the function.
Using window.Modernizr provides a clear intent that you want it to be a global variable. Using var doesn’t make that as clearly understood.
We don’t know if those values have been redefined as something else before our code starts to run, so passing them in allows all of the code within tyhe function to use them from a more trusted local source.
The this keyword refers to the window object, and this.document is for the document object. Notice how a third argument is not provided, yet one is defined for the function called undefined? Not passing a variable for that third argument results in it being automatically assigned to undefined, even if someone else has redefined undefined previously.
To overcome such issues, undefined is declared as a local variable to the function, so that a more trusted version of it can be used.
Commonly scripts check for the existence of things by checking of something is undefined. Since it’s possible for the undefined variable can be reassigned to be something, you don’t want to rely on the global undefined variable which might be mucked up by someone else, which is why the function created its own undefined variable so that they can guarantee that the correct things is being checked for.
Why that works is that all functions retain with them a copy of their parents environment. So if you had this:
function handler(index) {
var value = 'something or other';
return function () {
...
};
}
That inner function that’s returned will retain knowledge about both the index and the value, even though those variables aren’t declared within that inner function.