3

I am using my js files like:

(function() {
    'use strict';
    angular
    .module('app.someModule')
    .config(config);
    function config(someDependency){
    //some configuration
    }
    config.$inject=['someDependency'];
})();

However I saw when i using closures some people injects the angular object itself the closure. Something like:

(function(angular){/*whatever logic*/})(angular);

Which one is a better usage or Are there any difference between two usage?

So I dont add global angular variable as always that don't cause any trouble?

engincancan
  • 2,442
  • 2
  • 28
  • 43

1 Answers1

1

The difference between two immediately executed functions (IIFE) is that in the second case you invoke function with one parameter angular. The benefit it can give is slightly improved performance as there is no need for Javascript engine to look up for a variable in global scope, as the angular object is available in local closure scope as a reference passed with function invocation (but of course it still points to the same Angular object, defined in global scope).

dfsq
  • 191,768
  • 25
  • 236
  • 258