SlideShare une entreprise Scribd logo
1  sur  63
coffeescript and you
 become a better javascript developer
what coffeescript
     is not
not just a lack of
 punctuation!
so what is
coffeescript?
coffeescript is...
coffeescript is...
“a little language that compiles into
JavaScript”
easier to read and write
a hybrid of ruby and python
helpful
haven’t i heard this
     before?
other js compilers
other js compilers

gwt (java)
jsil (.net)
pyjamas (python)
gwt
import     com.google.gwt.core.client.EntryPoint;
import     com.google.gwt.user.client.Window;
import     com.google.gwt.user.client.ui.Button;
import     com.google.gwt.user.client.ui.ClickListener;
import     com.google.gwt.user.client.ui.RootPanel;
import     com.google.gwt.user.client.ui.Widget;

/**
 * HelloWorld application.
 */
public class Hello implements EntryPoint {

    public void onModuleLoad() {
      Button b = new Button("Click me", new ClickListener() {
        public void onClick(Widget sender) {
          Window.alert("Hello World");
        }
      });

        RootPanel.get().add(b);
    }
}
jsil
using System;
using System.Collections.Generic;

public static class Program {
    public static void Main (string[] args) {
        var array = new[] { 1, 2, 4, 8, 16 };

        foreach (var i in array)
            Console.WriteLine(i);

        var list = new List<int>(array);

        foreach (var j in list)
            Console.WriteLine(j);
    }
}
pyjamas
from pyjamas import Window
from pyjamas.ui import RootPanel, Button

def greet(sender):
    Window.alert("Hello, AJAX!")

b = Button("Click me", greet)
RootPanel().add(b)
so why should i
embrace (and love)
  coffeescript?
example
$(function() {
   $.get('example.php', function(data) {
      if (data.errors != null) {
        alert("There was an error!");
      } else {
        $("#content").text(data.message);
      }
   })
})
$(function() {
   $.get('example.php', function(data) {
      if (data.errors != null) {
        alert("There was an error!")
      } else {
        $("#content").text(data.message)
      }
   })
})
$ function() {
  $.get 'example.php', function(data) {
    if data.errors != null {
      alert "There was an error!"
    } else {
      $("#content").text data.message
    }
  }
}
$ function()
  $.get 'example.php', function(data)
    if data.errors != null
      alert "There was an error!"
    else
      $("#content").text data.message
$ ()->
  $.get 'example.php', (data)->
    if data.errors != null
       alert "There was an error!"
    else
       $("#content").text data.message
$ ->
  $.get 'example.php', (data)->
     if data.errors?
       alert "There was an error!"
     else
       $("#content").text data.message
$(function() {
   $.get('example.php', function(data) {
      if (data.errors != null) {
        alert("There was an error!");
      } else {
        $("#content").text(data.message);
      }
   })
})
ranges
# 1, 2, 3, 4, 5
[1..5]

# 1,..., 4
[1...5]

#1,...,500
[1..500]

#5, 4, 3, 2, 1
[5..1]

#500,...,1
[500..1]
var _i, _j, _results, _results2;
[1, 2, 3, 4, 5];
[1, 2, 3, 4];
(function() {
  _results = [];
  for (_i = 1; _i <= 500; _i++)
{ _results.push(_i); }
  return _results;
}).apply(this, arguments);
[5, 4, 3, 2, 1];
(function() {
  _results2 = [];
  for (_j = 500; _j >= 1; _j--)
{ _results2.push(_j); }
  return _results2;
}).apply(this, arguments);
conditionals
if a
  console.log "a is true"
else
  console.log "a is false"

unless a
  console.log "a is false"

console.log "a is false" unless a
if (a) {
  console.log("a   is true");
} else {
  console.log("a   is false");
}
if (!a) {
  console.log("a   is false");
}
if (!a) {
  console.log("a   is false");
}
if a is 'A'
  console.log "a is A"

if a == 'A'
  console.log "a is A"
if (a === 'A') {
  console.log("a is A");
}
if (a === 'A') {
  console.log("a is A");
}
existential operator
console?.log "Only log if there is a
console"

if $("#bid_form")?.html()? and @model?
  doSomething()
var _ref;
if (typeof console !== "undefined" && console !== null) {
  console.log("Only log if there is a console");
}
if ((((_ref = $("#bid_form")) != null ? _ref.html() : void
0) != null) && (this.model != null)) {
  doSomething();
}
functions
sayHi = (name)->
  console.log "hello: #{name}"

sayHi 'mark'
sayHi('mark')

sayBye = ->
  console.log "bye!"

sayBye()
var sayBye, sayHi;
sayHi = function(name) {
   return console.log("hello: " + name);
};
sayHi('mark');
sayHi('mark');
sayBye = function() {
   return console.log("bye!");
};
sayBye();
bound functions
class User

  constructor: (@name) ->

  sayHi: ->
    console.log "Hello, #{@name}"

u1 = new User('mark')
u2 = new User('bob')

jqxhr = $.get "example.php", ->
    alert "success"

jqxhr.success u1.sayHi
jqxhr.success u2.sayHi
var User, jqxhr, u1, u2;
User = (function() {
  function User(name) {
     this.name = name;
  }
  User.prototype.sayHi = function() {
     return console.log("Hello, " + this.name);
  };
  return User;
})();
u1 = new User('mark');
u2 = new User('bob');
jqxhr = $.get("example.php", function() {
  return alert("success");
});
jqxhr.success(u1.sayHi);
jqxhr.success(u2.sayHi);
Hello, undefined
Hello, undefined
class User

  constructor: (@name) ->

  sayHi: ->
    console.log "Hello, #{@name}"

u1 = new User('mark')
u2 = new User('bob')

jqxhr = $.get "example.php", ->
    alert "success"

jqxhr.success u1.sayHi
jqxhr.success u2.sayHi
class User

  constructor: (@name) ->

  sayHi: =>
    console.log "Hello, #{@name}"

u1 = new User('mark')
u2 = new User('bob')

jqxhr = $.get "example.php", ->
    alert "success"

jqxhr.success u1.sayHi
jqxhr.success u2.sayHi
var User, jqxhr, u1, u2;
var __bind = function(fn, me){ return function(){ return
fn.apply(me, arguments); }; };
User = (function() {
  function User(name) {
     this.name = name;
     this.sayHi = __bind(this.sayHi, this);
  }
  User.prototype.sayHi = function() {
     return console.log("Hello, " + this.name);
  };
  return User;
})();
u1 = new User('mark');
u2 = new User('bob');
jqxhr = $.get("example.php", function() {
  return alert("success");
});
jqxhr.success(u1.sayHi);
jqxhr.success(u2.sayHi);
Hello, mark
Hello, bob
default arguments
createElement = (name, attributes = {}) ->
  obj = document.createElement name
  for key, val of attributes
    obj.setAttribute key, val
  obj
var createElement;
createElement = function(name, attributes) {
   var key, obj, val;
   if (attributes == null) {
     attributes = {};
   }
   obj = document.createElement(name);
   for (key in attributes) {
     val = attributes[key];
     obj.setAttribute(key, val);
   }
   return obj;
};
scope
a = 'A'
myFunc = ->
  a = 'AAA'
  b = 'B'
(function() {
  var a, myFunc;
  a = 'A';
  myFunc = function() {
     var b;
     a = 'AAA';
     return b = 'B';
  };
}).call(this);
array loops
arr = [1..5]

for num in arr
  console.log num
var arr, num, _i, _len;
arr = [1, 2, 3, 4, 5];
for (_i = 0, _len = arr.length; _i < _len; _i++) {
  num = arr[_i];
  console.log(num);
}
classes

easy to define
easy to extend
help keep code clean
class Employee

  constructor: (@name, @options = {})->

  job: ->
    @options.job

mark = new Employee('Mark', job: 'Developer')

mark.job() # => 'Developer'
mark.name # => 'Mark'
var Employee, mark;
Employee = (function() {
  function Employee(name, options) {
     this.name = name;
     this.options = options != null ? options : {};
  }
  Employee.prototype.job = function() {
     return this.options.job;
  };
  return Employee;
})();
mark = new Employee('Mark', {
  job: 'Developer'
});
mark.job();
mark.name;
extending classes
class Manager extends Employee

  job: ->
    "$$$ #{super}"

steve = new Manager('Steve', job: 'CEO')

steve.job() # => '$$$ CEO'
steve.name # => 'Steve'
var Manager, steve;
var __hasProp = Object.prototype.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;
};
Manager = (function() {
   __extends(Manager, Employee);
   function Manager() {
      Manager.__super__.constructor.apply(this, arguments);
   }
   Manager.prototype.job = function() {
      return "$$$ " + Manager.__super__.job.apply(this, arguments);
   };
   return Manager;
})();
steve = new Manager('Steve', {
   job: 'CEO'
});
steve.job();
steve.name;
what the hell is @?
class User

  constructor: (@name)->
    @new_record = true

  @find: (id)->
    # do some work
class User

  constructor: (name)->
    @name = name
    @new_record = true

  @find: (id)->
    # do some work
var User;
User = (function() {
  function User(name) {
    this.name = name;
    this.new_record = true;
  }
  User.find = function(id) {};
  return User;
})();
thank you
                   @markbates
           http://www.markbates.com
http://www.slideshare.net/markykang/coffeescript-and-you

Contenu connexe

Plus de Mark

Plus de Mark (17)

Mangling Ruby with TracePoint
Mangling Ruby with TracePointMangling Ruby with TracePoint
Mangling Ruby with TracePoint
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.js
 
A Big Look at MiniTest
A Big Look at MiniTestA Big Look at MiniTest
A Big Look at MiniTest
 
A Big Look at MiniTest
A Big Look at MiniTestA Big Look at MiniTest
A Big Look at MiniTest
 
GET /better
GET /betterGET /better
GET /better
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Testing Your JavaScript & CoffeeScript
Testing Your JavaScript & CoffeeScriptTesting Your JavaScript & CoffeeScript
Testing Your JavaScript & CoffeeScript
 
Building an API in Rails without Realizing It
Building an API in Rails without Realizing ItBuilding an API in Rails without Realizing It
Building an API in Rails without Realizing It
 
5 Favorite Gems (Lightning Talk(
5 Favorite Gems (Lightning Talk(5 Favorite Gems (Lightning Talk(
5 Favorite Gems (Lightning Talk(
 
CoffeeScript for the Rubyist
CoffeeScript for the RubyistCoffeeScript for the Rubyist
CoffeeScript for the Rubyist
 
RubyMotion
RubyMotionRubyMotion
RubyMotion
 
Testing JavaScript/CoffeeScript with Mocha and Chai
Testing JavaScript/CoffeeScript with Mocha and ChaiTesting JavaScript/CoffeeScript with Mocha and Chai
Testing JavaScript/CoffeeScript with Mocha and Chai
 
CoffeeScript for the Rubyist
CoffeeScript for the RubyistCoffeeScript for the Rubyist
CoffeeScript for the Rubyist
 
Testing Rich Client Side Apps with Jasmine
Testing Rich Client Side Apps with JasmineTesting Rich Client Side Apps with Jasmine
Testing Rich Client Side Apps with Jasmine
 
DRb and Rinda
DRb and RindaDRb and Rinda
DRb and Rinda
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
Distributed Programming with Ruby/Rubyconf 2010
Distributed Programming with Ruby/Rubyconf 2010Distributed Programming with Ruby/Rubyconf 2010
Distributed Programming with Ruby/Rubyconf 2010
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

CoffeeScript and You

  • 1. coffeescript and you become a better javascript developer
  • 3. not just a lack of punctuation!
  • 6. coffeescript is... “a little language that compiles into JavaScript” easier to read and write a hybrid of ruby and python helpful
  • 7. haven’t i heard this before?
  • 9. other js compilers gwt (java) jsil (.net) pyjamas (python)
  • 10. gwt import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; /** * HelloWorld application. */ public class Hello implements EntryPoint { public void onModuleLoad() { Button b = new Button("Click me", new ClickListener() { public void onClick(Widget sender) { Window.alert("Hello World"); } }); RootPanel.get().add(b); } }
  • 11. jsil using System; using System.Collections.Generic; public static class Program { public static void Main (string[] args) { var array = new[] { 1, 2, 4, 8, 16 }; foreach (var i in array) Console.WriteLine(i); var list = new List<int>(array); foreach (var j in list) Console.WriteLine(j); } }
  • 12. pyjamas from pyjamas import Window from pyjamas.ui import RootPanel, Button def greet(sender): Window.alert("Hello, AJAX!") b = Button("Click me", greet) RootPanel().add(b)
  • 13. so why should i embrace (and love) coffeescript?
  • 15. $(function() { $.get('example.php', function(data) { if (data.errors != null) { alert("There was an error!"); } else { $("#content").text(data.message); } }) })
  • 16. $(function() { $.get('example.php', function(data) { if (data.errors != null) { alert("There was an error!") } else { $("#content").text(data.message) } }) })
  • 17. $ function() { $.get 'example.php', function(data) { if data.errors != null { alert "There was an error!" } else { $("#content").text data.message } } }
  • 18. $ function() $.get 'example.php', function(data) if data.errors != null alert "There was an error!" else $("#content").text data.message
  • 19. $ ()-> $.get 'example.php', (data)-> if data.errors != null alert "There was an error!" else $("#content").text data.message
  • 20. $ -> $.get 'example.php', (data)-> if data.errors? alert "There was an error!" else $("#content").text data.message
  • 21. $(function() { $.get('example.php', function(data) { if (data.errors != null) { alert("There was an error!"); } else { $("#content").text(data.message); } }) })
  • 23. # 1, 2, 3, 4, 5 [1..5] # 1,..., 4 [1...5] #1,...,500 [1..500] #5, 4, 3, 2, 1 [5..1] #500,...,1 [500..1]
  • 24. var _i, _j, _results, _results2; [1, 2, 3, 4, 5]; [1, 2, 3, 4]; (function() { _results = []; for (_i = 1; _i <= 500; _i++) { _results.push(_i); } return _results; }).apply(this, arguments); [5, 4, 3, 2, 1]; (function() { _results2 = []; for (_j = 500; _j >= 1; _j--) { _results2.push(_j); } return _results2; }).apply(this, arguments);
  • 26. if a console.log "a is true" else console.log "a is false" unless a console.log "a is false" console.log "a is false" unless a
  • 27. if (a) { console.log("a is true"); } else { console.log("a is false"); } if (!a) { console.log("a is false"); } if (!a) { console.log("a is false"); }
  • 28. if a is 'A' console.log "a is A" if a == 'A' console.log "a is A"
  • 29. if (a === 'A') { console.log("a is A"); } if (a === 'A') { console.log("a is A"); }
  • 31. console?.log "Only log if there is a console" if $("#bid_form")?.html()? and @model? doSomething()
  • 32. var _ref; if (typeof console !== "undefined" && console !== null) { console.log("Only log if there is a console"); } if ((((_ref = $("#bid_form")) != null ? _ref.html() : void 0) != null) && (this.model != null)) { doSomething(); }
  • 34. sayHi = (name)-> console.log "hello: #{name}" sayHi 'mark' sayHi('mark') sayBye = -> console.log "bye!" sayBye()
  • 35. var sayBye, sayHi; sayHi = function(name) { return console.log("hello: " + name); }; sayHi('mark'); sayHi('mark'); sayBye = function() { return console.log("bye!"); }; sayBye();
  • 37. class User constructor: (@name) -> sayHi: -> console.log "Hello, #{@name}" u1 = new User('mark') u2 = new User('bob') jqxhr = $.get "example.php", -> alert "success" jqxhr.success u1.sayHi jqxhr.success u2.sayHi
  • 38. var User, jqxhr, u1, u2; User = (function() { function User(name) { this.name = name; } User.prototype.sayHi = function() { return console.log("Hello, " + this.name); }; return User; })(); u1 = new User('mark'); u2 = new User('bob'); jqxhr = $.get("example.php", function() { return alert("success"); }); jqxhr.success(u1.sayHi); jqxhr.success(u2.sayHi);
  • 40. class User constructor: (@name) -> sayHi: -> console.log "Hello, #{@name}" u1 = new User('mark') u2 = new User('bob') jqxhr = $.get "example.php", -> alert "success" jqxhr.success u1.sayHi jqxhr.success u2.sayHi
  • 41. class User constructor: (@name) -> sayHi: => console.log "Hello, #{@name}" u1 = new User('mark') u2 = new User('bob') jqxhr = $.get "example.php", -> alert "success" jqxhr.success u1.sayHi jqxhr.success u2.sayHi
  • 42. var User, jqxhr, u1, u2; var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; User = (function() { function User(name) { this.name = name; this.sayHi = __bind(this.sayHi, this); } User.prototype.sayHi = function() { return console.log("Hello, " + this.name); }; return User; })(); u1 = new User('mark'); u2 = new User('bob'); jqxhr = $.get("example.php", function() { return alert("success"); }); jqxhr.success(u1.sayHi); jqxhr.success(u2.sayHi);
  • 45. createElement = (name, attributes = {}) -> obj = document.createElement name for key, val of attributes obj.setAttribute key, val obj
  • 46. var createElement; createElement = function(name, attributes) { var key, obj, val; if (attributes == null) { attributes = {}; } obj = document.createElement(name); for (key in attributes) { val = attributes[key]; obj.setAttribute(key, val); } return obj; };
  • 47. scope
  • 48. a = 'A' myFunc = -> a = 'AAA' b = 'B'
  • 49. (function() { var a, myFunc; a = 'A'; myFunc = function() { var b; a = 'AAA'; return b = 'B'; }; }).call(this);
  • 51. arr = [1..5] for num in arr console.log num
  • 52. var arr, num, _i, _len; arr = [1, 2, 3, 4, 5]; for (_i = 0, _len = arr.length; _i < _len; _i++) { num = arr[_i]; console.log(num); }
  • 53. classes easy to define easy to extend help keep code clean
  • 54. class Employee constructor: (@name, @options = {})-> job: -> @options.job mark = new Employee('Mark', job: 'Developer') mark.job() # => 'Developer' mark.name # => 'Mark'
  • 55. var Employee, mark; Employee = (function() { function Employee(name, options) { this.name = name; this.options = options != null ? options : {}; } Employee.prototype.job = function() { return this.options.job; }; return Employee; })(); mark = new Employee('Mark', { job: 'Developer' }); mark.job(); mark.name;
  • 57. class Manager extends Employee job: -> "$$$ #{super}" steve = new Manager('Steve', job: 'CEO') steve.job() # => '$$$ CEO' steve.name # => 'Steve'
  • 58. var Manager, steve; var __hasProp = Object.prototype.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; }; Manager = (function() { __extends(Manager, Employee); function Manager() { Manager.__super__.constructor.apply(this, arguments); } Manager.prototype.job = function() { return "$$$ " + Manager.__super__.job.apply(this, arguments); }; return Manager; })(); steve = new Manager('Steve', { job: 'CEO' }); steve.job(); steve.name;
  • 59. what the hell is @?
  • 60. class User constructor: (@name)-> @new_record = true @find: (id)-> # do some work
  • 61. class User constructor: (name)-> @name = name @new_record = true @find: (id)-> # do some work
  • 62. var User; User = (function() { function User(name) { this.name = name; this.new_record = true; } User.find = function(id) {}; return User; })();
  • 63. thank you @markbates http://www.markbates.com http://www.slideshare.net/markykang/coffeescript-and-you

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \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. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n