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

CoffeeScript 101

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

CoffeeScript 101

Avatar for Eric Schoffstall

Eric Schoffstall

February 23, 2013
Tweet

More Decks by Eric Schoffstall

Other Decks in Programming

Transcript

  1. Me • I write full stack javascript • I work

    at Fractal • I like to code • I love open source • @eschoff • github.com/Contra
  2. What • CoffeeScript is an attempt to expose the good

    parts of javascript in a simple way • “It’s just javascript”
  3. Javascript • Global variables and scoping nightmares • Optional semicolon

    • Typeof is awkward (null == object?) • ==,===,=! • “Lisp in C’s Clothing” – Crockford • Clunky syntax for common operations
  4. CoffeeScript • Private variables by default • Scoping handled automagically

    • No such thing as == • Compiled code is super fast • Compiled code passes JSLint • Shorthand syntax for common operations • The code compiles one-to-one with it’s javascript equivalent (unlike others)
  5. What you’ll learn • Syntax • Expressions • List comprehensions

    • Functions • Conditionals • Splats • Destructuring
  6. Syntax • Whitespace sensitive (tabs not braces) • Parenthesis optional

    • Commas/braces optional for objects • Conditional operators • var does not exist • Semicolons do not exist
  7. Operators • “is” is the equivalent of “===“ • “isnt”

    is the equivalent of “!==“ • “and” is the equivalent of “&&” • “unless” is the equivalent of “if (!)”
  8. String Interpolation a = "world" b = "Hello #{a}" •

    Any expression can be within a string interpolation
  9. Objects a = key: "value" other: "value" more: values: ["values"]

    • Spacing spacing spacing • So fresh and so clean
  10. Existential Operator b = "i exist!" if a? doWork a

    else doWork b • ? Checks for null and undefined
  11. Functions sayHello = (who) -> console.log "Hello #{who}!" sayHello "Desert

    code camp" • -> is the equivalent of function(){ • No braces – indentation is key • No parenthesis needed when you call a function with arguments
  12. Destructuring Objects options = isCool: true {isCool} = options •

    {isCool} = options is the equivalent of isCool = options.isCool
  13. Splats • Simple way to glob up the rest •

    Can be used with destructuring, function arguments, and for calling functions • …
  14. Destructuring Arrays + Splats people = ["bob","jill","tom","jim"] [first, second, rest...]

    = people • first = “bob” • second = “jill” • rest = [“tom”,”jim”]
  15. Loops sayHello = (who) -> for person in who console.log

    "Hello #{person}!" sayHello ["Phoenix", "Haters"] • Compiled loops are more efficient than loops by hand
  16. Conditional Loops sayHello = (who) -> for person in who

    when person is "Phoenix" console.log "Hello #{person}!" sayHello ["Phoenix", "Haters"] • Compiled loops are more efficient than loops by hand
  17. Equivalent var sayHello = function(who) { for (var i =

    0; i < who.length; i++) { if (who[i] === "Phoenix") { console.log("Hello " + who[i] + "!"); } } }; sayHello(["Phoenix", "Haters"]);
  18. List Comprehensions people = [ name: "Tom" cool: true ,

    name: "Bob" cool: false , name: "Jim" cool: true ] cool = (p for p in people when p.cool is true) • Wrapping a loop in parenthesis turns it into a list comprehension • cool = [tom, bob]
  19. List Comprehensions + Select people = [ name: "Tom" cool:

    true , name: "Bob" cool: false , name: "Jim" cool: true ] cool = (p.name for p in people when p.cool is true) • cool = [“Tom”, “Bob”]
  20. forEach • cool = [“Tom”, “Bob”] people = ["Tom", "Jim",

    "Bob"] slap = (name) -> console.log "#{name} got slapped" slap idiot for idiot in people
  21. End • Follow me on Twitter @eschoff @wearefractal • For

    more see http://coffeescriptbook.org/