Reputation: 1637
Which is better to do: export a const
arrow function, like so:
export const foo = () => 'bar'
or export a regular function, like so:
export function baz() {
return 'bar';
}
They compile like so:
exports.baz = baz;
function baz() {
return 'bar';
}
var foo = exports.foo = function foo() {
return 'bar';
};
It looks like using the const/arrow function combination declares an extra variable (foo
), which seems to be an unnecessary extra step over the simple function declaration.
Upvotes: 69
Views: 47284
Reputation: 664630
The differences are minuscule. Both declare a variable.
const
variable is constant also within your module, while a function declaration theoretically could be overwritten from inside the modulethis
const
can get lost among other const
s.Upvotes: 67