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

Coffescript

 Coffescript

Presentació de Pau sobre Coffescript i la seva utilització

creantbits

July 26, 2013
Tweet

More Decks by creantbits

Other Decks in Programming

Transcript

  1. Com fer-nos la vida una mica més fàcil programant per

    navegadors paurullan@apsl. net Què pot fer CoffeeScript per nosaltres
  2. The Zen of Python Beautiful is better than ugly. Explicit

    is better than implicit. Simple is better than complex. Flat is better than nested. Sparse is better than dense. Readability counts.
  3. (function() { var $; $ = jQuery; $(document). ready(function() {

    return $("#a"). click(function() { return $("#b"). toggle(); }); }); }). call(this);
  4. var $; $ = jQuery; $(document). ready(function() { return $("#a").

    click(function() { return $("#b"). toggle(); }); }); bare
  5. parèntesis implícits com en Ruby qx. Class. define("x. Application", extend:

    qx. application. Standalone, … ) --------------------------- qx. Class. define "x. Application", extend: qx. application. Standalone,
  6. intercanvi de variables [x, y] = [1 , 2, ]

    [a, b] = [b, a] (semblant al de Python però cal ficar-ho en llistes)
  7. array destructuring weather = (location) -> # aquí obtindríem alguns

    valors útils [location, 32, "Caloreta"] [city, temp, forecast] = weather("Palma")
  8. splats tercer_endavant = (a, b, rest. . . ) ->

    rest x = tercer_endavant(1 , 2, 3, 4, 5, 6) # x = 3, 4, 5, 6 *args en Python splat de Ruby
  9. splats + destructuring s = "text a xepar per espais"

    [inici, cos. . . , final] = s. split(" ") # inici = "text" # cos = ["a", "xepar", "per"] # final = "espais"
  10. generació de rangs [1 . . 4] # [1 ,

    2, 3, 4] [1 . . . 4] # [1 , 2, 3] com en Ruby range de Python
  11. generació de rangs //[1 . . 22] var _i, _results;

    (fucntion() { _results = []; for (_i = 1 ; _i <= 22; _i++){ _results. push(_i); } return _results; }). apply(this);
  12. slicing n = [1 , 2, 3, 4, ] n[2.

    . 4] n[3. . 5] = [-1 , ' a' , "bb"] [a, b. . . , x] = [1 , 2, 3, 4]
  13. list comprehension x*x for x in [1 . . 1

    0] item. map((x) -> x*x) de moment no amb diccionaris
  14. operador «@» pel «this» l = [1 , 2, 3,

    4, ] h = (a, b, c, d) -> a+b+c+d l. apply(@, h)