Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose using a named function when assigning to a variable in JavaScript?

Tags:

javascript

I recently came across this pattern in the source for https://github.com/yeoman/generator-webapp:

AppGenerator.prototype.packageJSON = function packageJSON() {
  this.template('_package.json', 'package.json');
};

What's the purpose of giving the function the name "packageJSON" when you're going to assign it to a variable or object property anyways? I've always used anonymous functions in similar cases.

like image 489
FriendOfFuture Avatar asked Apr 12 '13 20:04

FriendOfFuture


1 Answers

For debugging purposes. If you use a named function you can see that name in the call stack trace in your favorite dev tools. Otherwise you'd see anonymous function.

like image 60
elclanrs Avatar answered Nov 14 '22 16:11

elclanrs