4 ms·
My only major complaint with Coffeescript is that it doesn't have a construct for handling the context-sensitive 'this' keyword. I still have to write self
by statictype 15y ago
My only major complaint with Coffeescript is that it doesn't have a construct for handling the context-sensitive 'this' keyword. I still have to write
self= @
at the top of functions. The fat arrow for binding 'this' certainly helps but when you have to be really careful when writing code that nests several functions.
Also, would be nice if the for-loop had a way to automatically wrap the block in a function without having to write
for(var x in lst) do (x) ->
whatever(x)
- JoelMcCracken 15y agoNot sure that I understand you. Do you mean that you find it odd to use =>, and thus resort to self=@ for your callbacks? Also, sometimes I do $.each lst, (i, item) -> whatever x because I always use jQuery in my projects.
- d4n3 15y agoI often have to resort to self = @ when using jQuery in classes. Because of how jQuery binds the queried element to this, and the fat arrow overrides this, there doesn't seem to be another way to access both your class and the jQuery element in the same function. for instance: class ButtonLogger constructor: -> @clickedButtons = [] $(document).ready => @init() init: -> self = @ $('button').click -> $button = $(@) self.clickedButtons.push $button.attr 'id'
- jashkenas 15y agoTry `event.currentTarget`, and `event.target` instead. They're much preferable to having a `self` (representing some random DOM element), and a `this` within a function. $('button').click (e) => @clickedButtons.push $(e.target).attr 'id'
- M1573RMU74710N 15y agoI'm not sure I understand your first complaint. The fat arrow is a great helper and saves a lot of work in various situations....obviously it doesn't cover all situations...but in the situations it doen't cover I'm not sure what I'd expect the language to do. Could you give an example of what you imagine the feature to be? I'm just not sure if something as simple and easy as that = @ needs improvement. Anything beyond what that and => covers, to me seems like something a library should handle...and CS makes that very easy. For you second one, what about do ( -> whatever x ) for x in foobar One of the things I like about CS is that it is very conservative about not adding helpers for every little thing you can think of. It chooses a few very powerful and fundamental building blocks, and let's you use those to build up yourself. I can respect some people wanting more though.
- statictype 15y agoI did not know about the do () for x in foobar syntax. Thanks.
- M1573RMU74710N 15y agoSorry, my comment should read: """ ( do (x) -> whatever x ) for x in foobar or foo = (x) -> whatever x foo x for x in foobar or for x in foobar do (x) -> whatever x I find all of these pretty readable and easy. The former in simple cases like "whatever x", and the latter two when you are doing more than just "whatever x". I think it's important to distinguish cases like this and that the syntax is clean enough without adding some helper that makes what's going on implicit """ In the section where it says: """ do ( -> whatever x ) for x in foobar """