Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CoffeeScript - Write Javascript Easier

Mot
December 04, 2011

CoffeeScript - Write Javascript Easier

A presentation to Riverside JS on coffeescript.

Mot

December 04, 2011
Tweet

More Decks by Mot

Other Decks in Technology

Transcript

  1. Javascript • Great language • 60-70% of the code I

    write now is javascript • Javascript is the future - especially with fast browsers like Chrome • Javascript is on the ugly side - braces and brackets and semicolons lead to parse errors Monday, January 24, 2011
  2. CoffeeScript • Cleans up Javascript • Ruby-esque (designed to read

    like english) and Python-esque (no ends, tab driven) • Compiles to Javascript Monday, January 24, 2011
  3. Assignment favorite_number = 314 is_a_programmer = true # in javascript

    var favorite_number = 314; var is_a_programmer = true; Monday, January 24, 2011
  4. Conditions if (favorite_number == 3.14) is_a_programmer = true else is_a_programmer

    = false alert(is_a_programmer) # Conditions in javascript if (favorite_number == 3.14) { is_a_programmer = true; } else { is_a_programmer = false; } alert(is_a_programmer); Monday, January 24, 2011
  5. Functions area_of_circle = (r) -> 3.14 * (r*r) # in

    javascript area_of_circle = function(r) { return (3.14 * (r*r)); }; Monday, January 24, 2011
  6. Other Stuff • jQuery, Prototype, other frameworks - yes, you

    can mix them in. • Plugins - yes, you can mix them in. Monday, January 24, 2011
  7. Installation • 1) Install node.js • 2) Install npm (node

    package manager) • 3) Install coffeescript Monday, January 24, 2011
  8. Install node.js • mkdir ~/sources • cd ~/sources • wget

    http://nodejs.org/dist/node-v0.2.6.tar.gz • sudo tar xvzf node[press tab] • cd node[press tab] • ./configure • make • sudo make install Monday, January 24, 2011
  9. Install npm • cd ~/sources • git clone http://github.com/isaacs/npm.git •

    cd npm • make • sudo make install Monday, January 24, 2011
  10. Try it out • In terminal type: coffee • You

    will get a command line interface similar to irb. You can write coffee-script directly from there if you wish. Monday, January 24, 2011
  11. Using with Rails/Sinatra/etc • Use the barista gem - https://github.com/Sutto/

    barista • Add it to your gemfile • rails generate barista: install • edit config/initializers/barista_config.rb as necessary • Now your coffeescripts will automatically compile to javascript Monday, January 24, 2011
  12. Good practices • Create a app/javascripts folder • Put all

    your coffeescript in that folder - app/javascripts/ application.coffee • Set config/initializers/barista_config.rb to compile to public/javascripts/compiled • Now all your generated javascript from coffeescript will be in a compiled folder and you can still use normal javascript when necessary under your public/javascripts folder Monday, January 24, 2011
  13. Conclusion • CoffeeScript is a more enjoyable way to write

    javascript • You will make less errors writing javascript with CoffeeScript • You will probably write more powerful and more organized code too Monday, January 24, 2011