Understanding AngularJS $ arguments function

Since entering angular.js, I have seen a generic pattern related to callback arguments. An example is the ui-router documentation :

var myApp = angular.module('myApp', ['ui.router']);

myApp.config(function($stateProvider, $urlRouterProvider) {
    // Do stuff with ui-router providers
}):

But due to the desire I saw, the callback arguments myApp.configmay be different, and it will still behave as expected:

myApp.config(function(OtherProvider) {
    // Do stuff with provider from another service
});

How myApp.configdoes it know the difference? Has a strange magical introspection occurred, or is there some basic JS concept that allows this? For instance. sort of:

myApp.config = function(callback) {
    var providerNames = callback.argumentNames;
    var providers = this.findProviders(providerNames);
};

Maybe I'm too accustomed to python, where such a function will be possible only with the help of some very scary hacks.

+4
source share
2

" ", Angular - . .

( ) arguments . [[ToString]] 1 " ".

, - , .

:

// service names supplied separately
//  - only Order of parameters matters
someModule.controller('MyController',
  ['$scope', 'greeter', function(I_am_a_scope, greeter) { ..

// service names supplied separately
//  - only Order of parameters matters
var MyController = function($scope, GREETER) { ..
MyController.$inject = ['$scope', 'greeter'];

// "scary hack" magic of Angular JS, as service names not specified separately
//  - the Name of the parameters is used as service name
// arguably Too Much Magic (TM), and a source of pain for minification
someModule.controller('MyController', function(greeter, $scope) { ..

Function.prototype.apply, apply() Python. - " ".


1 , , :

f = function (a,b) { return a+b }
f.toString() // e.g. -> "function (a, b) { return a + b; }"

ECMAScript 5 .prototype.toString :

, . FunctionDeclaration. , , , String .

( - , "" , , ES5.)

+1

javascript. Javascript .

, :

 function abc (first, second) {

 } 

, . , , "undefined" , .

Javascript ( JS) "arguments" . ,

- Array Array.
abc(firs,second,third) 

abc

function abc (first, second) {
  var args_total = arguments.length; 
  var no_of_defined_args = Function.length;
} 

Function.length - , ,

+2

All Articles