際際滷

際際滷Share a Scribd company logo
CoffeeScript by Example


               Christopher Bartling
Example 1
                Functions
Integration with jQuery
     String interpolation
Functions (->)
  function(){} is replaced by the thin arrow syntax: ->

  Method body determined by indentation

  All functions can act as expressions

  Optional parameters list precedes the thin arrow syntax

      (name, age, birthDate) ->

  Parameters can have default values

      (age, gender = female) ->
jQuery integration
  Hooking into the document.ready event:

    $(document).ready ->
    $ ->

  jQuery selectors need parentheses

  Function calls typically do not parentheses unless function call has no
  parameters
Strings and string interpolation
  Double-quoted strings allow for interpolated values

  Ruby-style interpolation syntax: #{}

      My name is #{person.fullName}

  Single-quoted strings are literal

  Multi-line strings and heredocs are allowed, even using string
  interpolation
Example 2
Comprehensions
Comprehensions
  Every loop in CoffeeScript returns a value, an array containing the
  result of every loop iteration

     (variable for variable in array)

  Filter iteration results by using when clause:

     (variable for variable in array when clause)

  Common idiom to combine function invocation and comprehension
Example 3
Lexical Scoping
Lexical scoping of variables
  No var keyword in CoffeeScript

  CoffeeScript doesnt allow global variables unless you explicitly export
  them

      Everything wrapped up in anonymous functions to maintain local
      context

      Variable assignments automatically pre鍖xed by var in generated
      JavaScript

  Impossible to shadow a higher-level variable
Example 4
Classes and Objects
Classes and inheritance
  Classes are now 鍖rst-class citizens

  Use the class keyword

  Generates to JavaScripts prototype usage

  Exporting a class to global scope

     class @ClassName

  Extension of another objects prototype

     class SavingsAccount extends Account
Constructors
  CoffeeScript constructors have explicit syntax:

  class Account
      constructor: ->

  Setting instance variables

    constructor: (@width) ->

  Calling the superclass constructor, passing along all current arguments:

    super
Object instances
  Reference an instance variable:

     @variableName

  Reference an instance method:

     @methodName()
Example 5
 Function Binding
Function Context
Bound function operator (=>)
  aka Fat Arrow

  Binds a function to the current context

     Retains the context no matter where the function is invoked

  Avoids the self = this game

  Prevalent use with event callbacks

     See this in later advanced examples
Example 6
Using Jasmine
Unit testing with Jasmine
  Behavior-driven development (BDD) JavaScript testing framework

  Why Jasmine?

     Clean testing framework

     Good matcher support

     Integration with sinon.js and jQuery

  CoffeeScript promotes separation of concerns

     Makes testing components much easier
Example 7
Backbone.js and CoffeeScript
              Jasmine testing
               Sinon.js spying
Backbone.js
  Popular JavaScript MVC framework

  Components

     Backbone.Model (model)

     Backbone.View (view)

     Backbone.Collection and Backbone.Router (controller)

  CoffeeScript allows easy extension of these classes
Where do I go from here?
  http://jashkenas.github.com/coffee-script/

  http://arcturo.github.com/library/coffeescript/index.html

  http://coffeescriptcookbook.com/

  http://peepcode.com/products/coffeescript

  http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-
  sinon.html
Tools and Frameworks
  Jasmine: http://pivotal.github.com/jasmine/

  Sinon.js: http://sinonjs.org/

  Jasmine-jQuery: https://github.com/velesin/jasmine-jquery

  Jasmine-Sinon: https://github.com/froots/jasmine-sinon

  Backbone.js: http://documentcloud.github.com/backbone/

  Underscore.js: http://documentcloud.github.com/underscore/
Contact Information
  chris.bartling@gmail.com

  Twitter: cbartling

  Blog: http://bartling.blogspot.com



  Presentation and example code can be found at https://bitbucket.org/
  cebartling/coffeescript-stuff

More Related Content

What's hot (20)

Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference Counting
Robert Brown
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
cacois
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
monikadeshmane
A Practitioners guide to Hardened JavaScript
A Practitioners guide to Hardened JavaScriptA Practitioners guide to Hardened JavaScript
A Practitioners guide to Hardened JavaScript
Tom Van Cutsem
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
kaven yan
Plattform端bergreifende App-Entwicklung (ein Vergleich) - MobileTechCon 2010
Plattform端bergreifende App-Entwicklung (ein Vergleich) - MobileTechCon 2010Plattform端bergreifende App-Entwicklung (ein Vergleich) - MobileTechCon 2010
Plattform端bergreifende App-Entwicklung (ein Vergleich) - MobileTechCon 2010
Heiko Behrens
React Native Evening
React Native EveningReact Native Evening
React Native Evening
Troy Miles
The quest for global design principles (SymfonyLive Berlin 2015)
The quest for global design principles (SymfonyLive Berlin 2015)The quest for global design principles (SymfonyLive Berlin 2015)
The quest for global design principles (SymfonyLive Berlin 2015)
Matthias Noback
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
What To Expect From PHP7
What To Expect From PHP7What To Expect From PHP7
What To Expect From PHP7
Codemotion
Efficient Android Threading
Efficient Android ThreadingEfficient Android Threading
Efficient Android Threading
Anders G旦ransson
Mobile Open Day: React Native: Crossplatform fast dive
Mobile Open Day: React Native: Crossplatform fast diveMobile Open Day: React Native: Crossplatform fast dive
Mobile Open Day: React Native: Crossplatform fast dive
epamspb
Mac/iOS Design Patterns
Mac/iOS Design PatternsMac/iOS Design Patterns
Mac/iOS Design Patterns
Robert Brown
java script
java scriptjava script
java script
monikadeshmane
Harder Faster Stronger
Harder Faster StrongerHarder Faster Stronger
Harder Faster Stronger
snyff
Node.js essentials
 Node.js essentials Node.js essentials
Node.js essentials
Bedis ElAch竪che
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
Duy Do
Boost Maintainability
Boost MaintainabilityBoost Maintainability
Boost Maintainability
Mosky Liu
Intro to React
Intro to ReactIntro to React
Intro to React
Troy Miles
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
Haci Murat Yaman
Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference Counting
Robert Brown
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
cacois
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
monikadeshmane
A Practitioners guide to Hardened JavaScript
A Practitioners guide to Hardened JavaScriptA Practitioners guide to Hardened JavaScript
A Practitioners guide to Hardened JavaScript
Tom Van Cutsem
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
kaven yan
Plattform端bergreifende App-Entwicklung (ein Vergleich) - MobileTechCon 2010
Plattform端bergreifende App-Entwicklung (ein Vergleich) - MobileTechCon 2010Plattform端bergreifende App-Entwicklung (ein Vergleich) - MobileTechCon 2010
Plattform端bergreifende App-Entwicklung (ein Vergleich) - MobileTechCon 2010
Heiko Behrens
React Native Evening
React Native EveningReact Native Evening
React Native Evening
Troy Miles
The quest for global design principles (SymfonyLive Berlin 2015)
The quest for global design principles (SymfonyLive Berlin 2015)The quest for global design principles (SymfonyLive Berlin 2015)
The quest for global design principles (SymfonyLive Berlin 2015)
Matthias Noback
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
What To Expect From PHP7
What To Expect From PHP7What To Expect From PHP7
What To Expect From PHP7
Codemotion
Efficient Android Threading
Efficient Android ThreadingEfficient Android Threading
Efficient Android Threading
Anders G旦ransson
Mobile Open Day: React Native: Crossplatform fast dive
Mobile Open Day: React Native: Crossplatform fast diveMobile Open Day: React Native: Crossplatform fast dive
Mobile Open Day: React Native: Crossplatform fast dive
epamspb
Mac/iOS Design Patterns
Mac/iOS Design PatternsMac/iOS Design Patterns
Mac/iOS Design Patterns
Robert Brown
Harder Faster Stronger
Harder Faster StrongerHarder Faster Stronger
Harder Faster Stronger
snyff
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
Duy Do
Boost Maintainability
Boost MaintainabilityBoost Maintainability
Boost Maintainability
Mosky Liu
Intro to React
Intro to ReactIntro to React
Intro to React
Troy Miles
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
Haci Murat Yaman

Similar to CoffeeScript By Example (20)

Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1
Micha Orman
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
Ajax Experience 2009
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya Johnny
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
Jyothsna Sree
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
AVINASH KUMAR
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
Fulvio Corno
JDBC programming
JDBC programmingJDBC programming
JDBC programming
Fulvio Corno
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
Ravi Mone
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
s4al_com
Spring
SpringSpring
Spring
s4al_com
A Scala tutorial
A Scala tutorialA Scala tutorial
A Scala tutorial
Dima Statz
inheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptxinheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptx
janetvidyaanancys
Lift Framework
Lift FrameworkLift Framework
Lift Framework
Jeffrey Groneberg
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
Vinay Kumar
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
rsnarayanan
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scala
Michal Bigos
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1Integration of Backbone.js with Spring 3.1
Integration of Backbone.js with Spring 3.1
Micha Orman
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
Ajax Experience 2009
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
Framework prototype
Framework prototypeFramework prototype
Framework prototype
DevMix
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya Johnny
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
AVINASH KUMAR
JDBC programming
JDBC programmingJDBC programming
JDBC programming
Fulvio Corno
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
Ravi Mone
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
s4al_com
A Scala tutorial
A Scala tutorialA Scala tutorial
A Scala tutorial
Dima Statz
inheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptxinheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptx
janetvidyaanancys
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
Vinay Kumar
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
rsnarayanan
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scala
Michal Bigos
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini

More from Christopher Bartling (12)

JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
Acceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvmAcceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvm
Christopher Bartling
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher Bartling
Building Tropo Apps with Grails
Building Tropo Apps with GrailsBuilding Tropo Apps with Grails
Building Tropo Apps with Grails
Christopher Bartling
Acceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And FriendsAcceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And Friends
Christopher Bartling
Introduction To Grails
Introduction To GrailsIntroduction To Grails
Introduction To Grails
Christopher Bartling
Cucumber, Cuke4Duke, and Groovy
Cucumber, Cuke4Duke, and GroovyCucumber, Cuke4Duke, and Groovy
Cucumber, Cuke4Duke, and Groovy
Christopher Bartling
Test Driven In Groovy
Test Driven In GroovyTest Driven In Groovy
Test Driven In Groovy
Christopher Bartling
iPhone OS: The Next Killer Platform
iPhone OS: The Next Killer PlatformiPhone OS: The Next Killer Platform
iPhone OS: The Next Killer Platform
Christopher Bartling
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
Christopher Bartling
Grails Overview
Grails OverviewGrails Overview
Grails Overview
Christopher Bartling
Rich Web Clients 20081118
Rich Web Clients 20081118Rich Web Clients 20081118
Rich Web Clients 20081118
Christopher Bartling
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
Acceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvmAcceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvm
Christopher Bartling
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher Bartling
Building Tropo Apps with Grails
Building Tropo Apps with GrailsBuilding Tropo Apps with Grails
Building Tropo Apps with Grails
Christopher Bartling
Acceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And FriendsAcceptance Test Driven Development With Spec Flow And Friends
Acceptance Test Driven Development With Spec Flow And Friends
Christopher Bartling
Cucumber, Cuke4Duke, and Groovy
Cucumber, Cuke4Duke, and GroovyCucumber, Cuke4Duke, and Groovy
Cucumber, Cuke4Duke, and Groovy
Christopher Bartling
iPhone OS: The Next Killer Platform
iPhone OS: The Next Killer PlatformiPhone OS: The Next Killer Platform
iPhone OS: The Next Killer Platform
Christopher Bartling

CoffeeScript By Example

  • 1. CoffeeScript by Example Christopher Bartling
  • 2. Example 1 Functions Integration with jQuery String interpolation
  • 3. Functions (->) function(){} is replaced by the thin arrow syntax: -> Method body determined by indentation All functions can act as expressions Optional parameters list precedes the thin arrow syntax (name, age, birthDate) -> Parameters can have default values (age, gender = female) ->
  • 4. jQuery integration Hooking into the document.ready event: $(document).ready -> $ -> jQuery selectors need parentheses Function calls typically do not parentheses unless function call has no parameters
  • 5. Strings and string interpolation Double-quoted strings allow for interpolated values Ruby-style interpolation syntax: #{} My name is #{person.fullName} Single-quoted strings are literal Multi-line strings and heredocs are allowed, even using string interpolation
  • 7. Comprehensions Every loop in CoffeeScript returns a value, an array containing the result of every loop iteration (variable for variable in array) Filter iteration results by using when clause: (variable for variable in array when clause) Common idiom to combine function invocation and comprehension
  • 9. Lexical scoping of variables No var keyword in CoffeeScript CoffeeScript doesnt allow global variables unless you explicitly export them Everything wrapped up in anonymous functions to maintain local context Variable assignments automatically pre鍖xed by var in generated JavaScript Impossible to shadow a higher-level variable
  • 11. Classes and inheritance Classes are now 鍖rst-class citizens Use the class keyword Generates to JavaScripts prototype usage Exporting a class to global scope class @ClassName Extension of another objects prototype class SavingsAccount extends Account
  • 12. Constructors CoffeeScript constructors have explicit syntax: class Account constructor: -> Setting instance variables constructor: (@width) -> Calling the superclass constructor, passing along all current arguments: super
  • 13. Object instances Reference an instance variable: @variableName Reference an instance method: @methodName()
  • 14. Example 5 Function Binding Function Context
  • 15. Bound function operator (=>) aka Fat Arrow Binds a function to the current context Retains the context no matter where the function is invoked Avoids the self = this game Prevalent use with event callbacks See this in later advanced examples
  • 17. Unit testing with Jasmine Behavior-driven development (BDD) JavaScript testing framework Why Jasmine? Clean testing framework Good matcher support Integration with sinon.js and jQuery CoffeeScript promotes separation of concerns Makes testing components much easier
  • 18. Example 7 Backbone.js and CoffeeScript Jasmine testing Sinon.js spying
  • 19. Backbone.js Popular JavaScript MVC framework Components Backbone.Model (model) Backbone.View (view) Backbone.Collection and Backbone.Router (controller) CoffeeScript allows easy extension of these classes
  • 20. Where do I go from here? http://jashkenas.github.com/coffee-script/ http://arcturo.github.com/library/coffeescript/index.html http://coffeescriptcookbook.com/ http://peepcode.com/products/coffeescript http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine- sinon.html
  • 21. Tools and Frameworks Jasmine: http://pivotal.github.com/jasmine/ Sinon.js: http://sinonjs.org/ Jasmine-jQuery: https://github.com/velesin/jasmine-jquery Jasmine-Sinon: https://github.com/froots/jasmine-sinon Backbone.js: http://documentcloud.github.com/backbone/ Underscore.js: http://documentcloud.github.com/underscore/
  • 22. Contact Information chris.bartling@gmail.com Twitter: cbartling Blog: http://bartling.blogspot.com Presentation and example code can be found at https://bitbucket.org/ cebartling/coffeescript-stuff

Editor's Notes