IIFE functions are generally used in this way to safeguard which variables fall into the global scope.
An IIFE is a self-executing function, and it generates a new scope within itself.
Passing as a parameter of the function window
and document
is not very useful, but has a function that is to facilitate / optimize in code compression.
If there are many times in the code within this function the variables window
or document
then a JavaScript compressor like Uglify will shorten and give a letter to those variables, thus minimizing the file size.
The case of undefined is more interesting.
Sometimes you make the mistake of overwriting this variable. In other words, it is possible (in some browsers) to undefined = 20
. In this case all lines of code that use undefined
to check if a variable is set will give unexpected values. Creating a function with 3 parameters but only calling / using 2 causes the third party to receive the actual value of undefined
. Thus, for browsers that allow rewriting undefined
this trick re-establishes its original value.
The other advantage and reason for undefined there is the same as above: for code compressors to be able to transform n times the 9-character word "undefined" into a single letter. >