First of all, a quick note about terminology: you have no function declaration. This function declaration:
function fooBar() { }
Creates a function called fooBar , accessible through the variable fooBar . This is an assignment that includes a named function expression:
var foo = function fooBar() { };
The name of the function is still fooBar , but the function is bound only to the variable fooBar inside the function itself, and not outside. The fact that it makes an accessible function within its scope without having to refer to a variable in the outer scope means that there are two reasons to name it:
For the ability to refer to a function within itself, regardless of the code in the external function!
This can return what the external function wants:
function fooBar() { return fooBar.toString(); } var baz = fooBar; fooBar = 5; baz();
This is always agreed upon:
var fooBar = function fooBar() { return fooBar.toString(); }; var baz = fooBar; fooBar = 5; baz();
And yes, for a more detailed stack trace:
function trace(func) { try { func(); } catch (error) { console.log(error.stack); } } trace(function () { throw new Error("Bad thing"); }); trace(function descriptiveName() { throw new Error("Bad thing"); });
(Node.js pictured here.) Notice the descriptiveName at the top of the second stack trace. This is especially convenient when you have several complex systems of asynchronous callbacks, events, methods for transferred objects, etc.
source share