When Angular creates your controller, it will utilize the new
keyword on the function you provided. This results in the construction of a new object using the constructor you passed in. Returning objects from your constructor function will prompt Angular to use that specific instance of your newly created object, similar to how JavaScript constructors are typically used.
There are some nuances to keep in mind during the construction process (refer to this SO answer for more details):
- If the returned object is the same as
this
, it can be omitted since this
will be utilized by default.
- In cases where a primitive type or
null
is returned (essentially anything that is not an Object
), this
will also be used.
- Returning an instance will result in the reference to that specific instance being returned.
It should be noted that stating this
will be utilized in points 1 & 2 is an oversimplification. For more specific information, please refer to this detailed explanation regarding construction.