Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js efficiently export non-anonymous functions (so eslint stops complaining)

Using ESLint with Airbnb Style Guide how do I efficiently write export functions in a module without getting smacked? The following:

exports.istest = function() {
  console.log('Test');
};

throws an eslint 'unexpected unnamed function' warning while:

exports.istest = function istest() {
  console.log('Test');
};

Seems woefully repetitive.

like image 438
Trees4theForest Avatar asked Mar 14 '26 05:03

Trees4theForest


1 Answers

It is repetitive, but here's the thing: when you say function foo() {}, you're declaring a function with the name foo. When you say var foo = function() {} (or exports.foo = ...) you are declaring an anonymous function, then assigning it as the value to the foo variable.

It's a subtle difference, but it can matter. When you name the function your debugger is able to label it correctly for you in the debugging pane, but if you declare an anonymous function you'll just see anonymous function. This can be a pain when debugging, so by putting in a little repetitive effort when it's easy (when you name it) you can potentially save yourself a headache later when debugging.

But then again you can of course debug without function names, so if this isn't a concern to you you can simply disable the rule (either in your .eslintrc or inline with a comment).

like image 162
machineghost Avatar answered Mar 15 '26 18:03

machineghost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!