SlideShare ist ein Scribd-Unternehmen logo
1 von 22
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 doesn’t allow global variables unless you explicitly export
  them

      Everything wrapped up in anonymous functions to maintain local
      context

      Variable assignments automatically prefixed by var in generated
      JavaScript

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

  Use the class keyword

  Generates to JavaScript’s prototype usage

  Exporting a class to global scope

     class @ClassName

  Extension of another object’s 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

Weitere ähnliche Inhalte

Was ist angesagt?

Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference CountingRobert Brown
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tourcacois
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modulesmonikadeshmane
 
A Practitioner’s guide to Hardened JavaScript
A Practitioner’s guide to Hardened JavaScriptA Practitioner’s guide to Hardened JavaScript
A Practitioner’s guide to Hardened JavaScriptTom Van Cutsem
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Domkaven 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 2010Heiko Behrens
 
React Native Evening
React Native EveningReact Native Evening
React Native EveningTroy 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 androidRakesh Jha
 
What To Expect From PHP7
What To Expect From PHP7What To Expect From PHP7
What To Expect From PHP7Codemotion
 
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 diveepamspb
 
Mac/iOS Design Patterns
Mac/iOS Design PatternsMac/iOS Design Patterns
Mac/iOS Design PatternsRobert Brown
 
Harder Faster Stronger
Harder Faster StrongerHarder Faster Stronger
Harder Faster Strongersnyff
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task QueueDuy Do
 
Boost Maintainability
Boost MaintainabilityBoost Maintainability
Boost MaintainabilityMosky Liu
 
Intro to React
Intro to ReactIntro to React
Intro to ReactTroy Miles
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The ApproachHaci Murat Yaman
 

Was ist angesagt? (20)

Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference Counting
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
 
A Practitioner’s guide to Hardened JavaScript
A Practitioner’s guide to Hardened JavaScriptA Practitioner’s guide to Hardened JavaScript
A Practitioner’s guide to Hardened JavaScript
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
 
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
 
React Native Evening
React Native EveningReact Native Evening
React Native Evening
 
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)
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
What To Expect From PHP7
What To Expect From PHP7What To Expect From PHP7
What To Expect From PHP7
 
Efficient Android Threading
Efficient Android ThreadingEfficient Android Threading
Efficient Android Threading
 
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
 
Mac/iOS Design Patterns
Mac/iOS Design PatternsMac/iOS Design Patterns
Mac/iOS Design Patterns
 
java script
java scriptjava script
java script
 
Harder Faster Stronger
Harder Faster StrongerHarder Faster Stronger
Harder Faster Stronger
 
Node.js essentials
 Node.js essentials Node.js essentials
Node.js essentials
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
 
Boost Maintainability
Boost MaintainabilityBoost Maintainability
Boost Maintainability
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
 

Ähnlich wie CoffeeScript By Example

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.1Michał Orman
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationAjax Experience 2009
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya 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 sirAVINASH KUMAR
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery PluginRavi Mone
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204s4al_com
 
A Scala tutorial
A Scala tutorialA Scala tutorial
A Scala tutorialDima Statz
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depthVinay 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 Libraryrsnarayanan
 
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scalaMichal 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
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsFulvio Corno
 

Ähnlich wie 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
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
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
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
 
Spring
SpringSpring
Spring
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
 
A Scala tutorial
A Scala tutorialA Scala tutorial
A Scala tutorial
 
Lift Framework
Lift FrameworkLift Framework
Lift Framework
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
 
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
 
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scala
 
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...
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
 

Mehr von 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-jvmChristopher Bartling
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaChristopher 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 FriendsChristopher Bartling
 
iPhone OS: The Next Killer Platform
iPhone OS: The Next Killer PlatformiPhone OS: The Next Killer Platform
iPhone OS: The Next Killer PlatformChristopher Bartling
 

Mehr von 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
 
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
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
 
Building Tropo Apps with Grails
Building Tropo Apps with GrailsBuilding Tropo Apps with Grails
Building Tropo Apps with Grails
 
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
 
Introduction To Grails
Introduction To GrailsIntroduction To Grails
Introduction To Grails
 
Cucumber, Cuke4Duke, and Groovy
Cucumber, Cuke4Duke, and GroovyCucumber, Cuke4Duke, and Groovy
Cucumber, Cuke4Duke, and Groovy
 
Test Driven In Groovy
Test Driven In GroovyTest Driven In Groovy
Test Driven In Groovy
 
iPhone OS: The Next Killer Platform
iPhone OS: The Next Killer PlatformiPhone OS: The Next Killer Platform
iPhone OS: The Next Killer Platform
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Grails Overview
Grails OverviewGrails Overview
Grails Overview
 
Rich Web Clients 20081118
Rich Web Clients 20081118Rich Web Clients 20081118
Rich Web Clients 20081118
 

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 doesn’t allow global variables unless you explicitly export them Everything wrapped up in anonymous functions to maintain local context Variable assignments automatically prefixed by var in generated JavaScript Impossible to shadow a higher-level variable
  • 11. Classes and inheritance Classes are now first-class citizens Use the class keyword Generates to JavaScript’s prototype usage Exporting a class to global scope class @ClassName Extension of another object’s 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

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