Infact, strict mode throws an exception if you forget to use var for any variable declaration. But it works even without using external closure.
Using this template is much more to protect yourself from the outside world of javascript. For example, some other scripts overwrite window.undefined or any other variable, you can capture the value in this closure to access it from the inside.
for instance
(function _myApp( win, doc, undef ) {
In addition, when declaring variables with var or creating function declarations, they are always stored in the current activation object, respectively, in the Lexical environment record. This means that without using a Function context, you can easily rewrite methods and variables from some other point, because they will all be stored in the current Context (which would be global)
So:
(function _myApp( win, doc, undef ) { var myVar = 42; function myFunc() { } }( this, this.document )); (function _myModule( win, doc, undef ) { var myVar = 42; function myFunc() { } }( this, this.document ));
This works because of closure and context, but if you use the same code without function context, we will obviously rewrite our myVar and myFunc . This can happen everywhere, in the same file or in another loaded script.
jAndy source share