CoffeeScript
JavaScript in a simple way


    Open Tech Talk – 22 Jan 2011
         @HackerSpacePP
           @lchanmann
CoffeeScript
●
    One-to-one with JavaScript
●
    Better functional syntax
●
    Compiles to the good parts
Why CoffeeScript?
●
    Less code
●
    Readability
●
    Easy to understand, and maintain
●
    But … you should know how JavaScript's
    concept work
JavaScript
CoffeeScript
JavaScript
Coffee
JavaScript
CoffeeScript
Functions



square = (x) -> x * x

area = (x, y) -> x * y

cube = (x) -> x * square x
Objects


                        kids = {
                           brother: {
kids =
                              name: "Max",
  brother:

                   >>
                              age: 11
    name: "Max"
                           },
    age: 11
                           sister: {
  sister:
                              name: "Ida",
    name: "Ida"
                              age: 9
    age: 9
                           }
                        };
Lexical Scoping / Variable Safety


                        (function() {
                          var change, inner, outer;
                          outer = 1;
outer = 1

                   >>
                          change = function() {
change = ->
                             var inner;
  inner = -1
                             inner = -1;
  outer = 10
                             return (outer = 10);
inner = change()
                          };
                          inner = change();
                        }).call(this);
Splats...



gold = silver = rest = "unknown"

awardMedals = (first, second, others...) ->
  gold   = first
  silver = second
  rest   = others
OOP

class Animal
  constructor: (@name) ->

 move: (meters) ->
   alert @name + " moved " + meters + "m."

class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5

sam = new Snake "Sammy the Python"
sam.move()
The Rest...
✔   Existential operator
✔   Pattern matching with object literals
✔   Switch/When/Else
✔   Chained comparison
✔   Array comprehension
✔   Array slicing and splicing with ranges
✔   Everything is an expression; always a return value
✔   Function binding syntactical sugar
✔   String and RegExp Interpolation
✔   Multiline Strings, Heredocs, and Block Comments
✔   "text/coffeescript" script tags with extras/coffee-script.js
✔   It's just JavaScript
Last but not lease
●
    http://ryan.mcgeary.org/talks/2010/10/21/coffeescript-novarug/
●
    http://www.slideshare.net/mtaberski/coffee-script-6089214


●
    http://jashkenas.github.com/coffee-script/
Lim Chanmann


http://chanmannlim.wordpress.com/
           @lchanmann
      chanmannlim@gmail.com




           InSTEDD

CoffeeScript - JavaScript in a simple way

  • 1.
    CoffeeScript JavaScript in asimple way Open Tech Talk – 22 Jan 2011 @HackerSpacePP @lchanmann
  • 2.
    CoffeeScript ● One-to-one with JavaScript ● Better functional syntax ● Compiles to the good parts
  • 3.
    Why CoffeeScript? ● Less code ● Readability ● Easy to understand, and maintain ● But … you should know how JavaScript's concept work
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
    Functions square = (x)-> x * x area = (x, y) -> x * y cube = (x) -> x * square x
  • 11.
    Objects kids = { brother: { kids = name: "Max", brother: >> age: 11 name: "Max" }, age: 11 sister: { sister: name: "Ida", name: "Ida" age: 9 age: 9 } };
  • 12.
    Lexical Scoping /Variable Safety (function() { var change, inner, outer; outer = 1; outer = 1 >> change = function() { change = -> var inner; inner = -1 inner = -1; outer = 10 return (outer = 10); inner = change() }; inner = change(); }).call(this);
  • 13.
    Splats... gold = silver= rest = "unknown" awardMedals = (first, second, others...) -> gold = first silver = second rest = others
  • 14.
    OOP class Animal constructor: (@name) -> move: (meters) -> alert @name + " moved " + meters + "m." class Snake extends Animal move: -> alert "Slithering..." super 5 sam = new Snake "Sammy the Python" sam.move()
  • 15.
    The Rest... ✔ Existential operator ✔ Pattern matching with object literals ✔ Switch/When/Else ✔ Chained comparison ✔ Array comprehension ✔ Array slicing and splicing with ranges ✔ Everything is an expression; always a return value ✔ Function binding syntactical sugar ✔ String and RegExp Interpolation ✔ Multiline Strings, Heredocs, and Block Comments ✔ "text/coffeescript" script tags with extras/coffee-script.js ✔ It's just JavaScript
  • 16.
    Last but notlease ● http://ryan.mcgeary.org/talks/2010/10/21/coffeescript-novarug/ ● http://www.slideshare.net/mtaberski/coffee-script-6089214 ● http://jashkenas.github.com/coffee-script/
  • 17.
    Lim Chanmann http://chanmannlim.wordpress.com/ @lchanmann chanmannlim@gmail.com InSTEDD