Encapsulation patterns in AngularJS
Providing public and private design patterns in Angular.js
The concept of Encapsulation is an important design pattern in most object-oriented programming designs. Determining which properties and methods should be accessible is a nice forcing function because it forces the programmer to think about how his or her could will be consumed.
Encapsulation is typically achieved by defining local variables within the function object. These locals can only be referenced by other functions defined on the function object.
What about AngularJS?
I find myself thinking a lot about what should be exposed publicly in my view controllers. As a beginner, it’s easy to just attach your functions and variables to $scope (or this if using controller as syntax) and call it a day. But do you really need your view to have access to all of those functions? In most cases you don’t. Instead, assign these private functions or values to a local variable within your controller function.
I often define a function that initializes the default values within the controller.