I noticed that modules in angular are wrapped by a function with the same definition:
(function(window, angular, undefined){
.....
})(window, window.angular);
even they do not use it. For example angular cookies has that but window is never used. Why is this? I understand they closure principle, to remove variables from the global scope, but this does not explain all of it.
Thanks, Vlad
This is to ensures the meaning of these variables do not get changed by something else.
for example:
window.angular = "333";
(function test() {
var angular = "111"; //changing the meaning of angular
var undefined = 222; //changing undefined to 222
(function iife() {
document.write('Not Passed in: <br/>');
document.write('angular == ' + angular);
document.write('<br/>')
document.write('undefined == ' + undefined);
document.write('<br/>')
document.write('<br/>')
})();
(function iife(angular,undefined) {
document.write('Passed in: <br/>');
document.write('angular == ' + angular);
document.write('<br/>')
document.write('undefined == ' + undefined);
document.write('<br/>')
document.write('<br/>')
})(window.angular, window.undefined);
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With