SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Mehr von Mark

Mangling Ruby with TracePoint
Mangling Ruby with TracePointMangling Ruby with TracePoint
Mangling Ruby with TracePointMark
 
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.jsMark
 
A Big Look at MiniTest
A Big Look at MiniTestA Big Look at MiniTest
A Big Look at MiniTestMark
 
A Big Look at MiniTest
A Big Look at MiniTestA Big Look at MiniTest
A Big Look at MiniTestMark
 
GET /better
GET /betterGET /better
GET /betterMark
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScriptMark
 
Testing Your JavaScript & CoffeeScript
Testing Your JavaScript & CoffeeScriptTesting Your JavaScript & CoffeeScript
Testing Your JavaScript & CoffeeScriptMark
 
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 ItMark
 
5 Favorite Gems (Lightning Talk(
5 Favorite Gems (Lightning Talk(5 Favorite Gems (Lightning Talk(
5 Favorite Gems (Lightning Talk(Mark
 
CoffeeScript for the Rubyist
CoffeeScript for the RubyistCoffeeScript for the Rubyist
CoffeeScript for the RubyistMark
 
RubyMotion
RubyMotionRubyMotion
RubyMotionMark
 
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 ChaiMark
 
CoffeeScript for the Rubyist
CoffeeScript for the RubyistCoffeeScript for the Rubyist
CoffeeScript for the RubyistMark
 
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 JasmineMark
 
DRb and Rinda
DRb and RindaDRb and Rinda
DRb and RindaMark
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairMark
 
Distributed Programming with Ruby/Rubyconf 2010
Distributed Programming with Ruby/Rubyconf 2010Distributed Programming with Ruby/Rubyconf 2010
Distributed Programming with Ruby/Rubyconf 2010Mark
 

Mehr von 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
 

Kürzlich hochgeladen

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 Processorsdebabhi2
 
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 Servicegiselly40
 
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 MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
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 2024The Digital Insurer
 
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 organizationRadu Cotescu
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
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 interpreternaman860154
 
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.pptxHampshireHUG
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
[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.pdfhans926745
 
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 slidevu2urc
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Kürzlich hochgeladen (20)

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
 
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
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
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
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[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
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

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

Hinweis der Redaktion

  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