I've been writing some CoffeeScript lately, and it's been fantastic. I wish I had started looking into this language sooner. Here's some thoughts about it.
You bring only this snippet of code to battle?
You! What is your keyword?
CoffeeScript! What are your keywords?
-> ! => ! -> !
See old friend, I brought better code than you did.
functions & binding
I'm most excited about how accessible functions have become in CoffeeScript, and how the worry of doing
_.bindAll to keep the current object in
this just vanishes. Even our old friend
$(document).ready loses a few pounds of syntax weight:
$ -> new ItemsRouter() Backbone.history.start()
It's ridiculously neat to me that it just works. If you're doing a loop or something in a
Backbone.View to render many things, you can use the "fat arrow" to make sure
this stays as the current object.
class ItemView extends Backbone.View render: -> @options.items.each(@renderItem) renderItem: (item) => @el.append item.get("title")
No more worrying about
_.bindAll! Oh yeah, using
@ for instance methods (it substitutes
this.) is a great little cupcake of syntax sugar that makes code so much easier to read.
We know the Ruby 1.8 syntax for hashes is dead, but CS takes this one step further. In this example,
Item is a
var html = "";This is such nonsense. Why did we put up with this? In CoffeeScript, you can write:
if Rails.env.development? || Rails.env.test? Barista.configure do |c| c.root = Rails.root.join("app", "assets") c.output_root = Rails.root.join("public", "coffeescripts") c.bare = true c.add_preamble = false end end