What is the benefit of wrapping angular controller/service/factory declarations in an anonymous function [duplicate]

浪子不回头ぞ 提交于 2019-12-11 20:35:47

问题


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.



来源:https://stackoverflow.com/questions/31820566/what-is-the-benefit-of-wrapping-angular-controller-service-factory-declarations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!