Top Banner
Everybody loves coffee, right?
27

Webmontag Berlin "coffee script"

Jan 12, 2015

Download

Technology

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Webmontag Berlin "coffee script"

Everybody  loves  coffee,  right?  

Page 2: Webmontag Berlin "coffee script"

Today,  Everybody  loves  JavaScript,  right?  

•  JavaScript  revival  •  Node.js  •  One-­‐Page  applica@ons  •  Real@me  applica@ons  

Page 3: Webmontag Berlin "coffee script"

Some  things  I  like  about  CoffeeScript  

Page 4: Webmontag Berlin "coffee script"

1.  FUNCTION  SYNTAX  

Page 5: Webmontag Berlin "coffee script"

1.  FUNCTION  SYNTAX  

function  greet(name)  {    return  ”Hello  ”  +  name  

}  

Page 6: Webmontag Berlin "coffee script"

1.  FUNCTION  SYNTAX  

greet  =  (name)  -­‐>    ”Hello  ”  +  name  

Page 7: Webmontag Berlin "coffee script"

1.  FUNCTION  SYNTAX  

$(”a”).click(function(event)  {    $(this).addClass(”busy”);  

});  

Page 8: Webmontag Berlin "coffee script"

1.  FUNCTION  SYNTAX  

$(”a”).click  (event)  -­‐>    $(this).addClass  ”busy”  

Page 9: Webmontag Berlin "coffee script"

2.  SIGNIFICANT  WHITESPACE  

Page 10: Webmontag Berlin "coffee script"

2.  SIGNIFICANT  WHITESPACE  

if(url)  {    $.get(url,  function(data)  {      return  $(”#result”).html(data);    });  

}  else  {    $(”#error“).show();  

}  

Page 11: Webmontag Berlin "coffee script"

2.  SIGNIFICANT  WHITESPACE  

if  url    $.get  url,  (data)  -­‐>      $(”#result”).html  data  

else    $(”#error“).show()  

Page 12: Webmontag Berlin "coffee script"

2.  SIGNIFICANT  WHITESPACE  

if(url)  {    $.get(url,  function(data)  {      return  $(”#result”).html(data);    });  

}  else  {    $(”#error“).show();  

}  

Page 13: Webmontag Berlin "coffee script"

2.  SIGNIFICANT  WHITESPACE  

var  response  =  function(callback)  {    return  $.get("/data.php",  function(data,  textStatus)  {      if(textStatus  ==  200)  {        return  data.toUpperCase();      }      else  {        return  callback(data);      }    }  

};  

Page 14: Webmontag Berlin "coffee script"

2.  SIGNIFICANT  WHITESPACE  

response  =  (callback)  -­‐>    $.get  "/data.php",  (data,  textStatus)  -­‐>      if  textStatus  ==  200        data.toUpperCase()      else        callback(data)  

Page 15: Webmontag Berlin "coffee script"

3.  BARE  OBJECTS  

var  student  =  {    name:  ”Sebastian”,    age:  21,    hobbies:  [”drums”,  ”programming”]  

}  

Page 16: Webmontag Berlin "coffee script"

3.  BARE  OBJECTS  

student  =    name:  ”Sebastian”    age:  21    hobbies:  [”drums”,  ”programming”]  

Page 17: Webmontag Berlin "coffee script"

4.  COMPREHENSIONS  

var  squares  =  []    for(var  i  =  0;  i  <  100;  i++)  {  

 squares.push(i  *  i);  }  

Page 18: Webmontag Berlin "coffee script"

4.  COMPREHENSIONS  

squares  =  []    for  i  in  [0..100]  

 squares.push  i  *  i  

Page 19: Webmontag Berlin "coffee script"

4.  COMPREHENSIONS  

squares  =  (i  *  i  for  i  in  [0..100])  

Page 20: Webmontag Berlin "coffee script"

4.  COMPREHENSIONS  

squares  =  (i  *  i  for  i  in  [0..100]  when  i  %  2  is  0)  

Page 21: Webmontag Berlin "coffee script"

4.  COMPREHENSIONS  

var  i,  squares;  squares  =  (function()  {      var  _results;      _results  =  [];      for  (i  =  0;  i  <=  100;  i++)  {          if  (i  %  2  ===  0)  {              _results.push(i  *  i);          }      }      return  _results;  })();  

Page 22: Webmontag Berlin "coffee script"

5.  FUNCTION  BINDING  

$(”a”).click(function()  {    $.get(”/data.php”,  (function(data,  responseText)  {      $(this).html(data);    }).bind(this));  

});  

Page 23: Webmontag Berlin "coffee script"

5.  FUNCTION  BINDING  

$(”a”).click  -­‐>    $.get  ”/data.php”,  (data,  responseText)  =>      $(this).html  data  

Page 24: Webmontag Berlin "coffee script"

6.  STRING  SYNTAX  

$(”#elements”).append(    ”<li  class=’entry’>”  +  data.name  +  ”</li>”  

);  

Page 25: Webmontag Berlin "coffee script"

6.  STRING  SYNTAX  

$(”#elements”).append  ”<li  class=’entry’>#{data.name}</li>”  

Page 26: Webmontag Berlin "coffee script"

6.  STRING  SYNTAX  

render  (student)  -­‐>    ”””      <div  class=”student”>        <a  href=”/student/#{student.id}”>          <span>#{student.name}</span>        </a>      </div>    ”””  

Page 27: Webmontag Berlin "coffee script"

There  is  lots  more:  

hHp://coffeescript.org  

Sebas@an  Hoitz  -­‐  @sebas@anhoitz  –  hHp://suplify.me  

Classes,  existen@al  operator,  destructuring  assignment,  …