CoffeeScript - Spartan Javascript

CoffeeScript - Spartan Javascript

Going over the basics of CoffeeScript. Given at BarCampRochester and BuffaloJS.

Eb8975af8e49e19e3dd6b6b84a542e26?s=128

Nick Quaranto

January 16, 2013
Tweet

Transcript

  1. R R CoffeeScript @qrush BarCampRoc Apr 2013 Spartan JavaScript

  2. None
  3. JavaScript as a language is Not Verbose

  4. Good JS is OO

  5. Good JS is Concise

  6. CoffeeScript is “Just JS”

  7. CoffeeScript writes Good JS

  8. CoffeeScript is Spartan!

  9. a new battle! http://www.flickr.com/photos/pshab/1578426589/

  10. the closest I’ve felt to the power I had 20

    years ago in Smalltalk ~Ward Cunningham
  11. it was the first time I experienced language envy ~DHH

  12. I use both ~Brendan Eich

  13. function foo() { }

  14. function foo() func foo() function foo( function foo() func foo()

    function foo( function foo() func foo() function foo( function foo() func
  15. function foo() func foo() function foo( function foo() func foo()

    function foo( function foo() func foo() function foo( function foo() func
  16. this

  17. this

  18. foo = function() { return 1 + 1; }

  19. foo = () -> 1 + 1

  20. bar = function(x) { return x + 1; }

  21. bar = (x) -> x + 1

  22. _.bind(fn, obj) http://underscorejs.org/#bind http://coffeescript.org#fat_arrow

  23. baz = () => this.foo()

  24. foz = () => @foo()

  25. $ -> new ItemsRouter() Backbone.history.start()

  26. $ -> new ItemsRouter() Backbone.history.start()

  27. more features

  28. indents are scope

  29. last statements implicitly return

  30. all vars local by default

  31. easier object syntax

  32. title: "Sparta!" author: name: "Leonidas" handle: "theking" apples: 1

  33. string interpolation

  34. var html = "<option value='" + this.id + "'>" +

    this.get("title") + "</option>";
  35. "<option value='#{@id}'> #{@get("title")}</option>"

  36. "<option value='#{@id}'> #{@get("title")}</option>"

  37. inheritance actually works

  38. class Item extends Backbone.Model url: -> "/events/#{@id}"

  39. loops are less painful

  40. None
  41. nums = [4, 5, 6] for i in nums console.log

    i 4 5 6
  42. comprehensions compact code

  43. nums = [1, 2, 3] [ 1, 2, 3 ]

    sq = (n * n for n in nums) [ 1, 4, 9 ]
  44. nums = [1, 2, 3] [ 1, 2, 3 ]

    sq = (n * n for n in nums) [ 1, 4, 9 ]
  45. conditionals and loops can be inline

  46. console.log("Sparta!") if true Sparta! console.log("Persia!") unless false Sparta!

  47. x = 6 6 x += 1 while x <

    10 [ 7, 8, 9, 10 ] x 10
  48. get existential

  49. spear? false spear = -> "Throw!" [Function] spear? true

  50. window.bcx ?= {} {}

  51. startWaiting: -> @waitTimeout ?= setTimeout => @$container.addClass "waiting" , 300

    stopWaiting: -> if @waitTimeout? clearTimeout @waitTimeout @waitTimeout = null @$container.removeClass "waiting"
  52. meet your enemies

  53. None
  54. making coffee • Rails: http://guides.rubyonrails.org/ asset_pipeline.html • Jekyll & Rake:

    https://gist.github.com/4496420 • Node: npm install coffee • Anything else: Less.app http://incident57.com/less/
  55. debugging is not terrible

  56. http://alexspeller.com/

  57. some silly keywords

  58. yes, true, on no, false, off

  59. more info • http://coffeescript.org • http://coffeescript.codeschool.com/ • http://robots.thoughtbot.com/post/ 9251081564/coffeescript-spartan- javascript