8
I’ve been looking at some sample projects in Angularjs and came across several times the following code:
(function () {
angular.module('meuModulo', [
'alguma.dependencia',
'outra.dependencia'
]);
})();
This same code, in the pattern I’m used to, would be:
angular.module('meuModulo', [
'alguma.dependencia',
'outra.dependencia'
]);
That is, the code was placed inside a function
.
Is there a difference between the first and second examples? If so, what are the advantages of using the first approach instead of the second?
The above code is just an example of a module creation in Angularjs.
Modularization in Javascript
– Guilherme Lautert
This is a self-executing encapsulation. I would reply, but I have no knowledge of Angular to affirm an answer.
– Guilherme Lautert
This function has the property of autoload
(funcion(){ ... })()
, that is, it self-executes what is inside.– Ivan Ferrer
Thank you for the @Guilhermelautert link
– Jéf Bueno
When you encapsulate, it means that the elements inside the module will be self-executed... like get, http, etc...
– Ivan Ferrer