Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting jQuery to CoffeeScript?

CoffeeScript seems like a promising new technology, and I'll definitely give it a try in my next project.

It seems like jQuery can be used easily with CoffeeScript as is - however will it make any sense to port jQuery completely to CoffeeScript (in the same manner underscore was ported), or will it be just a waste of time?

  • What advantages would one enjoy from such a port?
  • Apart from possible compatibility issues - could this also cause problems (i.e. tricks possible used in jQuery's pure js which are not possible in CoffeeScript?)
like image 263
Udi Avatar asked Jun 13 '11 06:06

Udi


2 Answers

Underscore was only ported as a demonstration of CoffeeScript's power and succinctness. Plus, the annotated CoffeeScript source provides clearer documentation for those conversant in CoffeeScript. But the official version is still maintained in pure JS, not compiled from CoffeeScript.

I think that, similarly, it would make sense to port jQuery to CoffeeScript only so that curious coders could enjoy annotated source code that would make the inner workings of the library clearer. jQuery benefits from the attention of enough strong JavaScript programmers that I doubt any improvements would occur from porting it to CoffeeScript.

I'd love to see someone do it, though, assuming they made great-looking code with equivalent behavior. The one thing I think the adapter would have to carefully consider is how to port named functions (function foo()) to unnamed functions (foo = function()), since CoffeeScript only supports the latter (due to cross-browser inconsistencies in some cases) and the two have different scoping behaviors. JavaScript's == shouldn't be an issue, since the only cases I can find it being used in the jQuery source are x != null, which can be done in CoffeeScript with x?.

like image 168
Trevor Burnham Avatar answered Oct 20 '22 16:10

Trevor Burnham


Correct that Coffeescript is a Javascript generator. Also correct that source code can be clearer. There are absolutely no tricks that can be done in js that can't be done in Coffeescript -- you can drop into pure js if you need to -- and I'm not sure I've ever heard of anyone needing to do this. If you want to drop into Javascript, simply enclose the expression in backticks:

`var foo = 42`

I would disagree that the code is "bigger". I would assert that most extra bytes in the code are ones you should have been inserting anyhow but didn't -- like adding a namespace wrapper around your own code. In some cases, Coffeescript has clever helpers that factor out common usage patterns so they aren't coded one-off. You can figure out which these are by reading the generated js a bit.

In terms of advantages, I guess it depends a lot on how you think about code. I'm a fan of encapsulating a slab of functionality in a class and hate the Javascript prototype-based object definition. Coffeescript is more Rubyish or Pythonesque in terms of declaring classes and also in terms of inheritance.

My advice to you is: 1) Get Trevor Burnham's Coffeescript book and get a taste of how Coffeescript works in practice; 2) Cobble together a few simple examples that address your kind of use case; 3) See how you like it after you get comfortable with the syntax.

Note: The Coffeescript Google Group is a great resource and people answer questions quickly there -- it's likely someone has encountered whatever's puzzling you before and will share an answer. Trevor lists a complete set of resources in his book. (No, I don't get paid to give him props on the book :)

like image 27
Steve Ross Avatar answered Oct 20 '22 16:10

Steve Ross