In AngularJS these two controller declarations are equivalent:
function BlahCtrl($scope, $http) { ... }
function BlahCtrl($http, $scope) { ... }
Both $http and $scope will be the correct variables no matter what order they are in. i.e. the variable named $http will always be passed an instance of the $http service.
How does Angular know which objects to pass in and in what order? I thought this kind of reflection was not possible with javascript.
If you call
toStringon a function, you get the js declaration of that function:then you can parse the string for the order of arguments.
Some investigation into the angular source code confirms this:
They stringify the function, then extract the arguments with a regular expression and store them in an array.
jsFiddle showing how this all works.