|
|
|
|
|
by jashkenas
5188 days ago
|
|
Sure -- the syntax for arrow functions has been floating around for ages, in Haskell, C#, and probably most relevant for me, the "stabby lambdas" in Ruby 1.9. One of the first ideas with CoffeeScript was to simplify function syntax, mostly because JavaScript's anonymous functions are so useful and pervasive that you end up with a lot of this: _.each(toRemove, function(key){ delete data[key]; });
... where the "word" function there doesn't really tell you anything meaningful about what you're trying to accomplish.Arrows were appealing for a shorthand function syntax because of their visual representation of what happens in a (pure) function. Input goes in one side, output comes out the other. The input determines, or points to, the output. Since we use parentheses symmetrically to group parameters to a function, both when you define a function and when you call one, we arrive at this: (input) -> output
... or with our initial example: _.each toRemove, (key) -> delete data[key]
That was the thinking. CoffeeScript actually originally used => for all functions, but the fat arrow distinction was introduced when we introduced bound (lexical "this") functions as an alternative to normal (dynamic "this") functions. |
|