Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    widget =
      attach: ->
        @el.bind 'click', @handler
      handler: (event) =>
        doStuffWithThis()
wrong.

Handler is bound to whatever scope widget is in. http://jashkenas.github.com/coffee-script/#try:widget%20%3D%...

See how confusing fat arrow is?



It's not confusing at all. It's the same thing your sample code does. In all three of your examples, the handler is bound to whatever scope widget is in. The code is functionally identical to your examples.

Edit: Okay, I see what you're doing; attach is supposed to be invoked with a bound scope, which does change the binding. So yeah, my code won't work in your example. I'd argue that that's more of a lack-of-context problem in the example though, since it's dependent on knowledge of the usage of the attach() function to know that that binding behavior isn't desired. Given the context of the example, I contend my solution was acceptable. :)

Fat arrow there is still functionally equivalent to $.proxy.


I'll save the fat arrow discussion for my next post, but the only point here is that it encourages nesting.


The arguments you use against CoffeeScript are actually the reasons why I love CoffeeScript and the fat arrow.

To see what scope the fat arrow is bound to, all you need to do is scan up the parent scope in the file and look for a thin arrow (or a class declaration).

Your "whitespace problem" is my solution.


That's a good point, and I've actually adjusted my color scheme to make fat and thin arrows different brightness, the fat brighter to call it out more.


For classes, I'm still unclear as to why you might even use the thin arrow, lest you're providing a mixin. Thoughts?


The constructor doesn't need one, and there is a small performance hit for binding =>.

Mixins... Can you show me an example?

Times when you need a reference to a canonical function object, like with $.proxy and $.unbind. (CoffeeScript could implement => to set the original function as a property of the bound function)

Added a question on github: https://github.com/jashkenas/coffee-script/issues/1934




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: