Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular modules are wrapped around function(window, angular, undefined) even when they do not need to

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

like image 978
Vlad Avatar asked Aug 09 '26 12:08

Vlad


1 Answers

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);
  
})();
like image 109
Henry Zou Avatar answered Aug 12 '26 03:08

Henry Zou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!