CoffeeScript

So, it’s my turn to do something to get familiar with the new kid on the block – CoffeeScript .

I took a short 160 LOC JS file supporting my WiceGrid Rails plugin – https://gist.github.com/1067159 and rewrote it in CoffeeScript – https://gist.github.com/1067160 .

The JS generated out of the CoffeeScript code is here – https://gist.github.com/1067162

What I really liked is the syntax for javascript object literals in function calls:

1
2
3
4
5
6
7
  jQuery.ajax
    url: base_path_to_query_controller,
    async: true,
    data: request_path + '&query_name=' + encodeURIComponent(query_name),
    dataType: 'script',
    type: 'POST',
    success: () -> $('#' + field_id).val('')

and this (look at “.join”):

1
2
3
  jQuery.map grid_state, (pair) ->
    encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1])
  .join '&'

This not all all that CoffeeScript has in stock, but it seems than in order to use things like list comprehension you have to write code directly in CoffeeScript.

This new syntax could become a huge improvement for those writing big systems in javascript (think Node.js or RIAs like ExtJS or SproutCore), but with standard web applications, even modern and dynamic, the javascript that you write usually consists of short chunks and functions here and there, thanks to powerful frameworks like jQuery. And I just don’t see the point of adding one more layer for this kind of supporting code. I personally don’t mind writing it all in javascript, though I would love to use CoffeeScript merely for its beauty.