4 ms·
> The syntax for functions that take parameters after > an anonymous function isn't obvious. Eh? A function is an argument list (), an arrow ->, and an in
by jashkenas 13y ago
> The syntax for functions that take parameters after
> an anonymous function isn't obvious.
Eh? A function is an argument list (), an arrow ->, and an indented body of code. Putting 'em together:
request(url, (response) ->
handle response
, (error) ->
handle error
)
- lowboy 13y agoTo be fair, having the comma on the same indentation as the enclosing function call doesn't follow the standard multiline list formatting in comma-last JS.
- jashkenas 13y agoTo be fair, that style works just fine as well, if you prefer it. request url, ((response) -> handle response), (error) -> handle error
- lowboy 13y agoBut then you get into do-you-don't-you with parentheses, which I find is a sticking point for some people when they try CS. I prefer your first example, personally.
- wmil 13y agoThe following are all equivalent: request(url, (response) -> handle response , (error) -> handle error ) request(url, (response) -> handle response (error) -> handle error ) request(url (response) -> handle response (error) -> handle error ) request url, \ (response) -> handle response , (error) -> handle error request url, (response) -> handle response , (error) -> handle error It's very reasonable for a JS developer to be confused when seeing those.
- unquietcode 13y agoIf you can't see what all of these have in common, then you probably shouldn't call yourself a software developer. It's just syntax, people.
- lucisferre 13y agoI'll be the first to admit, I still find it a bit of an awkward syntax, and it was one of the parts of the syntax I kept bumping up against for the longest time. Amazingly I'm finally comfortable with it, but there is a kind of cognitive friction around that one. Personally, I blame whitespace significance. It's hard for whitespace to be both significant to syntax and formatting without impacting one or the other negatively. The principle argument against whitespace significant languages is separation of concerns. Punctuation only for syntax and whitespace only for formatting. That said the benefits of CoffeeScript still outweigh the tradeoffs for me.