SlideShare a Scribd company logo
1 of 30
Download to read offline
Who am I?
Who am I?
• A software engineer and entrepreneur
• On the tablet team at Kobo
• Find me blogging at FaisalAbid.com
• Tweeting @FaisalAbid
A few things I’ve worked on
101
History of Javascript
The bad parts
• Global Variables
• operator overloading in a dynamic typed language
• semicolon optional
• typeof inconsistency. null = object?!
• ==,===, !=
• false, null, undefined, NaN
Javascript also has good parts
Being the good guy is hard in Javascript
Would you like a cup of coffee?
Not the first of its kind.
GWT Output
function A0(){this.B0();return this.js[0];}
function C0(){if(this.js.length > 1)
{this.js = [this.js.join('')];this.length = this.js[0].length;}}
function D0(E0){this.js = [E0];this.length = E0.length;}
function Ez(F0,a1){return F0.yx(yZ(a1));}
function yB(b1){c1(b1);return b1;}
function c1(d1){d1.e1('');}
function zB(){}
_ = zB.prototype = new f();_.yx = w0;_.vB = A0;_.B0 = C0;_.e1 =
D0;
_.i = 'java.lang.StringBuffer';_.j = 75;
function f1(){f1 = a;g1 = new iX();h1 = new iX();return window;}
CoffeeScript Output

 var bitlist, kids, singers, song;

 song = ["do", "re", "mi", "fa", "so"];

 singers = {
    Jagger: "Rock",
    Elvis: "Roll"
 };
The golden rule of CoffeeScript is: "It's just JavaScript". The
code compiles one-to-one into the equivalent JS, and there is no
interpretation at runtime. [....].

The compiled output is readable and pretty-printed, passes
through JavaScript Lint without warnings, will work in every
JavaScript runtime, and tends to run as fast or faster than the
equivalent handwritten JavaScript.




                               “It's just javascript!”
Javascript


    $("#ComboBox").change(function() {
             callThisMessage(function(){
                  $.callOtherMethods(function(){
                    $("#ComoboBox").move()
                  });
             })
    });
CoffeeScript


    $("#ComboBox").change ->
       callThisMessage ->
          $.callOtherMethods ->
               $("#ComoboBox").move()
Whats good about CoffeeScript
• Private by default
• no more variable leaking
• == vs ===, Fuzzy matching is gone. "is" is the ===
• Passes JSLint with flying colours.
Getting Started With CoffeeScript
• CoffeeScript.org is the best resource available.
• Install Node.js and install the CoffeeScript compiler
• coffee -c filename.coffee generates the javascript!
What well go over
• Syntax
• Whitespace Matters
• Object Syntax
• Everything is an expression
• Comprehension
• Classes, Inheritance
• Bound Functions
• Conditionals
Syntax



    message = "hello world"


    sayHello = ->
    	    alert message


    sayhelloWithName = (name)->
    	    alert message+' '+name
Whitespace Matters


    message = ->
    	   someOtherMessage ->
    	   	   console.log "hello"


    someOtherMessage = (callback)->
    	   callback()


    message()
Object Syntax

    conference =
    	   title: 'FITC Screens 2012'
    	   description: 'The coolest conference ever!'
    	   details:
    	   	   homepage: 'http://www.fitc.ca'
    	   	   rss:'http://somerssurl.com'
    	   sayName: ->
    	   	   alert @title


    conference.sayName()
Everything is an expression
    x = 4
    lessmessage = "less then four"
    greatermessage = "greater than or equal to four"
    message = if x < 4   then less message else greatermessage
    alert message


    numberToString = (value) ->
    	   switch value
    	   	   when value is 1 then "one"
    	   	   when value is 2 then "two"
    	   	   when value is 3 then "three"
    	   	   when value is 4 then "four"


    number = numberToString(3)
    alert number
Comprehension


   items = [1,2,3,4,5,6,7,8,9,10]


   doubleitems = (double num for num in items when num <5)
   tripleitems = (triple num for num in items when num >= 5)


   double = (x)->
   	   x*2


   triple = (x)->
   	   x*3
Classes & Inheritence
  class Animal                             var Animal, Zebra,
                                            __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },

  	   constructor: (@name) ->               __hasProp = {}.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; };

                                           Animal = (function() {

  	   sayName: ->                           function Animal(name) {
                                              this.name = name;
                                            }
  	   	   @name                             Animal.prototype.sayName = function() {
                                              return this.name;
                                            };

                                            return Animal;

  class Zebra extends Animal               })();

                                           Zebra = (function(_super) {
  	   constructor: (@name,@hasStripes)->    __extends(Zebra, _super);

  	   	   super(@name)                      function Zebra(name, hasStripes) {
                                             this.name = name;
                                             this.hasStripes = hasStripes;
                                             this.checkName = __bind(this.checkName, this);

                                                Zebra.__super__.constructor.call(this, this.name);
  	   hasStripes: ->                        }

                                            Zebra.prototype.hasStripes = function() {

  	   	   @hasStripes                       };
                                              return this.hasStripes;

                                            Zebra.prototype.checkName = function() {
                                              return httpgetcall(this.name, function() {
                                                return console.log(this.hasStripes);
                                              });
                                            };
  	   isCool: =>                            return Zebra;

  	   	   httpgetcall @name,->             })(Animal);



  	   	   	   console.log @hasStripes
Bound Functions


  [...]
  class Zebra extends Animal
  	   constructor: (@name,@hasStripes)->
  	   	   super(@name)


  	   hasStripes: ->
  	   	   @hasStripes


  	   isCool: =>
  	   	   httpgetcall @name,->
  	   	   	   console.log @hasStripes
Conditionals



  x = 10
  number = 5
  shouldDouble = true




  doubleNumber = (value)->
  	   value*2


  number = doubleNumber x if shouldDouble
CoffeeScript is Javascripts new Hope
Thank you.
follow me @faisalabid

More Related Content

What's hot

CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
None
 
Devoxx 15 equals hashcode
Devoxx 15 equals hashcodeDevoxx 15 equals hashcode
Devoxx 15 equals hashcode
bleporini
 
The core libraries you always wanted - Google Guava
The core libraries you always wanted - Google GuavaThe core libraries you always wanted - Google Guava
The core libraries you always wanted - Google Guava
Mite Mitreski
 

What's hot (20)

Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
 
Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScript
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Evolutionary Nursery
Evolutionary NurseryEvolutionary Nursery
Evolutionary Nursery
 
Phylogenetics in R
Phylogenetics in RPhylogenetics in R
Phylogenetics in R
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Marimba - A MapReduce-based Programming Model for Self-maintainable Aggregate...
Marimba - A MapReduce-based Programming Model for Self-maintainable Aggregate...Marimba - A MapReduce-based Programming Model for Self-maintainable Aggregate...
Marimba - A MapReduce-based Programming Model for Self-maintainable Aggregate...
 
Marimba - Ein MapReduce-basiertes Programmiermodell für selbstwartbare Aggreg...
Marimba - Ein MapReduce-basiertes Programmiermodell für selbstwartbare Aggreg...Marimba - Ein MapReduce-basiertes Programmiermodell für selbstwartbare Aggreg...
Marimba - Ein MapReduce-basiertes Programmiermodell für selbstwartbare Aggreg...
 
Python - Lecture 3
Python - Lecture 3Python - Lecture 3
Python - Lecture 3
 
Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1
 
Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
 
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorProgramming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Swing database(mysql)
Swing database(mysql)Swing database(mysql)
Swing database(mysql)
 
Clean code with google guava jee conf
Clean code with google guava jee confClean code with google guava jee conf
Clean code with google guava jee conf
 
Google guava
Google guavaGoogle guava
Google guava
 
Devoxx 15 equals hashcode
Devoxx 15 equals hashcodeDevoxx 15 equals hashcode
Devoxx 15 equals hashcode
 
ES6 and BEYOND
ES6 and BEYONDES6 and BEYOND
ES6 and BEYOND
 
The core libraries you always wanted - Google Guava
The core libraries you always wanted - Google GuavaThe core libraries you always wanted - Google Guava
The core libraries you always wanted - Google Guava
 

Viewers also liked

Cloud Foundry Open Tour Keynote
Cloud Foundry Open Tour KeynoteCloud Foundry Open Tour Keynote
Cloud Foundry Open Tour Keynote
RamnivasLaddad
 

Viewers also liked (7)

10 myths 10 Minutes 10 tweets
10 myths 10 Minutes 10 tweets10 myths 10 Minutes 10 tweets
10 myths 10 Minutes 10 tweets
 
Display Ads Performance with Sugar
Display Ads Performance with SugarDisplay Ads Performance with Sugar
Display Ads Performance with Sugar
 
Design Pattern with Actionscript
Design Pattern with ActionscriptDesign Pattern with Actionscript
Design Pattern with Actionscript
 
Cloud Foundry Open Tour Keynote
Cloud Foundry Open Tour KeynoteCloud Foundry Open Tour Keynote
Cloud Foundry Open Tour Keynote
 
Jason Hunter Slides from NoSQL Oakland meetup
Jason Hunter Slides from NoSQL Oakland meetupJason Hunter Slides from NoSQL Oakland meetup
Jason Hunter Slides from NoSQL Oakland meetup
 
MarkLogic Server / NoSQL at ApacheCon
MarkLogic Server / NoSQL at ApacheConMarkLogic Server / NoSQL at ApacheCon
MarkLogic Server / NoSQL at ApacheCon
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 

Similar to FITC CoffeeScript 101

Internal Project: Under the Hood
Internal Project: Under the HoodInternal Project: Under the Hood
Internal Project: Under the Hood
Vladik Khononov
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
jsmith92
 

Similar to FITC CoffeeScript 101 (20)

Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Grails GORM - You Know SQL. You Know Queries. Here's GORM.Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Grails GORM - You Know SQL. You Know Queries. Here's GORM.
 
Coffee Scriptでenchant.js
Coffee Scriptでenchant.jsCoffee Scriptでenchant.js
Coffee Scriptでenchant.js
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
Internal Project: Under the Hood
Internal Project: Under the HoodInternal Project: Under the Hood
Internal Project: Under the Hood
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leet
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
 
JS OO and Closures
JS OO and ClosuresJS OO and Closures
JS OO and Closures
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
 

FITC CoffeeScript 101

  • 2. Who am I? • A software engineer and entrepreneur • On the tablet team at Kobo • Find me blogging at FaisalAbid.com • Tweeting @FaisalAbid
  • 3. A few things I’ve worked on
  • 4. 101
  • 6. The bad parts • Global Variables • operator overloading in a dynamic typed language • semicolon optional • typeof inconsistency. null = object?! • ==,===, != • false, null, undefined, NaN
  • 7. Javascript also has good parts
  • 8. Being the good guy is hard in Javascript
  • 9. Would you like a cup of coffee?
  • 10. Not the first of its kind.
  • 11. GWT Output function A0(){this.B0();return this.js[0];} function C0(){if(this.js.length > 1) {this.js = [this.js.join('')];this.length = this.js[0].length;}} function D0(E0){this.js = [E0];this.length = E0.length;} function Ez(F0,a1){return F0.yx(yZ(a1));} function yB(b1){c1(b1);return b1;} function c1(d1){d1.e1('');} function zB(){} _ = zB.prototype = new f();_.yx = w0;_.vB = A0;_.B0 = C0;_.e1 = D0; _.i = 'java.lang.StringBuffer';_.j = 75; function f1(){f1 = a;g1 = new iX();h1 = new iX();return window;}
  • 12.
  • 13. CoffeeScript Output var bitlist, kids, singers, song; song = ["do", "re", "mi", "fa", "so"]; singers = { Jagger: "Rock", Elvis: "Roll" };
  • 14.
  • 15. The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. [....]. The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript. “It's just javascript!”
  • 16. Javascript $("#ComboBox").change(function() { callThisMessage(function(){ $.callOtherMethods(function(){ $("#ComoboBox").move() }); }) });
  • 17. CoffeeScript $("#ComboBox").change -> callThisMessage -> $.callOtherMethods -> $("#ComoboBox").move()
  • 18. Whats good about CoffeeScript • Private by default • no more variable leaking • == vs ===, Fuzzy matching is gone. "is" is the === • Passes JSLint with flying colours.
  • 19. Getting Started With CoffeeScript • CoffeeScript.org is the best resource available. • Install Node.js and install the CoffeeScript compiler • coffee -c filename.coffee generates the javascript!
  • 20. What well go over • Syntax • Whitespace Matters • Object Syntax • Everything is an expression • Comprehension • Classes, Inheritance • Bound Functions • Conditionals
  • 21. Syntax message = "hello world" sayHello = -> alert message sayhelloWithName = (name)-> alert message+' '+name
  • 22. Whitespace Matters message = -> someOtherMessage -> console.log "hello" someOtherMessage = (callback)-> callback() message()
  • 23. Object Syntax conference = title: 'FITC Screens 2012' description: 'The coolest conference ever!' details: homepage: 'http://www.fitc.ca' rss:'http://somerssurl.com' sayName: -> alert @title conference.sayName()
  • 24. Everything is an expression x = 4 lessmessage = "less then four" greatermessage = "greater than or equal to four" message = if x < 4 then less message else greatermessage alert message numberToString = (value) -> switch value when value is 1 then "one" when value is 2 then "two" when value is 3 then "three" when value is 4 then "four" number = numberToString(3) alert number
  • 25. Comprehension items = [1,2,3,4,5,6,7,8,9,10] doubleitems = (double num for num in items when num <5) tripleitems = (triple num for num in items when num >= 5) double = (x)-> x*2 triple = (x)-> x*3
  • 26. Classes & Inheritence class Animal var Animal, Zebra, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, constructor: (@name) -> __hasProp = {}.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; }; Animal = (function() { sayName: -> function Animal(name) { this.name = name; } @name Animal.prototype.sayName = function() { return this.name; }; return Animal; class Zebra extends Animal })(); Zebra = (function(_super) { constructor: (@name,@hasStripes)-> __extends(Zebra, _super); super(@name) function Zebra(name, hasStripes) { this.name = name; this.hasStripes = hasStripes; this.checkName = __bind(this.checkName, this); Zebra.__super__.constructor.call(this, this.name); hasStripes: -> } Zebra.prototype.hasStripes = function() { @hasStripes }; return this.hasStripes; Zebra.prototype.checkName = function() { return httpgetcall(this.name, function() { return console.log(this.hasStripes); }); }; isCool: => return Zebra; httpgetcall @name,-> })(Animal); console.log @hasStripes
  • 27. Bound Functions [...] class Zebra extends Animal constructor: (@name,@hasStripes)-> super(@name) hasStripes: -> @hasStripes isCool: => httpgetcall @name,-> console.log @hasStripes
  • 28. Conditionals x = 10 number = 5 shouldDouble = true doubleNumber = (value)-> value*2 number = doubleNumber x if shouldDouble
  • 30. Thank you. follow me @faisalabid

Editor's Notes

  1. \n
  2. \n
  3. - Some projects ive worked on\n- Describe the projects\n- Talk about the ARC\n- Personal projects use CoffeeScript\n
  4. - CoffeeScript 101\n- Popular language taking over the landscape\n- Dropbox recently changed over most of their code to CoffeeScript.\n- If DB can change with there immense scale, then there has to be something to it.\n- before we see what that something is, lets take a step back and go back in time to 2005\n
  5. - JS is 17 years old. \n- Originally Mocha, LiveScript. \n- Brendan Eich created this in 3 days. But it still lives today.\n- lots of holes, but behind those scars lies a beautiful language.\n\n- Before Javascript was for garbage mouse followers, flashy things, and a lot of people werent really js developers\n- in 2005 google introduced Google maps which became a revolution in JS\n- Suddenly javascript went from being a bullshit sprinkled language to a full blown powerhouse in the eyes of developers\n- Frameworks cropped up, JQUery came out. and in 2012, HTML5 and JS have taken over.\n\n- but theres a problem. \n- JS is very clunky, and very very hard to write Google Map type web apps without tons of time invested.\n- Time = Money \n- Gets very messy and unreadable after a while.\n\n- Ajax, and other stuff\n\n- in short JS has its bad parts\n\n
  6. \n
  7. - Great book by Douglas Crockford.\n- Pioneer in good JS development\n- JSLint/ JSOn Libraries\n- following these &amp;#x201C;good parts&amp;#x201D; is not always easy.\n
  8. Not always easy to adopt to changes. \nTons of JS libraries make it hard. Some might be bad, not your fault!\n\n
  9. On December 13, 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript with the comment: &quot;initial commit of the mystery language.&quot;[9] The compiler was written in Ruby. \nOn December 24, he made the first tagged and documented release, 0.1.0. On February 21, 2010, he committed version 0.5, which replaced the Ruby compiler with one written in pure CoffeeScript. By that time the project had attracted several other contributors on GitHub, and was receiving over 300 page hits per day.\nOn December 24, 2010, Ashkenas announced the release of stable 1.0.0 to Hacker News, the site where the project was announced for the first time.[10][11]\n\nTalk abou how you came to the language. what were you doing?\n\n
  10. - in 2006 google announced GWT\n- holy crist mind blown, java to javascript. no need to learn that javascript crap anymore.\n- Checked it out. Had to go through tons of hoops. \n- Install extension ,etc etc. JS it outputs is cryptic.\n
  11. - first impression. wow look at how optimized my js i, hell ya hell ya\n- I love this tool.when people see im doing this ill be so cool\n\n
  12. - Makes your JS hard to debug. Obfuscation is fine in production, but in development its stupid.\n- on the contrary, take a look at what CoffeeScript outputs\n
  13. - Boring old javascript. nothing special.\n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. A new hope of Javascript.\nCleaner more efficent.\nWith Node.js its even more awesome.\nPlease check it out.\n\n\n
  30. \n