One of the frustrations I face with AngularJS is the variety of styles that are used in examples. I have seen 4 different ways to declare and add AngularJS elements to a module (including different ways to declare the module). In another question I asked about using a global variable vs. directly declaring the module. Now I would like to know what are the advantages/disadvantages to using various styles to add elements to the module. (I will not include the examples that declare a global variable for the module, as that style has been discounted as a 'bad practice')
Assume the module is declared in file myMod.js as:
angular.module('myMod', [
// Angular modules
'ui.router'
,'ui.bootstrap'
,'ngResource'
,'ngStorage'
]);
Now I want to add a controller to the module, I have seen:
(assume each example represents the entire contents of a file named: myController.js)
option A:
angular.module('myMod')
.controller.('thisController',['dependencyA','dependencyB',
function(dependencyA, dependencyB){
....stuff to do
}]);
option B: just adding 'use strict'
'use strict';
angular.module('myMod')
.controller.('thisController',['dependencyA','dependencyB',
function(dependencyA, dependencyB){
....stuff to do
}]);
options C: wrapping the whole thing in an anonymous function as:
(function(){
'use strict';
angular.module('myMod')
.controller.('thisController',['dependencyA','dependencyB',
function(dependencyA, dependencyB){
....stuff to do
}]);
option D: Declaring a named function rather than an anonymous function in the controller declaration as:
(function(){
'use strict'
angular
.module('myMod')
.controller('thisController',thisController);
thisController.$inject = ['dependencyA','dependencyB'];
function thisController(dependencyA,dependencyB){
...stuff to do
}
Similar diverse examples exist for adding factories, services, directives, etc. into a module. As best I can tell these are different styles for accomplishing the same goal. Which techniques represent 'best practices' and 'why'?
(I am not wanting this to turn into an 'opinion piece' please offer 'objective' reasons to select a style.)