To understand the point of IFEE, see What is the (function() { } )() construct in JavaScript?. Basically, it's all about avoiding global variables as much as possible.
So what about the use of the namespace
parameter in this context? Well, the value of namespace
is the window
object, because the window
object is passed to the self-executing anonymous function that encapsulates your code as the namespace
parameter. So, namespace.Game
= window.Game
.
One reason for using a namespace
parameter and not just the window
object directly, is because namespace
can be minified, while window
cannot... which means you can save a lot of bytes if you need to reference the window
object a lot.
Another reason for using a namespace
variable, is because you might want your code to be compatible with other environments where the window
object doesn't exist (like NodeJS) at some later time. Or, you might want your code to be rewritten as eg. a RequireJS module. Not hardcoding the window
object anywhere except the anonymous function wrapped around your code makes that a lot easier.
Or maybe you just want to change the scope as some later point in time? Maybe you want to add your Game
to a different object at some later point in time? This too would be a lot easier if you avoid hardcoding the window
object anywhere except the anonymous function wrapped around your code.