Generally speaking, IIFE functions are used in this way to safeguard that variables fall within the global scope.
An EEI is a self-executing function, and generates a new scope within itself.
Pass as function parameter window
and document
does not have much use, but has a function that is to facilitate/optimize the compression of the code.
If there are many times in the code within that function the variables window
or document
then a Javascript compressor like Uglify will shorten and give a letter to these variables, thus minimizing the file size.
The case of Undefined is more interesting.
Sometimes the error of over-writing this variable is made. That is it is possible (in some browsers) to do 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 call/use 2 causes the third party to receive the real value of undefined
. So, for browsers that allow you to rewrite undefined
this trick re-establishes its original value.
The other advantage and reason why Undefined is there is the same as above: so code compressors can transform n times the 9-character word "Undefined" in a single letter.
Actually the information about the IIFE is outside the scope of my question, but your information about the "compressing code" part was the answer I needed to know. + 1
– Guilherme Nascimento