AngularJS: weird design or brilliant idea?
Posted on September 22, 2013
Ahem, I have always thought the difference between parameters and arguments are there for a reason and I can use an arbitrary parameter name for my function. It’s probably called a local scope. Well, not AngularJS $scope
:
window.SomeCtrl = function($scope) { ... } //works
window.SomeCtrl = function($myScope) { ... } //doesn't work
The reason why the latter doesn’t work is because AngularJs pretends to be source code parser:
if (typeof fn == 'function') {
if (!($inject = fn.$inject)) {
$inject = [];
fnText = fn.toString().replace(STRIP_COMMENTS, '');
argDecl = fnText.match(FN_ARGS);
forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){
arg.replace(FN_ARG, function(all, underscore, name){
$inject.push(name);
});
});
fn.$inject = $inject;
}
}
Not that I’m against reflection, but this looks like a super-reflection. Next step in this direction would be to modify source code on the fly and re-eval it.