airbnb / javascript

JavaScript Style Guide
MIT License
145.15k stars 26.51k forks source link

Consider allowing anonymous function expressions #450

Open tomek-he-him opened 9 years ago

tomek-he-him commented 9 years ago

At the moment you forbid unnamed function expressions like function() {}. That’s a good thing.

But when using the new function bind syntax with trine-style libraries, you can’t use arrow functions because they inherit this from the outer scope.

So a good old anonymous function expression:

[my, collection]::map(function() {
  return !!this;
});

…often feels more natural than inventing obscure names:

[my, collection]::map(function justCastToBoolean() {
  return !!this;
});
hshoff commented 9 years ago

Makes sense to me :+1:

ljharb commented 9 years ago

If we used trine, or if function bind syntax was a more advanced proposal than stage zero (which indicates that it is in flux, and shouldn't be relied upon just yet), then I'd agree. However, absent either of those two, it seems like this is a change that you should feel free to fork the guide and make, but not one we should make in the guide directly?

goatslacker commented 9 years ago

This rule should be added regardless. We currently allow anonymous function expressions if you need this scope to be bound or called.

ljharb commented 8 years ago

This rule can't differentiate currently between a function expression that's passed as a callback, and a normal function expression.

If we remove this rule, then we'd be allowing all anonymous functions. Do we want to do that, for the few cases where we have to invent an obscure name? (I ask this having certainly added more than a few sorryButTheThisKeywordIsNeededHere and pleaseDontUseTheThisKeywordInSpecs function names into the airbnb codebase while enabling this rule)

goatslacker commented 8 years ago

Maybe we should add this to eslint then?

ljharb commented 8 years ago

That seems like a good idea.