Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is diffence between two closures? [duplicate]

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?

like image 547
engincancan Avatar asked Jun 13 '26 11:06

engincancan


1 Answers

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).

like image 181
dfsq Avatar answered Jun 15 '26 02:06

dfsq



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!