CoffeeScript: The Cool Parts

by woody2shoes on May 13, 2011

CoffeeScript offers more than nice syntax for setting and managing data and functions. It also offers Classes, Inheritance, access to a ‘super’ method, Ruby-style string interpolation, easy variable and function bindings, and chained comparisons.

Here’s the code I showed in the video:

class Vehicle
  constructor: (@name) ->

  move: (miles) ->
    console.log @name + " drove " + miles + " miles."

class VWBug extends Vehicle
  move: ->
    console.log "Cruisin'..."
    super 100

class Truck extends Vehicle
  move: ->
    console.log "Haulin'..."
    super 50

mater = new Truck "Mater"
herbie = new VWBug "Herbie"

mater.move()
herbie.move()
a1c = "7.6"

healthy = 7.0 > a1c > 5.0

console.log "A1C within healthy range: #{a1c}"
Account = (customer, cart) ->
  @customer = customer
  @cart = cart

  $('.shopping_cart').click (event) =>
    @customer.purchase @cart

If you’re new to CoffeeScript, make sure you check out the CoffeeScript Cookbook and CoffeeScript Basics.

Download 150.3 MB
Download (iPod & iPhone) 87.1 MB

  • http://twitter.com/icidAsset Steven Vandevelde

     Great video tutorial! And I think, it’s “hi()” instead of just “hi” to execute the function ;) P.S. There’s some html in the last code example.

    • http://teachmetocode.com Charles Max Wood

      Oh, duh. I left it in because I figured someone would correct it. 

  • http://icidasset.heroku.com/ Steven Vandevelde

     Great video tutorial! And I think, it’s “hi()” instead of just “hi” to execute the function ;) P.S. There’s some html in the last code example.

    • http://teachmetocode.com Charles Max Wood

      Oh, duh. I left it in because I figured someone would correct it. 

  • Felix Rotthowe

     Without knowing anything about coffee-script (yet): Could it be that the code in the back ticks is executed, so all you’re doing is define the function hi, but not call it? Later, when you execute with “hi” selected, you just execute the selection which translates (of course) to “hi” in javascript.

  • Pingback: [Friday Links] The Upnorth Edition | Web Communications - Wayne State University Blogs

  • Adriano Tadao

     Great video! Congratulations … thanks!

  • mull

    log() instead of console.log() if I remember correctly. console.log() will spew out errors if that function does note exists (IE6 methinks).

Previous post:

Next post: