Slide 1

Slide 1 text

CoffeeScript A New Brew David L Kinney WindyCityRails Chicago 2011

Slide 2

Slide 2 text

My History with JS

Slide 3

Slide 3 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 4

Slide 4 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 5

Slide 5 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 6

Slide 6 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 7

Slide 7 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 8

Slide 8 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 9

Slide 9 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 10

Slide 10 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 11

Slide 11 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 12

Slide 12 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 13

Slide 13 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 14

Slide 14 text

My History with JS 1995 JS released It’s not Applets! YAY! 1996–2003 DHTML fails Meh. 2004–2009 GMail, Google Maps, jQuery Ajax! YAY! Present MVC in the browser Meh.

Slide 15

Slide 15 text

My History with JS Today, I love JavaScript

Slide 16

Slide 16 text

My History with JS Today, I love JavaScript but we can do better

Slide 17

Slide 17 text

What’s Wrong with JS?

Slide 18

Slide 18 text

What’s Wrong with JS?

Slide 19

Slide 19 text

What’s Wrong with JS? 1100 pages

Slide 20

Slide 20 text

What’s Wrong with JS? 1100 pages 176 pages

Slide 21

Slide 21 text

What’s Wrong with JS?

Slide 22

Slide 22 text

What’s Wrong with JS? • function

Slide 23

Slide 23 text

What’s Wrong with JS? • function • return

Slide 24

Slide 24 text

What’s Wrong with JS? • function • return • var

Slide 25

Slide 25 text

What’s Wrong with JS? • function • return • var • ==

Slide 26

Slide 26 text

What’s Wrong with JS? • function • return • var • == • for

Slide 27

Slide 27 text

What’s Wrong with JS? • function • return • var • == • for • prototype-based OO

Slide 28

Slide 28 text

What’s Right with JS?

Slide 29

Slide 29 text

What’s Right with JS? • closures / blocks • namespaces • loosely typed variables • evented instead of threaded

Slide 30

Slide 30 text

Starting Over Today

Slide 31

Slide 31 text

Starting Over Today • Just give me the good parts

Slide 32

Slide 32 text

Starting Over Today • Just give me the good parts • …and improve the expressiveness of business logic

Slide 33

Slide 33 text

Starting Over Today • Just give me the good parts • …and improve the expressiveness of business logic • …but let me interoperate with all the existing great JS libraries

Slide 34

Slide 34 text

Whirlwind Tour

Slide 35

Slide 35 text

Whirlwind Tour intersection = -> { |ary1,ary2| ary1 & ary2 } Ruby

Slide 36

Slide 36 text

Whirlwind Tour var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; }

Slide 37

Slide 37 text

Whirlwind Tour var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; }

Slide 38

Slide 38 text

Whirlwind Tour var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; }

Slide 39

Slide 39 text

var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; } Whirlwind Tour

Slide 40

Slide 40 text

var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; } Whirlwind Tour

Slide 41

Slide 41 text

var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; } Whirlwind Tour X X X X X

Slide 42

Slide 42 text

var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; } Whirlwind Tour X X X X X X X

Slide 43

Slide 43 text

var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; } Whirlwind Tour X X X X X X X X X X X X X

Slide 44

Slide 44 text

var intersection = function(ary1, ary2) { if (ary1.length > ary2.length) { var tmp = ary1; ary1 = ary2; ary2 = tmp; } var out = []; for (var i=0; i= 0) out.push(val); } return out; } Whirlwind Tour X X X X X X X X X X X X X X X X X X

Slide 45

Slide 45 text

Whirlwind Tour intersection = (ary1, ary2) if (ary1.length > ary2.length) tmp = ary1; ary1 = ary2; ary2 = tmp out = [] for (i=0; i= 0) out.push(val) out

Slide 46

Slide 46 text

Whirlwind Tour intersection = (ary1, ary2) if (ary1.length > ary2.length) tmp = ary1; ary1 = ary2; ary2 = tmp out = [] for (i=0; i= 0) out.push(val) out ->

Slide 47

Slide 47 text

Whirlwind Tour intersection = (ary1, ary2) -> if (ary1.length > ary2.length) tmp = ary1; ary1 = ary2; ary2 = tmp out = [] for (i=0; i= 0) out.push(val) out

Slide 48

Slide 48 text

Whirlwind Tour intersection = (ary1, ary2) -> if (ary1.length > ary2.length) tmp = ary1; ary1 = ary2; ary2 = tmp out = [] for (i=0; i= 0) out.push(val) out

Slide 49

Slide 49 text

Whirlwind Tour intersection = (ary1, ary2) -> if (ary1.length > ary2.length) tmp = ary1; ary1 = ary2; ary2 = tmp out = [] for (i=0; i= 0) out.push(val) out

Slide 50

Slide 50 text

Whirlwind Tour intersection = (ary1, ary2) -> if ary1.length > ary2.length tmp = ary1; ary1 = ary2; ary2 = tmp out = [] for (i=0; i= 0) out.push(val) out

Slide 51

Slide 51 text

Whirlwind Tour intersection = (ary1, ary2) -> if ary1.length > ary2.length [ary2,ary1] = [ary1,ary2] out = [] for (i=0; i= 0) out.push(val) out

Slide 52

Slide 52 text

Whirlwind Tour intersection = (ary1, ary2) -> out = [] for (i=0; i= 0) out.push(val) out if ary1.length > ary2.length [ary2,ary1] = [ary1,ary2]

Slide 53

Slide 53 text

Whirlwind Tour intersection = (ary1, ary2) -> out = [] for (i=0; i= 0) out.push(val) out if ary1.length > ary2.lengt [ary2,ary1] = [ary1,ary2]

Slide 54

Slide 54 text

Whirlwind Tour intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l out = [] for (i=0; i= 0) out.push(val) out

Slide 55

Slide 55 text

Whirlwind Tour intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l out = [] for (i=0; i= 0) out.push(val) out

Slide 56

Slide 56 text

Whirlwind Tour intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l out = (x for x in ary1 when ary2.indexOf(x) >= 0) out

Slide 57

Slide 57 text

intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l out = (x for x in ary1 when ary2.indexOf(x) >= 0) out Whirlwind Tour ary1.

Slide 58

Slide 58 text

intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l out = (x for x in ary1 when ary2.indexOf(x) >= 0) out Whirlwind Tour select { |x| ary2.indexOf(x) >= 0 }. ary1.

Slide 59

Slide 59 text

intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l out = (x for x in ary1 when ary2.indexOf(x) >= 0) out Whirlwind Tour select { |x| ary2.indexOf(x) >= 0 }. ary1. map { |x| x }

Slide 60

Slide 60 text

Whirlwind Tour intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l out = (x for x in ary1 when ary2.indexOf(x) >= 0) out

Slide 61

Slide 61 text

Whirlwind Tour intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l out = (x for x in ary1 when ary2.indexOf(x) >= 0) out when x in ary2)

Slide 62

Slide 62 text

intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l Whirlwind Tour out = out (x for x in ary1 when x in ary2)

Slide 63

Slide 63 text

intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l Whirlwind Tour (x for x in ary1 when x in ary2)

Slide 64

Slide 64 text

intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l (x for x in ary1 when x in ary2) Whirlwind Tour

Slide 65

Slide 65 text

intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l (x for x in ary1 when x in ary2) Whirlwind Tour

Slide 66

Slide 66 text

intersection = (ary1, ary2) -> [ary2,ary1] = [ary1,ary2] if ary1.length > ary2.l (x for x in ary1 when x in ary2) Whirlwind Tour

Slide 67

Slide 67 text

Whirlwind var intersection; var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; }; intersection = function(ary1, ary2) { var x, _i, _len, _ref, _results; if (ary1.length > ary2.l) { _ref = [ary1, ary2], ary2 = _ref[0], ary1 = _ref[1]; } _results = []; for (_i = 0, _len = ary1.length; _i < _len; _i++) { x = ary1[_i]; if (__indexOf.call(ary2, x) >= 0) { _results.push(x); } } return _results; }; JavaScript

Slide 68

Slide 68 text

Functions

Slide 69

Slide 69 text

Functions say_hello = -> "Hello, world" # same as say_hello = -> "Hello, world" # invoked with say_hello()

Slide 70

Slide 70 text

Functions say_hello = -> "Hello, world" # same as say_hello = -> "Hello, world" # invoked with say_hello() // compiles to: var say_hello; say_hello = function() { return "Hello, world"; }; say_hello();

Slide 71

Slide 71 text

Functions say_hello = (name) -> "Hello, #{name}" say_hello("David") say_hello "David"

Slide 72

Slide 72 text

Functions say_hello = (name) -> "Hello, #{name}" say_hello("David") say_hello "David" // compiles to: var say_hello; say_hello = function(name) { return "Hello, " + name; }; say_hello("David"); say_hello("David");

Slide 73

Slide 73 text

Functions Using jQuery show_person = (person) -> $('#firstname').text(person.first_name) $('#lastname').text(person.last_name) $('button').click -> $('#status').text('Clicked')

Slide 74

Slide 74 text

Classes

Slide 75

Slide 75 text

Classes class Person constructor: (first_name, last_name) -> this.first_name = first_name this.last_name = last_name full_name: -> "#{this.first_name} #{this.last_name}"

Slide 76

Slide 76 text

Classes class Person constructor: (first_name, last_name) -> @first_name = first_name @last_name = last_name full_name: -> "#{@first_name} #{@last_name}"

Slide 77

Slide 77 text

Classes class Person constructor: (first_name, last_name) -> @first_name = first_name @last_name = last_name full_name: -> "#{@first_name} #{@last_name}"

Slide 78

Slide 78 text

Classes class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}"

Slide 79

Slide 79 text

Classes class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" // compiles to: var Person; Person = (function() { function Person(first_name, last_name) { this.first_name = first_name; this.last_name = last_name; } Person.prototype.full_name = function() { return "" + this.first_name + " " + this.last_name; }; return Person; })();

Slide 80

Slide 80 text

class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" Classes: Inheritance

Slide 81

Slide 81 text

class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" class Employee extends Person constructor: (first, last, @title) -> super(first, last) full_name: -> "#{super}, #{@title}" Classes: Inheritance

Slide 82

Slide 82 text

class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" class Employee extends Person constructor: (first, last, @title) -> super(first, last) full_name: -> "#{super}, #{@title}" // "class Employee extends Person" compiles to: var Employee; var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; Employee = (function() { __extends(Employee, Person); function Employee() { Employee.__super__.constructor.apply(this, arguments); } return Employee; })(); Classes: Inheritance

Slide 83

Slide 83 text

class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" class Employee extends Person constructor: (first, last, @title) -> super(first, last) full_name: -> "#{super}, #{@title}" Classes: Inheritance

Slide 84

Slide 84 text

class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" class Employee extends Person constructor: (first, last, @title) -> super(first, last) full_name: -> "#{super}, #{@title}" employee = new Employee("Tim", "Cook", "CEO") console.log employee.full_name() # prints "Tim Cook, CEO" Classes: Inheritance

Slide 85

Slide 85 text

Classes: Fat Arrow class PersonController constructor: (@person) -> display_person: -> $('#person_name').text(@person.full_name)

Slide 86

Slide 86 text

Classes: Fat Arrow class PersonController constructor: (@person) -> display_person: -> $('#person_name').text(@person.full_name) person = # loaded from server

Slide 87

Slide 87 text

Classes: Fat Arrow class PersonController constructor: (@person) -> display_person: -> $('#person_name').text(@person.full_name) person = # loaded from server controller = new PersonController(person)

Slide 88

Slide 88 text

Classes: Fat Arrow class PersonController constructor: (@person) -> display_person: -> $('#person_name').text(@person.full_name) person = # loaded from server controller = new PersonController(person) $('button').click controller.display_person # FAIL!

Slide 89

Slide 89 text

Classes: Fat Arrow class PersonController constructor: (@person) -> display_person: -> $('#person_name').text(@person.full_name) => person = # loaded from server controller = new PersonController(person) $('button').click controller.display_person # FAIL!

Slide 90

Slide 90 text

Comparisons

Slide 91

Slide 91 text

1 == '1' // true 1 == '1.0' // true 1 == true // true Comparisons JavaScript

Slide 92

Slide 92 text

1 == '1' // true 1 == '1.0' // true 1 == true // true Comparisons JavaScript 1 === '1.0' // false 1 === '1' // false

Slide 93

Slide 93 text

1 == '1' // true 1 == '1.0' // true 1 == true // true Comparisons JavaScript 1 === '1.0' // false 1 === '1' // false 1 is '1' # false 1 is '1.0' # false 1 is true # false 1 is 1 # true 1 is 1.0 # true CoffeeScript

Slide 94

Slide 94 text

is odd = num % 2 is 1 isnt apple_rejection = app_lang isnt "Objective-C" in toddler = age in [1,2,3] not even = not odd and never_true = even and odd or fun = ruby or coffee_script Comparisons

Slide 95

Slide 95 text

Existential Operator typeof person !== "undefined" && person !== null JavaScript

Slide 96

Slide 96 text

Existential Operator typeof person !== "undefined" && person !== null JavaScript person? CoffeeScript

Slide 97

Slide 97 text

Existential Operator $('#name').text(person.full_name) if person? person ?= new Person("David", "Kinney") beneficiary = spouse ? daughter zip = lottery.drawWinner?().address?.zipcode

Slide 98

Slide 98 text

More Features

Slide 99

Slide 99 text

Playing with Prototypes String::dasherize = -> this.replace /_/g, "-"

Slide 100

Slide 100 text

Switch switch day when "Mon" then go work when "Tue" then go relax when "Thu" then go iceFishing when "Fri", "Sat" if day is bingoDay go bingo go dancing when "Sun" then go church else go work

Slide 101

Slide 101 text

try-catch-finally try allHellBreaksLoose() catsAndDogsLivingTogether() catch error print error finally cleanUp()

Slide 102

Slide 102 text

Stuff with Objects yearsOld = alice: 10, bob: 9, charlie: 11 ages = for child, age of yearsOld "#{child} is #{age}"

Slide 103

Slide 103 text

Destructuring Objects futurists = sculptor: "Umberto Boccioni" painter: "Vladimir Burliuk" poet: name: "F.T. Marinetti" address: [ "Via Roma 42R" "Bellagio, Italy 22021" ] # extract name, street, and city {poet: {name, address: [street, city]}} = futurists

Slide 104

Slide 104 text

Rails 3.1

Slide 105

Slide 105 text

Rails 3.1

Slide 106

Slide 106 text

Rails 3.1

Slide 107

Slide 107 text

Rails 3.1

Slide 108

Slide 108 text

Rails 3.1 $ -> more_link = $('') more_link.addClass 'more-link' more_link.text 'more' more_link.click -> $('#full-text').slideDown() $('#full-text').before(more_link).hide() app/assets/javascript/more_expander.js.coffee

Slide 109

Slide 109 text

Rails 3.1 class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" app/assets/javascript/person.js.coffee

Slide 110

Slide 110 text

Rails 3.1 class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" app/assets/javascript/person.js.coffee class Employee extends Person constructor: (first, last, @title) -> super(first, last) full_name: -> "#{super}, #{@title}" app/assets/javascript/employee.js.coffee

Slide 111

Slide 111 text

Rails 3.1 class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" app/assets/javascript/person.js.coffee class Employee extends Person constructor: (first, last, @title) -> super(first, last) full_name: -> "#{super}, #{@title}" app/assets/javascript/employee.js.coffee window.Person = Person unless window.Person?

Slide 112

Slide 112 text

Rails 3.1 class Person constructor: (@first_name, @last_name) -> full_name: -> "#{@first_name} #{@last_name}" app/assets/javascript/person.js.coffee class Employee extends Person constructor: (first, last, @title) -> super(first, last) full_name: -> "#{super}, #{@title}" app/assets/javascript/employee.js.coffee window.Person = Person unless window.Person? window.Employee = Employee unless window.Employee?

Slide 113

Slide 113 text

Next Steps Website coffeescript.org PeepCode Screencast peepcode.com/ products/coffeescript PragProg Book pragprog.com/book/ tbcoffee/coffeescript

Slide 114

Slide 114 text

Rich UIs Agile development Rails and mobile for enterprise