Slide 1

Slide 1 text

CoffeeScript This is . @zachdennis

Slide 2

Slide 2 text

CoffeeScript number = 42 opposite = true number = -42 if opposite square = (x) -> x * x list = [1, 2, 3, 4, 5] math = root: Math.sqrt square: square cube: (x) -> x * square x race = (winner, runners...) -> print winner, runners alert "I knew it!" if elvis?

Slide 3

Slide 3 text

7 things to know

Slide 4

Slide 4 text

1. Compiles into JavaScript. outer = 1 changeNumbers = -> inner = -1 outer = 10 inner = changeNumbers() var changeNumbers, inner, outer; outer = 1; changeNumbers = function() { var inner; inner = -1; return outer = 10; }; inner = changeNumbers(); CoffeeScript JavaScript

Slide 5

Slide 5 text

2. White-space Sensitive changeNumbers = -> inner = -1 outer = 10 badChangeNumbers = -> inner = -1 outer = 10 changeNumbers = function() { var inner; inner = -1; return outer = 10; }; badChangeNumbers = function() {}; inner = -1; outer = 10; CoffeeScript JavaScript

Slide 6

Slide 6 text

3. Use existing JavaScript clicked = -> console.log "clicked" jQuery -> $("button").on "click", clicked CoffeeScript

Slide 7

Slide 7 text

4. Use existing CoffeeScript # clicked.coffee clicked = -> console.log "clicked" // app.js jQuery(function() { $("button").on ("click", clicked); }); clicked(); CoffeeScript JavaScript

Slide 8

Slide 8 text

5. Generated JavaScript passes JSLint CoffeeScript JavaScript ✓

Slide 9

Slide 9 text

6. Play with CoffeeScript Online http://www.coffeescript.org > Try CoffeeScript http://js2coffee.org > Coffee → JS

Slide 10

Slide 10 text

It’s JavaScript without the unnecessary syntax 7.

Slide 11

Slide 11 text

1more thing before we begin

Slide 12

Slide 12 text

1. Compile your coffee > coffee -c foo.coffee > coffee -co javascripts/ source/

Slide 13

Slide 13 text

33 language features approx. minutes

Slide 14

Slide 14 text

1. Variable Assignment outer = 1 changeNumbers = -> inner = -1 outer = 10 var changeNumbers, inner, outer; outer = 1; changeNumbers = function() { var inner; inner = -1; return outer = 10; }; You never need to write var yourself. CoffeeScript JavaScript

Slide 15

Slide 15 text

2. Conditional Assignment value = computeBigNumber() value ||= computeBigNumber() var value; value = computeBigNumber(); value || (value = computeBigNumber()) CoffeeScript JavaScript

Slide 16

Slide 16 text

3. Traditional conditionals if condition1 doThing1() else if condition2 doThing2() else doThing3() if(condition1) { doThing1(); } else if (condition2) { doThing2(); } else { doThing3(); } Remove unnecessary syntax. CoffeeScript JavaScript

Slide 17

Slide 17 text

4. One line conditionals congratulate() if winner showErrors() unless success if(winner){ congratulate(); } if(!success){ showErrors(); } Adding readability. CoffeeScript JavaScript

Slide 18

Slide 18 text

5. Conditional Operators foo == bar foo != bar foo === bar foo !== bar Safety first. CoffeeScript JavaScript

Slide 19

Slide 19 text

6. Conditional Operators foo and bar foo or bar CoffeeScript JavaScript foo && bar foo || bar Plain english.

Slide 20

Slide 20 text

7. Conditional Operators action is "clicked" action isnt "clicked" CoffeeScript action === "clicked" action !== "clicked" JavaScript Saying what you mean.

Slide 21

Slide 21 text

8. Conditional Operators admin is true admin is false checked is yes checked is no value is on value is off admin === true admin === false checked === true checked === false value === true value === false CoffeeScript JavaScript More ways to say what you mean.

Slide 22

Slide 22 text

9. Conditional Operators, In Array CoffeeScript odds = [1, 3, 5, 7, 9, 11] if 1 in odds console.log "It’s odd!" else console.log "It’s even!" Checking array membership.

Slide 23

Slide 23 text

10. Conditional Existence CoffeeScript car ?= {} car.speed ?= 75 Checking existence of a variable/property. CoffeeScript's existential operator ? returns true unless a variable is null or undefined.

Slide 24

Slide 24 text

11. Conditional Existence cont... CoffeeScript car = {} car.speed = 0 car.speed ||= 75 What about ||= instead of ?= ||= will overwrite a speed of 0 with 75 when it shouldn’t. ?= is safer.

Slide 25

Slide 25 text

12. Arrays fruits = ["apples", "bananas"] CoffeeScript JavaScript Single line or multi-line array definitions. var fruits; person = [ "apples", "bananas" ]; fruits = [ "apples" "bananas" ]

Slide 26

Slide 26 text

13. Objects person = name: "Joe", age: 39 CoffeeScript JavaScript Single line or multi-line object definitions. var person; person = { name: "Joe", age: 39 }; person = name: "Joe", age: 39

Slide 27

Slide 27 text

14. Functions hello = -> "Zach" hello = () -> "Zach" hello() # doesn’t call function hello CoffeeScript JavaScript var hello; hello = function(){ return "Zach"; }; Parameter-less function definitions.

Slide 28

Slide 28 text

15. Functions hello = (name) -> "Hello " + name hello("Joe") hello "Joe" CoffeeScript JavaScript var hello; hello = function(name){ return "Hello " + name; }; Parameter-filled function definitions.

Slide 29

Slide 29 text

16. Functions $("button").on "click", -> alert("Hello " + name) $("button").on "click", -> alert("Hello " + name) $("button").on "click", (e) -> e.preventDefault() alert("Hello " + name) CoffeeScript JavaScript In-line function definitions.

Slide 30

Slide 30 text

16 and a half. CoffeeScript JavaScript Everything is an expression. grade = (student) -> if student.excellentWork "A+" else if student.okayStuff if student.triedHard "B" else "B-" else "C" var grade; grade = function(student) { if (student.excellentWork) { return "A+"; } else if (student.okayStuff) { if (student.triedHard) { return "B"; } else { return "B-"; } } else { return "C"; } };

Slide 31

Slide 31 text

17. Splats hello = (names...) -> "Hello " + names hello("Joe", "Tim", "Jim") hello "Joe", "Tim", "Jim" CoffeeScript JavaScript Variable-length parameter lists (aka splats). var hello, __slice = [].slice; hello = function(){ var names; names = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return "Hello " + names; };

Slide 32

Slide 32 text

18. String Interpolation "Hello " + name + "!" CoffeeScript JavaScript Double quotes for interpolation. "Hello #{name}!" Single quotes for string literals. 'Hello #{name}!' "Hello #{name}"

Slide 33

Slide 33 text

19. Array comprehensions CoffeeScript Collecting values over elements. numbers = [1, 2, 3, 4] negatives = (-num for num in numbers) # negatives = [-1, -2, -3, -4]

Slide 34

Slide 34 text

20. Array comprehensions CoffeeScript The by modifier. numbers = [1, 2, 3, 4] negatives = (-num for num in numbers by 2) # negatives = [-1, -3]

Slide 35

Slide 35 text

21. Array comprehensions CoffeeScript The when modifier. numbers = [1, 2, 3, 4] negatives = (-num for num in numbers when num > 2) # negatives = [-3, -4]

Slide 36

Slide 36 text

22. Object comprehensions CoffeeScript Over properties with of. person = name: "Joe", age: 39 properties = (name for name of person) # properties = ["name", "age"]

Slide 37

Slide 37 text

23. Object comprehensions CoffeeScript Over properties and values with of. person = name: "Joe", age: 39 properties = ([name, value] for name, value of person) # properties = [["name", "Joe"], ["age", 39]]

Slide 38

Slide 38 text

24. Ranges [1..10] [1,2,3,4,5,6,7,8,9,10] CoffeeScript JavaScript [10..1] [10,9,8,7,6,5,4,3,2,1]

Slide 39

Slide 39 text

25. Comments CoffeeScript Single line and block comments! # This is a single line comment ### This is a block comment ###

Slide 40

Slide 40 text

27. De-structuring assignment CoffeeScript Array pattern matching. [foo, bar, baz] = ["foo", "bar", "baz"] # equivalent to: foo = "foo" bar = "bar" baz = "baz" # also equivalent to: arr = ["foo", "bar", "baz"] foo = arr[0] bar = arr[1] baz = arr[2]

Slide 41

Slide 41 text

28. De-structuring assignment CoffeeScript Swapping values. foo = 1 bar = 2 [foo, bar] = [bar, foo] # foo = 2 # bar = 1

Slide 42

Slide 42 text

29. De-structuring assignment CoffeeScript With splats. numbers = [1, 2, 3, 4] [head, tail...] = numbers # head = 1 # tail = [2,3,4]

Slide 43

Slide 43 text

30. De-structuring assignment CoffeeScript With objects. person = name: "Joe", age: 39 {name: myName, age: myAge} = person # myName = “Joe” # myAge = 39

Slide 44

Slide 44 text

31. De-structuring assignment CoffeeScript Shorthand when variable and property names are the same. person = name: "Joe", age: 39 {name: name, age: age} = person # the above is shorthand for {name, age} = person # name = “Joe” # age = 39

Slide 45

Slide 45 text

32. this @ is short-hand for this. $("a").on "click", -> alert @.attr("href") # you can omit the dot $("a").on "click", -> alert @attr("href") alert(this.attr("href")) alert(this.attr("href")) CoffeeScript JavaScript @ this

Slide 46

Slide 46 text

33. Classes class Animal tim = new Animal "Tim the turtle" CoffeeScript

Slide 47

Slide 47 text

33b. Classes class Animal constructor: (@name) -> # instance method move: (meters) -> alert "#{@name} moved #{meters}m." tim = new Animal "Tim the turtle" tim.move 10 CoffeeScript

Slide 48

Slide 48 text

33c. Classes class Animal # class method @all: -> @animals ?= [] constructor: (@name) -> Animal.all().push @ # instance method move: (meters) -> alert "#{@name} moved #{meters}m." tim = new Animal "Tim the turtle" tim.move 10 Animal.all() # [Animal instance] Animal.animals # [Animal instance] CoffeeScript

Slide 49

Slide 49 text

xx. Diving Deeper Classes (super, inheritance, etc) Namespaces. Function bindings. Block regular expressions. Embedding JavaScript. Exceptions. Chained comparisons.

Slide 50

Slide 50 text

3 resources

Slide 51

Slide 51 text

Resources http://autotelicum.github.com/Smooth-CoffeeScript/ CoffeeScript%20Quick%20Ref.pdf http://www.coffeescript.org http://arcturo.github.com/library/coffeescript/ Little Book on Coffee Script CoffeeScript Quick Ref Card http://coffeescript.org/#resources