CoffeeScript: JavaScript with Class Sugar
It's late 2009, and Jeremy Ashkenas has just given us a gift: CoffeeScript. If you're coming from Ruby or Python, you've probably spent the last year squinting at JavaScript's function keyword and struggling with this context.
CoffeeScript fixes all of that. "It's just JavaScript," but prettier.
The class Keyword
Finally! No more MyClass.prototype.myMethod = function() { ... }. In CoffeeScript, classes feel natural.
class Animal
constructor: (@name) ->
move: (meters) ->
console.log "#{@name} moved #{meters}m."
class Snake extends Animal
move: ->
console.log "Slithering..."
super 5
sam = new Snake "Sammy the Python"
sam.move()
The Fat Arrow =>
How many times have you typed var self = this; inside a setTimeout or an event handler this year? CoffeeScript's fat arrow => automatically binds the function to the current context.
class Counter
constructor: ->
@count = 0
# The fat arrow binds 'this' to the Counter instance
setInterval (=>
@count++
console.log @count
), 1000
Significant Whitespace
No more curly braces. No more semicolons. CoffeeScript uses indentation to define blocks. It makes your code look clean, readable, and-dare I say-elegant.
In 2009, CoffeeScript is proving that we don't have to settle for the "Good Parts" of JavaScript. We can make the whole language better by transpiling it.
The future is here, and it smells like freshly brewed coffee.