"Coffee Script" in Brief

49
Much Ado About CoeeScript Mattt Thompson (@mattt) Lone Star Ruby Conf V

description

Original Presentation "Much Ado About CoffeeScript - LSRC V" By Mattt Thompson => http://www.slideshare.net/matttthompson/much-ado-about-coffeescript-lsrc-v

Transcript of "Coffee Script" in Brief

Page 1: "Coffee Script" in Brief

Much Ado About Co!eeScript

Mattt Thompson (@mattt)Lone Star Ruby Conf V

Page 2: "Coffee Script" in Brief

Sass

Co!eeScript

Page 3: "Coffee Script" in Brief

Sass ! CSS$blue: #3bbfce$margin: 16px

.content-navigation border-color: $blue color: darken($blue, 9%)

.border padding: $margin / 2 margin: $margin / 2 border-color: $blue

.content-navigation { border-color: #3bbfce; color: #2ca2af; }

.border { padding: 8px; margin: 8px; border-color: #3bbfce; }

Page 4: "Coffee Script" in Brief

Co!eeScript " JavaScript

Account = (customer, cart) -> @customer = customer @cart = cart

$('.shopping_cart').bind ↩'click', (event) => @customer.purchase @cart

var Account;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };Account = function(customer, cart) { this.customer = customer; this.cart = cart; return $('.shopping_cart').bind('click', __bind(function(event) { return this.customer.purchase(this.cart); }, this));};

Page 5: "Coffee Script" in Brief

Co!eeScript

Page 6: "Coffee Script" in Brief

“It’s just Javascript”

Page 7: "Coffee Script" in Brief

• Less Syntactic Cruft• JavaScript Design Patterns are Features of Co!eeScript

Page 8: "Coffee Script" in Brief
Page 9: "Coffee Script" in Brief
Page 10: "Coffee Script" in Brief

Syntax

Page 11: "Coffee Script" in Brief

jQuery(function() { $("#title").show();});

Page 12: "Coffee Script" in Brief

jQuery function() $("#title").show()

Page 13: "Coffee Script" in Brief

jQuery -> $("#title").show()

Page 14: "Coffee Script" in Brief

Store.prototype.add = function(item) { this.items.push(item);}

Page 15: "Coffee Script" in Brief

Store.prototype.add = function(item) this.items.push(item)

Page 16: "Coffee Script" in Brief

Store.prototype.add = function(item) @items.push(item)

Page 17: "Coffee Script" in Brief

add: function(item) @items.push(item)

Page 18: "Coffee Script" in Brief

add: -> (item) @items.push(item)

Page 19: "Coffee Script" in Brief

add: (item) -> @items.push(item)

Page 20: "Coffee Script" in Brief

• Redundant Punctuation Omitted

• function becomes ->

• this. becomes @

• {} becomes ⇥

Page 21: "Coffee Script" in Brief

Features

Page 22: "Coffee Script" in Brief
Page 23: "Coffee Script" in Brief
Page 24: "Coffee Script" in Brief

Class Pattern

1

Page 25: "Coffee Script" in Brief

class Animal constructor: (name) -> @name = name

Page 26: "Coffee Script" in Brief

var Animal;Animal = (function() { function Animal(name) { this.name = name; } return Animal;})();

Page 27: "Coffee Script" in Brief

2Lexical Scoping

(No Global Variables)

Page 28: "Coffee Script" in Brief

window.Animal = class Animal constructor: (name) -> @name = name

Page 29: "Coffee Script" in Brief

Default Arguments

3

Page 30: "Coffee Script" in Brief

window.Animal = class Animal constructor: (name = "Unknown") -> @name = name

Page 31: "Coffee Script" in Brief

String Interpolation

4

Page 32: "Coffee Script" in Brief

class Bear extends Animal constructor: (name) -> @name = "#{name}, the Bear"

Page 33: "Coffee Script" in Brief

Conditional Su!ixes

5

Page 34: "Coffee Script" in Brief

@price = amount if amount > 0.00

Page 35: "Coffee Script" in Brief

Operator Aliases

6

Page 36: "Coffee Script" in Brief

@lovesTacos = false unless @name is "Mattt"

Page 37: "Coffee Script" in Brief

Co!eeScript JavaScript

is ===

isnt !==

not !

and &&

or ||

true, yes, on true

@, this this

of in

in N/A

Page 38: "Coffee Script" in Brief

7Deconstructing

Assignment

Page 39: "Coffee Script" in Brief

[dollars, cents] = input.match /(\d+)/g

Page 40: "Coffee Script" in Brief

Existential Operator

8

Page 41: "Coffee Script" in Brief

?

Page 42: "Coffee Script" in Brief

[dollars, cents] = (input.match /(\d+)/g) ? [0, 0]

Page 43: "Coffee Script" in Brief

Splats

9

Page 44: "Coffee Script" in Brief

gold, silver, bronze, rest = ""

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

pieEatingCompetitors = [ "Wynn Netherland", "Steve Klabnik", "Adam Keys", "Richard Schneeman", "Rob Mack", "Tim Tyrrell", "Steve Stedman", "Mando Escamilla", "Keith Gaddis" ]

awardMedals pieEatingCompetitors...

Page 45: "Coffee Script" in Brief

List Comprehensions

10

Page 46: "Coffee Script" in Brief

eat food for food in ['toast', 'cheese', 'wine']

Page 47: "Coffee Script" in Brief

eat food for food in ['toast', 'cheese', 'wine'] ↩when food is "toast"

Page 48: "Coffee Script" in Brief

languages = { "Javascript": 3, "CoffeeScript": 9, "Ruby": 9, "Python": 6, "Objective-C": 7, "Potion": 10}

favorites = language for language, awesomeness of ↩ languages when awesomeness >= 7

Page 49: "Coffee Script" in Brief