An AngularJS service
is quite unique in its behavior.
During initialization, it goes through the process of being instantiated with the use of the new
keyword. For example:
function CalcService() {
this.square = function() {
// perform some square calculation
};
}
// then in the controller, directive, or any other component,
// it is initialized implicitly as shown above
new CalcService();
However, what sets it apart is that it is instantiated as a singleton, indicating that there's always just one instance of the object, even if attempts are made to reinitialize it within the registering component (refer to my response on singletons in AngularJS for more information).
I'm not entirely sure about your reference to a "revealing prototype pattern," but in the context of an AngularJS service, the this
simply signifies the implementation of a non-prototypal method on a standard JavaScript object.
Expanding on the previous example, in typical JavaScript, you could execute new CalcService().square()
. It's worth noting that JavaScript doesn't inherently support private methods (although there are techniques for simulating "class" methods that seem confidential.)
var service = new CalcService();
service.square();
There isn't anything technically "private" about that method, much like the methods associated with AngularJS service objects. The closest thing to privacy is that it specifically belongs solely to that particular object by way of utilizing the this
keyword.