0

So, I've run across this idea a couple times. Basically, several sources out there are saying that using closures when defining your app.js (modules, controllers, etc.) is considered best practice.

Could someone break down why exactly this is considered a best practice and what we gain by doing it?

One example is the top answer to this question: Argument 'MainController' is not a function, got undefined in AngularJS

arahansen
  • 97
  • 2
  • 3
  • 12

1 Answers1

0

I'm assuming you're wondering why the author is using the IIFE?

It is to prevent polution of the global namespace. Image you included someone else's angular module as a dependency in your project and in that module a global MainController would've been used. You

a) would not expect it and hence b) it would lead to unexpected behaviour once you start defining your own MainController

To prevent this but at the same time keep the code short and readable, IIFEs and closures are used

elmalto
  • 1,008
  • 1
  • 14
  • 23