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

CoffeeScript: JavaScript "bien hecho"

CoffeeScript: JavaScript "bien hecho"

Transparencias de mi taller sobre CoffeeScript en la Tenerife Lan Party 2012 ... espero que las disfruten :-)

Yeray Darias

July 22, 2012
Tweet

More Decks by Yeray Darias

Other Decks in Technology

Transcript

  1. var message = “hola”; function saluda() { return message; }

    var message = “adios”; function despidete() { return message; } Global variables Si ambos ficheros están “cargados”, ¿Cuál es la salida esperada en cada función?
  2. Global variables CoffeeScript no crea variables globales de forma implícita

    (function() { var message, saluda; message = "hola"; saluda = function() { return message; }; }).call(this); message = "hola" saluda = () -> message
  3. Scopes Todas las variables en una función se declaran al

    principio (function() { var message, saluda; message = "hola"; saluda = function() { return message; }; }).call(this); message = "hola" saluda = () -> message
  4. Semicolons CoffeeScript autocompleta los ; (function() { var message, saluda;

    message = "hola"; saluda = function() { return message; }; }).call(this); message = "hola" saluda = () -> message
  5. Reserved words CoffeeScript escapa las palabras reservadas automáticamente var myObject;

    myObject = { "case": 'keyword', tutu: 'allowed name' }; myObject = case: 'keyword' tutu: 'allowed name'
  6. Equality comparisons CoffeeScript siempre usa los operadores como ===, !==,

    ... var testFunction; testFunction = function(input) { if (input === 'string') { 'string'; } if (input === 9) { return 'number nine'; } }; testFunction = (input) -> if (input == 'string') 'string' if (input == 9) 'number nine'
  7. switch fall through Bond = (input) -> switch input when

    'Sean Connery', 'Daniel Craig' 'Fucking crack' when 'Roger Moore' 'A bit boring'
  8. switch fall through var Bond; Bond = function(input) { switch

    (input) { case 'Sean Connery': case 'Daniel Craig': return 'Fucking crack'; case 'Roger Moore': return 'A bit boring'; } }; Todas las opciones acaban con un return
  9. Control de flujo mood = greatlyImproved if singing if happy

    and knowsIt clapsHands() else showIt() date = if friday then sue else jill isToday = yes unless yesterday or tomorrow // ----------------------------------------------------- cholesterol = 127 healthy = 200 > cholesterol > 60 Chained comparisons Everything a expression
  10. Control de flujo Bond = (input) -> switch input when

    'Sean Connery', 'Daniel Craig' 'Fucking crack' when 'Roger Moore' 'A bit boring' else 'No comments'
  11. Bucles # Health conscious meal. foods = ['broccoli', 'spinach', 'chocolate']

    eat food for food in foods when food isnt 'chocolate' countdown = (num for num in [10..1]) # Econ 101 if this.studyingEconomics buy() while supply > demand sell() until supply > demand while age < 18 canNotSmoke() canNotDrink()
  12. Funciones square = (x) -> x * x cube =

    (x) -> square(x) * x fill = (container, liquid = "coffee") -> "Filling the #{container} with #{liquid}..." // -------------------------------------------------- awardMedals = (first, second, others...) -> gold = first silver = second rest = others Splats String interpolation
  13. Operators & aliases CoffeeScript JavaScript is === isnt !== not

    ! and && or || true, yes, on true false, no, off false @, this this of in in no JS equivalent
  14. Clases class Animal constructor: (@name) -> move: (meters) -> alert

    @name + " moved #{meters}m." class Snake extends Animal move: -> alert "Slithering..." super 5 class Horse extends Animal move: -> alert "Galloping..." super 45
  15. Clases sam = new Snake "Sammy the Python" tom =

    new Horse "Tommy the Palomino" sam.move() tom.move()
  16. Function binding Account = (customer, cart) -> @customer = customer

    @cart = cart $('.shopping_cart').bind 'click', (event) => @customer.purchase @cart var Account; Account = function(customer, cart) { var _this = this; this.customer = customer; this.cart = cart; return $('.shopping_cart').bind('click', function(event) { return _this.customer.purchase(_this.cart); }); };