Slide 1

Slide 1 text

Let’s Have a Cup of

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

JavaScript

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

JavaScript

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

me = { name: “Nicolás”, location: “Montevideo, Uruguay”, age: 26, likesWorkingOn: [“Ruby”, “JavaScript”], twitter: “@godfoca”, github: “http://github.com/foca”, url: “http://nicolassanguinetti.info”, company: { name: “Cubox”, url: “http://cuboxsa.com” } }

Slide 8

Slide 8 text

me = { name: “Nicolás”, location: “Montevideo, Uruguay age: 26, likesWorkingOn: [“Ruby”, “Java twitter: “@godfoca”, github: “http://github.com/foc url: “http://nicolassanguinett company: { name: “Cubox”, url: “http://cuboxsa.com”

Slide 9

Slide 9 text

me = { name: “Nicolás”, location: “Montevid age: 26, likesWorkingOn: [“R twitter: “@godfoca” github: “http://git

Slide 10

Slide 10 text

me = { name: “Nicolás” location: “Mont age: 26, likesWorkingOn: twitter: “@godf

Slide 11

Slide 11 text

= { name: “Nico location: “ age: 26, me There’s no var

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

me = { name: “Nicolás”, location: “Montevideo, Uruguay”, age: 26, likesWorkingOn: [“Ruby”, “JavaScript”], twitter: “@godfoca”, github: “http://github.com/foca”, url: “http://nicolassanguinetti.info”, company: { name: “Cubox”, url: “http://cuboxsa.com” } }

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

me = { name: “Nicolás”, location: “Montevideo, Uruguay”, age: 26, likesWorkingOn: [“Ruby”, “JavaScript”], twitter: “@godfoca”, github: “http://github.com/foca”, url: “http://nicolassanguinetti.info”, company: { name: “Cubox”, url: “http://cuboxsa.com” } }

Slide 16

Slide 16 text

me = name: “Nicolás”, location: “Montevideo, Uruguay”, age: 26, likesWorkingOn: [“Ruby”, “JavaScript”], twitter: “@godfoca”, github: “http://github.com/foca”, url: “http://nicolassanguinetti.info”, company: name: “Cubox”, url: “http://cuboxsa.com”

Slide 17

Slide 17 text

me = name: “Nicolás”, location: “Montevideo, Uruguay”, age: 26, likesWorkingOn: [“Ruby”, “JavaScript”], twitter: “@godfoca”, github: “http://github.com/foca”, url: “http://nicolassanguinetti.info”, company: name: “Cubox”, url: “http://cuboxsa.com”

Slide 18

Slide 18 text

me = name: “Nicolás”, location: “Montevideo, Uruguay”, age: 26, likesWorkingOn: [“Ruby”, “JavaScript”], twitter: “@godfoca”, github: “http://github.com/foca”, url: “http://nicolassanguinetti.info”, company: name: “Cubox”, url: “http://cuboxsa.com”

Slide 19

Slide 19 text

me = name: “Nicolás” location: “Montevideo, Uruguay” age: 26 likesWorkingOn: [“Ruby”, “JavaScript”] twitter: “@godfoca” github: “http://github.com/foca” url: “http://nicolassanguinetti.info” company: name: “Cubox” url: “http://cuboxsa.com”

Slide 20

Slide 20 text

me = name: “Nicolás” location: “Montevideo, Uruguay” age: 26 likesWorkingOn: [“Ruby”, “JavaScript”] twitter: “@godfoca” github: “http://github.com/foca” url: “http://nicolassanguinetti.info” company: name: “Cubox” url: “http://cuboxsa.com”

Slide 21

Slide 21 text

Let’s Have a Cup of

Slide 22

Slide 22 text

Let’s Have a Cup (or six) of

Slide 23

Slide 23 text

1st Cup A simple comparison

Slide 24

Slide 24 text

var square = function(num) { return num * num; } var list = [1, 2, 3, 4, 5]; var squares = []; for (var i = 0; i < list.length; i++) { squares.push(square(list[i])); } 1st Cup: A simple comparison

Slide 25

Slide 25 text

var square = function(num) { return num * num; } var list = [1, 2, 3, 4, 5]; var squares = []; for (var i = 0; i < list.length; i++) { squares.push(square(list[i])); } 1st Cup: A simple comparison

Slide 26

Slide 26 text

var square = function(num) { return num * num; } var list = [1, 2, 3, 4, 5]; var squares = []; for (var i = 0; i < list.length; i++) { squares.push(square(list[i])); } 1st Cup: A simple comparison

Slide 27

Slide 27 text

var square = function(num) { return num * num; } var list = [1, 2, 3, 4, 5]; var squares = []; for (var i = 0; i < list.length; i++) { squares.push(square(list[i])); } 1st Cup: A simple comparison

Slide 28

Slide 28 text

var square = function(num) { return num * num; } var list = [1, 2, 3, 4, 5]; var squares = []; for (var i = 0; i < list.length; i++) { squares.push(square(list[i])); } 1st Cup: A simple comparison

Slide 29

Slide 29 text

square = (num) -> num * num list = [1..5] squares = (square n for n in list) 1st Cup: A simple comparison

Slide 30

Slide 30 text

square = (num) -> num * num list = [1..5] squares = (square n for n in list) 1st Cup: A simple comparison

Slide 31

Slide 31 text

square = (num) -> num * num list = [1..5] squares = (square n for n in list) 1st Cup: A simple comparison

Slide 32

Slide 32 text

square = (num) -> num * num list = [1..5] squares = (square n for n in list) 1st Cup: A simple comparison

Slide 33

Slide 33 text

square = (num) -> num * num list = [1..5] squares = (square n for n in list) 1st Cup: A simple comparison

Slide 34

Slide 34 text

2nd Cup The Useless Calculator

Slide 35

Slide 35 text

var add = function(x, y) { return x + “ + “ + y + “ = “ + (x + y); } console.log(add(2, 3)) > 2 + 3 = 5 console.log(add(5, 7)) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 36

Slide 36 text

var add = function(x, y) { return x + “ + “ + y + “ = “ + (x + y); } console.log(add(2, 3)) > 2 + 3 = 5 console.log(add(5, 7)) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 37

Slide 37 text

var add = function(x, y) { return x + “ + “ + y + “ = “ + (x + y); } console.log(add(2, 3)) > 2 + 3 = 5 console.log(add(5, 7)) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 38

Slide 38 text

var add = function(x, y) { return x + “ + “ + y + “ = “ + (x + y); } console.log(add(2, 3)) > 2 + 3 = 5 console.log(add(5, 7)) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 39

Slide 39 text

var add = function(x, y) { return x + “ + “ + y + “ = “ + (x + y); } console.log(add(2, 3)) > 2 + 3 = 5 console.log(add(5, 7)) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 40

Slide 40 text

add = (x, y) -> “#{x} + #{y} = #{x + y}” console.log add(2, 3) > 2 + 3 = 5 console.log add(5, 7) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 41

Slide 41 text

add = (x, y) -> “#{x} + #{y} = #{x + y}” console.log add(2, 3) > 2 + 3 = 5 console.log add(5, 7) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 42

Slide 42 text

add = (x, y) -> “#{x} + #{y} = #{x + y}” console.log add(2, 3) > 2 + 3 = 5 console.log add(5, 7) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 43

Slide 43 text

add = (x, y) -> “#{x} + #{y} = #{x + y}” console.log add(2, 3) > 2 + 3 = 5 console.log add(5, 7) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 44

Slide 44 text

add = (x, y) -> “#{x} + #{y} = #{x + y}” console.log add(2, 3) > 2 + 3 = 5 console.log add(5, 7) > 5 + 7 = 12 2nd Cup: The Useless Calculator

Slide 45

Slide 45 text

3rd Cup Your Favorite Language

Slide 46

Slide 46 text

languageOfChoice = (lang) -> “#{lang} rocks!” 3rd Cup: Your Favorite Language

Slide 47

Slide 47 text

languageOfChoice = (lang) -> “#{lang} rocks!” 3rd Cup: Your Favorite Language

Slide 48

Slide 48 text

languageOfChoice = (lang) -> “#{lang} rocks!” 3rd Cup: Your Favorite Language

Slide 49

Slide 49 text

languageOfChoice = (lang) -> “#{lang} rocks!” 3rd Cup: Your Favorite Language

Slide 50

Slide 50 text

languageOfChoice = (lang) -> “#{lang} rocks!” console.log languageOfChoice(“Ruby”) > Ruby rocks! 3rd Cup: Your Favorite Language

Slide 51

Slide 51 text

languageOfChoice = (lang) -> “#{lang} rocks!” Implicit Return 3rd Cup: Your Favorite Language

Slide 52

Slide 52 text

languageOfChoice = (lang) -> return “#{lang} rocks!” Implicit Return 3rd Cup: Your Favorite Language

Slide 53

Slide 53 text

languageOfChoice = (lang) -> if lang == “PHP” “Get outta here!” else “#{lang} rocks!” Implicit Return 3rd Cup: Your Favorite Language

Slide 54

Slide 54 text

languageOfChoice = (lang) -> if lang == “PHP” return “Get outta here!” else return “#{lang} rocks!” Implicit Return 3rd Cup: Your Favorite Language

Slide 55

Slide 55 text

Default Arguments languageOfChoice = (lang = “Ruby”) -> “#{lang} rocks!” 3rd Cup: Your Favorite Language

Slide 56

Slide 56 text

languageOfChoice = (lang = “Ruby”) -> “#{lang} rocks!” console.log languageOfChoice(“Lua”) > Lua rocks! Default Arguments 3rd Cup: Your Favorite Language

Slide 57

Slide 57 text

languageOfChoice = (lang = “Ruby”) -> “#{lang} rocks!” console.log languageOfChoice(“Lua”) > Lua rocks! console.log languageOfChoice() > Ruby rocks! Default Arguments 3rd Cup: Your Favorite Language

Slide 58

Slide 58 text

Optional Parentheses languageOfChoice = (lang = “Ruby”) -> “#{lang} rocks!” 3rd Cup: Your Favorite Language

Slide 59

Slide 59 text

languageOfChoice = (lang = “Ruby”) -> “#{lang} rocks!” console.log languageOfChoice “Lua” > Lua rocks! Optional Parentheses 3rd Cup: Your Favorite Language

Slide 60

Slide 60 text

languageOfChoice = (lang = “Ruby”) -> “#{lang} rocks!” console.log languageOfChoice “Lua” > Lua rocks! console.log languageOfChoice Optional Parentheses 3rd Cup: Your Favorite Language

Slide 61

Slide 61 text

languageOfChoice = (lang = “Ruby”) -> “#{lang} rocks!” console.log languageOfChoice “Lua” > Lua rocks! console.log languageOfChoice > [Function] Optional Parentheses 3rd Cup: Your Favorite Language

Slide 62

Slide 62 text

languageOfChoice = (lang = “Ruby”) -> “#{lang} rocks!” console.log languageOfChoice “Lua” > Lua rocks! console.log languageOfChoice > [Function] console.log languageOfChoice() > Ruby rocks! Optional Parentheses 3rd Cup: Your Favorite Language

Slide 63

Slide 63 text

Splats languagesOfChoice = (langs...) -> “I like #{langs.join(“, “)}” 3rd Cup: Your Favorite Language

Slide 64

Slide 64 text

Splats languagesOfChoice = (langs...) -> “I like #{langs.join(“, “)}” languagesOfChoice “Ruby”, “JS”, “Python” > I like Ruby, JS, Python 3rd Cup: Your Favorite Language

Slide 65

Slide 65 text

Splats myLangs = [“Ruby”, “JS”, “Python”] 3rd Cup: Your Favorite Language

Slide 66

Slide 66 text

Splats myLangs = [“Ruby”, “JS”, “Python”] languagesOfChoice myLangs... > I like Ruby, JS, Python 3rd Cup: Your Favorite Language

Slide 67

Slide 67 text

Splats languagesOfChoice = (best, langs...) -> “I love #{best}, and I like #{langs.join(“, “)}” 3rd Cup: Your Favorite Language

Slide 68

Slide 68 text

Splats languagesOfChoice = (best, langs...) -> “I love #{best}, and I like #{langs.join(“, “)}” languagesOfChoice “Ruby”, “JS”, “Python” > I love Ruby, and I like JS, Python 3rd Cup: Your Favorite Language

Slide 69

Slide 69 text

Splats languagesOfChoice = (langs..., worst) -> “I like #{langs.join(“, “)}, but I hate #{worst}” 3rd Cup: Your Favorite Language

Slide 70

Slide 70 text

Splats languagesOfChoice = (langs..., worst) -> “I like #{langs.join(“, “)}, but I hate #{worst}” languagesOfChoice “Ruby”, “JS”, “PHP” > I like Ruby, JS, but I hate PHP 3rd Cup: Your Favorite Language

Slide 71

Slide 71 text

4th Cup Getting Inside Your Objects

Slide 72

Slide 72 text

list = [“Ruby”, “JS”, “Python”] 4th Cup: Getting Inside Your Objects

Slide 73

Slide 73 text

list = [“Ruby”, “JS”, “Python”] first_two = list[0..1] 4th Cup: Getting Inside Your Objects Tasty Slices

Slide 74

Slide 74 text

list = [“Ruby”, “JS”, “Python”] first_two = list[0..1] first_two #=> [“Ruby”, “JS”] 4th Cup: Getting Inside Your Objects Tasty Slices

Slide 75

Slide 75 text

list = [“Ruby”, “JS”, “Python”] last_two = list[1..-1] 4th Cup: Getting Inside Your Objects Tasty Slices

Slide 76

Slide 76 text

list = [“Ruby”, “JS”, “Python”] last_two = list[1..-1] last_two #=> [“JS”, “Python”] 4th Cup: Getting Inside Your Objects Tasty Slices

Slide 77

Slide 77 text

list = [“Ruby”, “JS”, “Python”] 4th Cup: Getting Inside Your Objects

Slide 78

Slide 78 text

list = [“Ruby”, “JS”, “Python”] Head Tail 4th Cup: Getting Inside Your Objects

Slide 79

Slide 79 text

list = [“Ruby”, “JS”, “Python”] head = list[0] tail = list[1..-1] 4th Cup: Getting Inside Your Objects

Slide 80

Slide 80 text

list = [“Ruby”, “JS”, “Python”] [head, tail] = [list[0], list[1..-1]] Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 81

Slide 81 text

list = [“Ruby”, “JS”, “Python”] [head, tail] = [list[0], list[1..-1]] Pattern Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 82

Slide 82 text

list = [“Ruby”, “JS”, “Python”] [head, tail] = [list[0], list[1..-1]] Values Pattern Matching 4th Cup: Getting Inside Your Objects Pattern

Slide 83

Slide 83 text

list = [“Ruby”, “JS”, “Python”] [head, tail] = [list[0], list[1..-1]] Values Pattern Matching 4th Cup: Getting Inside Your Objects Variables

Slide 84

Slide 84 text

list = [“Ruby”, “JS”, “Python”] [head, tail] = [list[0], list[1..-1]] Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 85

Slide 85 text

list = [“Ruby”, “JS”, “Python”] [head, tail] = [list[0], list[1..-1]] Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 86

Slide 86 text

list = [“Ruby”, “JS”, “Python”] [head, tail] = [list[0], list[1..-1]] head #=> “Ruby” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 87

Slide 87 text

list = [“Ruby”, “JS”, “Python”] [head, tail] = [list[0], list[1..-1]] head #=> “Ruby” tail #=> [“JS”, “Python”] Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 88

Slide 88 text

list = [“Ruby”, “JS”, “Python”] [head, tail...] = list Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 89

Slide 89 text

list = [“Ruby”, “JS”, “Python”] [head, tail...] = list Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 90

Slide 90 text

list = [“Ruby”, “JS”, “Python”] [head, tail...] = list head #=> “Ruby” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 91

Slide 91 text

list = [“Ruby”, “JS”, “Python”] [head, tail...] = list head #=> “Ruby” tail #=> [“JS”, “Python”] Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 92

Slide 92 text

list = [“Ruby”, “JS”, “Python”] [top, rest..., bottom] = list Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 93

Slide 93 text

list = [“Ruby”, “JS”, “Python”] [top, rest..., bottom] = list top #=> “Ruby” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 94

Slide 94 text

list = [“Ruby”, “JS”, “Python”] [top, rest..., bottom] = list top #=> “Ruby” bottom #=> “Python” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 95

Slide 95 text

list = [“Ruby”, “JS”, “Python”] [top, rest..., bottom] = list top #=> “Ruby” bottom #=> “Python” rest #=> [“JS”] Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 96

Slide 96 text

list = [“Ruby”, “JS”] [top, rest..., bottom] = list Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 97

Slide 97 text

list = [“Ruby”, “JS”] [top, rest..., bottom] = list top #=> “Ruby” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 98

Slide 98 text

list = [“Ruby”, “JS”] [top, rest..., bottom] = list top #=> “Ruby” bottom #=> “JS” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 99

Slide 99 text

list = [“Ruby”, “JS”] [top, rest..., bottom] = list top #=> “Ruby” bottom #=> “JS” rest #=> [] Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 100

Slide 100 text

me = name: “Nicolás” company: “Cubox” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 101

Slide 101 text

me = name: “Nicolás” company: “Cubox” name = me.name company = me.company Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 102

Slide 102 text

me = name: “Nicolás” company: “Cubox” { name: name, company: company } = me Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 103

Slide 103 text

me = name: “Nicolás” company: “Cubox” { name: name, company: company } = me Pattern Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 104

Slide 104 text

Pattern Matching 4th Cup: Getting Inside Your Objects me = name: “Nicolás” company: “Cubox” { name: name, company: company } = me Variables

Slide 105

Slide 105 text

me = name: “Nicolás” company: “Cubox” { name: name, company: company } = me Values Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 106

Slide 106 text

me = name: “Nicolás” company: “Cubox” { name: name, company: company } = me name #=> “Nicolás” company #=> “Cubox” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 107

Slide 107 text

{ name, company } = me Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 108

Slide 108 text

{ name, company } = me name #=> “Nicolás” Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 109

Slide 109 text

{ name, company } = me name #=> “Nicolás” company #=> 26 Pattern Matching 4th Cup: Getting Inside Your Objects

Slide 110

Slide 110 text

5th Cup Collecting Collections

Slide 111

Slide 111 text

list = [1..10] for item in list doSomethingWith(item) 5th Cup: Collecting Collections Iterating over arrays

Slide 112

Slide 112 text

list = [1..10] for item in list doSomethingWith(item) Iterating over arrays 5th Cup: Collecting Collections

Slide 113

Slide 113 text

me = name: “Nicolás”, age: 26 for key, value of me console.log “#{key}: #{value}” Iterating over objects 5th Cup: Collecting Collections

Slide 114

Slide 114 text

me = name: “Nicolás”, age: 26 for key, value of me console.log “#{key}: #{value}” Iterating over objects 5th Cup: Collecting Collections

Slide 115

Slide 115 text

doubles = (n * 2 for n in [1..5]) 5th Cup: Collecting Collections Comprehensions

Slide 116

Slide 116 text

n * 2 for n in [1..5] when n > 2 Filtering Comprehensions 5th Cup: Collecting Collections

Slide 117

Slide 117 text

n * 2 for n in [1..5] when n > 2 Filtering Comprehensions 5th Cup: Collecting Collections

Slide 118

Slide 118 text

n * 2 for n in [1..5] when n > 2 for n in [1..5] continue unless n > 2 n * 2 Filtering Comprehensions 5th Cup: Collecting Collections

Slide 119

Slide 119 text

n * 2 for n in [1..5] when n > 2 for n in [1..5] continue unless n > 2 n * 2 Filtering Comprehensions 5th Cup: Collecting Collections

Slide 120

Slide 120 text

6th Cup Context, the classy way

Slide 121

Slide 121 text

User = (id, name) -> this.id = id this.name = name this.element = $(“#user-#{this.id}”) 6th Cup: Context, the classy way

Slide 122

Slide 122 text

Context Accessor 6th Cup: Context, the classy way User = (id, name) -> this.id = id this.name = name this.element = $(“#user-#{this.id}”)

Slide 123

Slide 123 text

Context Accessor 6th Cup: Context, the classy way User = (id, name) -> this.id = id this.name = name this.element = $(“#user-#{this.id}”)

Slide 124

Slide 124 text

Context Accessor 6th Cup: Context, the classy way User = (id, name) -> this.id = id this.name = name this.element = $(“#user-#{this.id}”)

Slide 125

Slide 125 text

Context Accessor 6th Cup: Context, the classy way User = (id, name) -> this.id = id this.name = name this.element = $(“#user-#{this.id}”)

Slide 126

Slide 126 text

User = (id, name) -> @id = id @name = name @element = $(“#user-#{@id}”) Context Accessor 6th Cup: Context, the classy way

Slide 127

Slide 127 text

Property Arguments 6th Cup: Context, the classy way User = (id, name) -> @id = id @name = name @element = $(“#user-#{@id}”)

Slide 128

Slide 128 text

User = (id, name) -> @id = id @name = name @element = $(“#user-#{@id}”) Property Arguments 6th Cup: Context, the classy way

Slide 129

Slide 129 text

Property Arguments 6th Cup: Context, the classy way User = (@id, @name) -> @element = $(“#user-#{@id}”)

Slide 130

Slide 130 text

Property Arguments 6th Cup: Context, the classy way User = (@id, @name) -> @element = $(“#user-#{@id}”)

Slide 131

Slide 131 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) Property Arguments 6th Cup: Context, the classy way

Slide 132

Slide 132 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) 6th Cup: Context, the classy way

Slide 133

Slide 133 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 134

Slide 134 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 135

Slide 135 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 136

Slide 136 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) $(“a.sign-out”, @element).click -> this.signOut() false User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 137

Slide 137 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) $(“a.sign-out”, @element).click -> this.signOut() false User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 138

Slide 138 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) $(“a.sign-out”, @element).click -> this.signOut() false User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 139

Slide 139 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) self = this $(“a.sign-out”, @element).click -> self.signOut() false User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 140

Slide 140 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) self = this $(“a.sign-out”, @element).click -> self.signOut() false User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 141

Slide 141 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) $(“a.sign-out”, @element).click => this.signOut() false User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 142

Slide 142 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) $(“a.sign-out”, @element).click => this.signOut() false User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 143

Slide 143 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) $(“a.sign-out”, @element).click => this.signOut() false User.prototype.signOut = -> $.ajax(...) Bound Functions 6th Cup: Context, the classy way

Slide 144

Slide 144 text

User = (@id, @name) -> @element = $(“#user-#{@id}”) User.prototype.signOut = -> ... 6th Cup: Context, the classy way

Slide 145

Slide 145 text

class User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ... 6th Cup: Context, the classy way Classes

Slide 146

Slide 146 text

6th Cup: Context, the classy way Classes class User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ...

Slide 147

Slide 147 text

6th Cup: Context, the classy way Classes class User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ...

Slide 148

Slide 148 text

6th Cup: Context, the classy way Classes class User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ...

Slide 149

Slide 149 text

6th Cup: Context, the classy way Inheritance class User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ...

Slide 150

Slide 150 text

6th Cup: Context, the classy way Inheritance class SignedInUser extends User ... class SignedOutUser extends User ...

Slide 151

Slide 151 text

6th Cup: Context, the classy way super class SignedInUser extends User constructor: (args...) -> super args... $(“a.sign-out”, @element).click => this.signOut()

Slide 152

Slide 152 text

class User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ... 6th Cup: Context, the classy way On variable locality

Slide 153

Slide 153 text

class window.User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ... 6th Cup: Context, the classy way On variable locality

Slide 154

Slide 154 text

class window.User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ... 6th Cup: Context, the classy way On variable locality

Slide 155

Slide 155 text

class this.User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ... 6th Cup: Context, the classy way On variable locality

Slide 156

Slide 156 text

class @User constructor: (@id, @name) -> @element = $(“#user-#{@id}”) signOut: -> ... 6th Cup: Context, the classy way On variable locality

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

We’ve Covered ➡ Object Literals ➡ String Interpolation ➡ Functions ➡ Default Arguments ➡ Splats ➡ Ranges ➡ Pattern Matching ➡ Context and this ➡ Property Accessors ➡ Bound Functions ➡ Classes ➡ Inheritance ➡ super

Slide 159

Slide 159 text

So…

Slide 160

Slide 160 text

…is it worth it?

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

No content

Slide 163

Slide 163 text

Questions?

Slide 164

Slide 164 text

Thanks!

Slide 165

Slide 165 text

Thanks! http://github.com/foca @godfoca

Slide 166

Slide 166 text

Thanks! http://github.com/foca @godfoca [email protected]

Slide 167

Slide 167 text

No content

Slide 168

Slide 168 text

Follow-up: Existential Operator

Slide 169

Slide 169 text

languagesOfChoice = (best, langs...) -> “I love #{best}, and I like #{langs.join(“, “)}” Follow-up: Existential Operator

Slide 170

Slide 170 text

languagesOfChoice = (best, langs...) -> “I love #{best}, and I like #{langs.join(“, “)}” languagesOfChoice “Ruby” > I love Ruby, and I like Follow-up: Existential Operator

Slide 171

Slide 171 text

languagesOfChoice = (best, langs...) -> “I love #{best}, and I like #{langs.join(“, “)}” languagesOfChoice “Ruby” > I love Ruby, and I like languagesOfChoice() > I love , and I like Follow-up: Existential Operator

Slide 172

Slide 172 text

Follow-up: Existential Operator languagesOfChoice = (best, langs...) -> ret = [] if best? ret.push “I love #{best}” if langs.length > 0 ret.push “I like #{langs.join(“, “)}” ret.join(“, and “)

Slide 173

Slide 173 text

languagesOfChoice = (best, langs...) -> ret = [] if best? ret.push “I love #{best}” if langs.length > 0 ret.push “I like #{langs.join(“, “)}” ret.join(“, and “) Follow-up: Existential Operator

Slide 174

Slide 174 text

languagesOfChoice = (best, langs...) -> ret = [] if best? ret.push “I love #{best}” if langs.length > 0 ret.push “I like #{langs.join(“, “)}” ret.join(“, and “) Follow-up: Existential Operator

Slide 175

Slide 175 text

languagesOfChoice = (best, langs...) -> ret = [] if best? ret.push “I love #{best}” if langs.length > 0 ret.push “I like #{langs.join(“, “)}” ret.join(“, and “) Follow-up: Existential Operator

Slide 176

Slide 176 text

languagesOfChoice = (best, langs...) -> ret = [] if best? ret.push “I love #{best}” if langs.length > 0 ret.push “I like #{langs.join(“, “)}” ret.join(“, and “) Follow-up: Existential Operator

Slide 177

Slide 177 text

languagesOfChoice “Ruby”, “JS”, “Python” > I love Ruby, and I like JS, Python Follow-up: Existential Operator

Slide 178

Slide 178 text

languagesOfChoice “Ruby”, “JS”, “Python” > I love Ruby, and I like JS, Python languagesOfChoice “Ruby” > I love Ruby Follow-up: Existential Operator

Slide 179

Slide 179 text

languagesOfChoice “Ruby”, “JS”, “Python” > I love Ruby, and I like JS, Python languagesOfChoice “Ruby” > I love Ruby languagesOfChoice undefined, “Ruby”, “JS” > I like Ruby, JS Follow-up: Existential Operator

Slide 180

Slide 180 text

languagesOfChoice “Ruby”, “JS”, “Python” > I love Ruby, and I like JS, Python languagesOfChoice “Ruby” > I love Ruby languagesOfChoice undefined, “Ruby”, “JS” > I like Ruby, JS languagesOfChoice() > “” Follow-up: Existential Operator

Slide 181

Slide 181 text

expr? Follow-up: Existential Operator

Slide 182

Slide 182 text

expr? typeof expr !== undefined && expr !== null Follow-up: Existential Operator

Slide 183

Slide 183 text

x ?= y Follow-up: Existential Operator

Slide 184

Slide 184 text

x ?= y x = y unless x? Follow-up: Existential Operator

Slide 185

Slide 185 text

obj = x: { y: 2 } Follow-up: Existential Operator

Slide 186

Slide 186 text

obj = x: { y: 2 } obj.x?.y Follow-up: Existential Operator

Slide 187

Slide 187 text

obj = x: { y: 2 } obj.x?.y if obj.x? then obj.x.y else undefined Follow-up: Existential Operator

Slide 188

Slide 188 text

obj = x: { y: 2 } obj.x?.y if obj.x? then obj.x.y else undefined Follow-up: Existential Operator

Slide 189

Slide 189 text

obj = x: { y: 2 } obj.x?.y if obj.x? then obj.x.y else undefined Follow-up: Existential Operator

Slide 190

Slide 190 text

obj = x: { y: 2 } obj.x?.y if obj.x? then obj.x.y else undefined Follow-up: Existential Operator

Slide 191

Slide 191 text

func?() Follow-up: Existential Operator

Slide 192

Slide 192 text

func?() func() if typeof func == “function” Follow-up: Existential Operator

Slide 193

Slide 193 text

options.callback?() Follow-up: Existential Operator

Slide 194

Slide 194 text

Follow-up: this

Slide 195

Slide 195 text

function isCute() { return this.cute; } Follow-up: this

Slide 196

Slide 196 text

var cuteKitty = { cute: true, isCute: isCute }; Follow-up: this

Slide 197

Slide 197 text

var meanKitty = { cute: false, isCute: isCute }; Follow-up: this

Slide 198

Slide 198 text

meanKitty.isCute() cuteKitty.isCute() Follow-up: this

Slide 199

Slide 199 text

cuteKitty.isCute() true meanKitty.isCute() Follow-up: this

Slide 200

Slide 200 text

cuteKitty.isCute() true meanKitty.isCute() false Follow-up: this

Slide 201

Slide 201 text

function whatHappensNow() { console.log(this); } whatHappensNow() Follow-up: this

Slide 202

Slide 202 text

function whatHappensNow() { console.log(this); } whatHappensNow() > [window] Follow-up: this

Slide 203

Slide 203 text

$(“a.sign-out”).click(function() { console.log(this); }); Follow-up: this

Slide 204

Slide 204 text

$(“a.sign-out”).click(function() { console.log(this); }); Follow-up: this

Slide 205

Slide 205 text

$(“a.sign-out”).click(function() { console.log(this); }); Delete Follow-up: this

Slide 206

Slide 206 text

function returnThis() { return this; } Follow-up: this

Slide 207

Slide 207 text

function returnThis() { return this; } returnThis.apply(42); //=> 42 Follow-up: this

Slide 208

Slide 208 text

function returnThis() { return this; } returnThis.apply(42); //=> 42 returnThis.apply(“hi”); //=> “hi” Follow-up: this

Slide 209

Slide 209 text

Follow-up: Loop closures and do

Slide 210

Slide 210 text

for i in [1..5] console.log i Follow-up: Loop closures and do

Slide 211

Slide 211 text

for i in [1..5] setTimeout (-> console.log i), 100 Follow-up: Loop closures and do

Slide 212

Slide 212 text

for i in [1..5] setTimeout (-> console.log i), 100 > 5 > 5 > 5 > 5 > 5 Follow-up: Loop closures and do

Slide 213

Slide 213 text

No content

Slide 214

Slide 214 text

for i in [1..5] setTimeout (-> console.log i), 100 > 5 > 5 > 5 > 5 > 5 Follow-up: Loop closures and do

Slide 215

Slide 215 text

for i in [1..5] ((i) -> setTimeout (-> console.log i), 100 )(i) > 1 > 2 > ... Follow-up: Loop closures and do

Slide 216

Slide 216 text

for i in [1..5] do (i) -> setTimeout (-> console.log i), 100 > 1 > 2 > ... Follow-up: Loop closures and do

Slide 217

Slide 217 text

Follow-up: Comprehension Madness

Slide 218

Slide 218 text

A = [1, 2, 3, 4, 5] B = [8, 7, 1, 2] cartesian = ([x, y] for x in A for y in B) Keep It Simple, Stupid Follow-up: Comprehension Madness

Slide 219

Slide 219 text

(ret.push(val) unless val in ret) for val in orig when shouldAdd(val) Keep It Simple, Stupid Follow-up: Comprehension Madness

Slide 220

Slide 220 text

No content