2

I know this is close to duplicating

Are named functions or anonymous functions preferred in JavaScript?

or the even more popular

var functionName = function() {} vs function functionName() {}

(to name a few) questions, but after reading neither really satisfactorily answers my question. I think I understand the difference between function expressions, named function expressions, and function declarations (well, almost understand...).

When I create directives, my usual style is something like this:

angular.directive('myDirective', function() {
   return {
       ...
       controller: function($scope){
       }
   };
});

Looking at a lot of the angular.js source, it seems they prefer

angular.directive('myDirective', function() {
   return {
       ...
       controller: function myDirectiveCtrl($scope){
       }
   };
});

I would think this might be useful if you are going to use myDirectiveCtrl inside of itself, but this isn't the case for most angular directives.

Is this just a style choice or is there an advantage or reason for using named function expressions when declaring controllers, link functions, etc.?

Community
  • 1
  • 1
Joe Enzminger
  • 10,882
  • 2
  • 47
  • 75
  • 6
    Named functions are easier to debug, stack trace will show something useful instead of "anonymous function" call. – Klaster_1 Feb 09 '15 at 03:25

0 Answers0