What is the benefit of wrapping angular controller

2019-08-22 04:19发布

问题:

This question already has an answer here:

  • What is the purpose of a self executing function in javascript? 16 answers

I've seen a few developers tout 'best practices' when wrapping angular components in anonymous functions. For example:

(function(){
     angular.controller('MyCtrl', [function(){
         // ... controller logic
     }]);
})()

What is the benefit of wrapping angularjs components in anonymous functions, if at all any?

回答1:

This is an immediately invoked function.

(function(){

})()

The above will declare an anonymous function that will be called immediately.