Named functions versus anonymous functions

Sometimes I see examples like this and I wonder what it is. I mean this.methodA = function methodA () {} why is this?

The only thing I can imagine is using it without this if you have a scoping problem. Anyone have an idea?

function MyModule() {

  this.myMethod = function myMethod() {
    alert( 'my method' );
  };

  this.myOtherMethod = function myOtherMethod() {
    alert( 'my other method' );
  };

}
// Versus:
function MyModule() {

  this.myMethod = function () {
    alert( 'my method' );
  };

  this.myOtherMethod = function () {
    alert( 'my other method' );
  };

}

      

+3


source to share


1 answer


The main advantage of named function expressions over anonymous ones is that their name will show up in debuggers (stacktraces, etc.), making it much easier to figure out what happens when something goes wrong.



A named function can also be called using its name from its own scope. This is useful for creating recursive functions.

+3


source







All Articles