3

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.

Trees4theForest
  • 816
  • 1
  • 10
  • 34
  • Related: [Why does the Airbnb style guide say that relying on function name inference is discouraged?](https://stackoverflow.com/questions/37288950/why-does-the-airbnb-style-guide-say-that-relying-on-function-name-inference-is-d) – jfriend00 Aug 14 '17 at 05:01

1 Answers1

5

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).

machineghost
  • 28,573
  • 26
  • 128
  • 197