Porting jQuery to CoffeeScript?
CoffeeScript seems like a promising new tec开发者_如何学JAVAhnology, 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?)
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?
.
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 :)
Take into account that coffescript is just another way to code javascript, so everything you can do in javascript can be done in coffescript and viceversa...
Said that is obvious that the only advantage of coffescript is a clearer source code, what makes it easier to read and to code. The disadvantage is that the javascript that coffescript generates will most provably be much bigger that the current.. what for a library is quite a big issue...
So it would be interesting to have one coffescript version as a documentation document for people trying to understand how jQuery works but it wouldn't make any different to use coffescript in the main code
I know I'm over two years late but I ported jQuery to CoffeeScript. The code is intended to be viewed for comparison purposes (jQuery's .js code against .coffee code) since I didn't focus on adding comments.
View http://github.com/sharikul/jQuery-Coffee
精彩评论