Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CoffeeScript with Patrick Simpson

Patrick
March 20, 2014

CoffeeScript with Patrick Simpson

An introduction of the benefits of CoffeeScript. A breif explaination of how things used to be, the pain of debugging, and how CoffeeScript has helped. A walkthrough of the beautiful CoffeeScript syntax and examining the lint free JavaSript that it creates. Also, dispelling some of the preceived downsides of CoffeeScript.

Patrick

March 20, 2014
Tweet

More Decks by Patrick

Other Decks in Programming

Transcript

  1. :-(

  2. ,

  3. 1 // A single line comment.! 2 // You can

    have a few.! 3 ! 4 /*! 5 Wow! 6 A Multiline! 7 Comment! 8 Such Amaze! 9 */! Comments: JS
  4. 1 # A single line comment.! 2 # You can

    have a few.! 3 ! 4 ###! 5 Wow! 6 A Multiline! 7 Comment! 8 Such Amaze! 9 ###! ! ! Comments: Coffee
  5. 1 name = "Patrick"! 2 ! 3 bio = "From

    Dayton Ohio ! 4 Married with 3 children. ! 5 Loves Coffee, and drinks it too."! 6 ! 7 html = """! 8 <p>! 9 #{name} had a cup of coffee! 10 </p>! 11 """! Strings: Coffee
  6. 1 // Generated by CoffeeScript 1.6.3! 2 (function() {! 3

    var bio, html, name;! 4 ! 5 name = "Patrick";! 6 ! 7 bio = "From Dayton Ohio Married with 3 children. Loves Coffee, and drinks it too.";! 8 ! 9 html = "<p>\n " + name + " had a cup of coffee\n</p>";! 10 ! 11 }).call(this);! Strings: Coffee
  7. 1 function computeName() {! 2 me = "Patrick Simpson";! 3

    //... Do stuff! 4 return me;! 5 }! Functions: JS
  8. 1 computeName = ->! 2 me = "Patrick Simpson"! 3

    # ... Do stuff! 4 me! Functions: Coffee
  9. 1 computeName = ->! 2 me = "Patrick Simpson"! 3

    # ... Do stuff! 4 me! Functions: Coffee
  10. 2 (function() {! 3 var computeName;! 4 ! 5 computeName

    = function() {! 6 var me;! 7 me = "Patrick Simpson";! 8 return me;! 9 };! 10 ! 11 }).call(this);! Functions: Compiled
  11. 1 computeName = ->! 2 me = "Patrick Simpson"! 3

    # ... Do stuff! 4 me! White Space: Coffee
  12. 1 me = ""! 2 computeName = ->! 3 me

    = "Patrick Simpson"! 4 # ... Do stuff! 5 me! Variables
  13. 2 (function() {! 3 var computeName, me;! 4 ! 5

    me = "";! 6 ! 7 computeName = function() {! 8 me = "Patrick Simpson";! 9 return me;! 10 };! 11 ! 12 }).call(this);! Variables
  14. 1 me = ""! 2 computeName = ->! 3 me

    = "Patrick Simpson"! 4 # ... Do stuff! 5 me! Scoping: Coffee
  15. 2 (function() {! 3 var computeName, me;! 4 ! 5

    me = "";! 6 ! 7 computeName = function() {! 8 me = "Patrick Simpson";! 9 return me;! 10 };! 11 ! 12 }).call(this);! Scoping: Compiled
  16. 2 (function() {! 3 var computeName, me;! 4 ! 5

    me = "";! 6 ! 7 computeName = function() {! 8 me = "Patrick Simpson";! 9 return me;! 10 };! 11 ! 12 }).call(this);! Scoping
  17. 1 var names = ["Patrick",! 2 "Joe",! 3 "Smoe",! 4

    "Barney",! 5 "Batman"];! Arrays: JS
  18. 1 people = [! 2 "Patrick"! 3 "Joe"! 4 "Smoe"!

    5 "Barney"! 6 "Batman"! 7 ]! Arrays: Coffee
  19. 2 (function() {! 3 var people;! 4 ! 5 people

    = ["Patrick", "Joe", "Smoe", "Barney", "Batman"];! 6 ! 7 }).call(this);! Arrays: Compiled
  20. 1 var patrick = {! 2 name: "Patrick Simpson",! 3

    age: 28,! 4 awesome: true,! 5 skills: ["JavaScript", "CoffeeScript", "Bacon"]! 6 };! 7 ! 8 // patrick! 9 console.log(patrick.name);! Objects: JS
  21. 1 patrick =! 2 name: "Patrick Simpson"! 3 age: 28!

    4 awesome: true! 5 skills: [! 6 "JavaScript"! 7 "CoffeeScript"! 8 "Bacon"! 9 ]! 10 ! 11 # patrick! 12 console.log patrick.name! Objects: Coffee
  22. 2 (function() {! 3 var patrick;! 4 ! 5 patrick

    = {! 6 name: "Patrick Simpson",! 7 age: 28,! 8 awesome: true,! 9 skills: ["JavaScript", "CoffeeScript", "Bacon"]! 10 };! 11 ! 12 console.log(patrick.name);! 13 ! 14 }).call(this);! Objects: Compiled
  23. 1 var names = ["Patrick",! 2 "Joe",! 3 "Smoe",! 4

    "Barney",! 5 "Batman"];! Looping: JS
  24. ! 7 for(var i = 0; i < names.length; i++){!

    8 printName(names[i]);! 9 }! Looping: JS
  25. 7 for (_i = 0, _len = names.length; _i <

    _len; _i++) {! 8 name = names[_i];! 9 prettyPrint(name);! 10 }! Loops: Compiled
  26. 1 names = [! 2 "Patrick"! 3 "Joe"! 4 "Smoe"!

    5 "Barney"! 6 "Batman"! 7 ]! 8 printName(name) for name in names! 9 me = (name for name in names when name is "Patrick")! 10 doOtherThing(name) for name in names! Loops: Coffee
  27. 1 // Generated by CoffeeScript 1.6.3! 2 (function() {! 3

    var me, name, names, _i, _j, _len, _len1;! 4 ! 5 names = ["Patrick", "Joe", "Smoe", "Barney", "Batman"];! 6 ! 7 for (_i = 0, _len = names.length; _i < _len; _i++) {! 8 name = names[_i];! 9 prettyPrint(name);! 10 }! 11 ! 12 me = (function() {! 13 var _j, _len1, _results;! 14 _results = [];! 15 for (_j = 0, _len1 = names.length; _j < _len1; _j++) {! 16 name = names[_j];! 17 if (name === "Patrick") {! 18 _results.push(name);! 19 }! 20 }! 21 return _results;! 22 })();! 23 ! 24 for (_j = 0, _len1 = names.length; _j < _len1; _j++) {! 25 name = names[_j];! 26 doOtherThing(name);! 27 }! 28 ! 29 }).call(this);!
  28. 1 names = [! 2 "Patrick"! 3 "Joe"! 4 "Smoe"!

    5 "Barney"! 6 "Batman"! 7 ]! 8 ! 9 prettyPrint = (string) ->! 10 console.log "~~~ " + string + " ~~~"! 11 ! 12 modNames = names.map (name) ->! 13 # I can now do what I want with this current name...! 14 if name == "Patrick"! 15 prettyPrint name! 16 name = "Patrick Simpson"! 17 name! 18 ! 19 console.log names! 20 console.log modNames! Loop: Maps
  29. 1 // Generated by CoffeeScript 1.6.3! 2 (function() {! 3

    var modNames, names, prettyPrint;! 4 ! 5 names = ["Patrick", "Joe", "Smoe", "Barney", "Batman"];! 6 ! 7 prettyPrint = function(string) {! 8 return console.log("~~~ " + string + " ~~~");! 9 };! 10 ! 11 modNames = names.map(function(name) {! 12 if (name === "Patrick") {! 13 prettyPrint(name);! 14 name = "Patrick Simpson";! 15 }! 16 return name;! 17 });! 18 ! 19 console.log(names);! 20 ! 21 console.log(modNames);! 22 ! 23 }).call(this);!
  30. 1 class Person! 2 constructor: (@name) ->! 3 ! 4

    drink: (n) ->! 5 console.log @name + " drank #{n} cups"! 6 ! 7 class Employee extends Person! 8 drink: (n) ->! 9 super 10! 10 console.log " of coffee"! 11 ! 12 patrick = new Employee "Patrick Simpson"! Classes
  31. 1 (function() {! 2 var Employee, Person, patrick, _ref,! 3

    __hasProp = {}.hasOwnProperty,! 4 __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; };! 5 ! 6 Person = (function() {! 7 function Person(name) {! 8 this.name = name;! 9 }! 10 ! 11 Person.prototype.drink = function(n) {! 12 return console.log(this.name + (" drank " + n + " cups"));! 13 };! 14 ! 15 return Person;! 16 ! 17 })();! 18
  32. ! 19 Employee = (function(_super) {! 20 __extends(Employee, _super);! 21

    ! 22 function Employee() {! 23 _ref = Employee.__super__.constructor.apply(this, arguments);! 24 return _ref;! 25 }! 26 ! 27 Employee.prototype.drink = function(n) {! 28 Employee.__super__.drink.call(this, 10);! 29 return console.log(" of coffee");! 30 };! 31 ! 32 return Employee;! 33 ! 34 })(Person);! 35 ! 36 patrick = new Employee("Patrick Simpson");! 37 ! 38 patrick.drink();! 39 ! 40 }).call(this);!
  33. http://coffeescriptcookbook.com/ ! http://arcturo.github.io/library/coffeescript/ ! http://coffeescript.org/ ! https://github.com/raganwald/homoiconic/blob/master/2012/09/lexical-scope-in- coffeescript.md ! https://github.com/raganwald/homoiconic/blob/master/2012/09/actually-YOU-

    dont-understand-lexical-scope.md ! http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/ ! https://github.com/sparkbox/generator-mg ! https://github.com/mutewinter/tapas-with-ember !