coffeescript by example

22
CoffeeScript by Example Christopher Bartling

Upload: christopher-bartling

Post on 29-Jan-2018

9.523 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CoffeeScript By Example

CoffeeScript by Example

Christopher Bartling

Page 2: CoffeeScript By Example

Example 1

FunctionsIntegration with jQuery

String interpolation

Page 3: CoffeeScript By Example

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”) ->

Page 4: CoffeeScript By Example

jQuery integrationHooking into the document.ready event:

$(document).ready -> $ ->

jQuery selectors need parentheses

Function calls typically do not parentheses unless function call has no parameters

Page 5: CoffeeScript By Example

Strings and string interpolationDouble-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

Page 6: CoffeeScript By Example

Example 2

Comprehensions

Page 7: CoffeeScript By Example

ComprehensionsEvery 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

Page 8: CoffeeScript By Example

Example 3

Lexical Scoping

Page 9: CoffeeScript By Example

Lexical scoping of variablesNo 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

Page 10: CoffeeScript By Example

Example 4

Classes and Objects

Page 11: CoffeeScript By Example

Classes and inheritanceClasses 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

Page 12: CoffeeScript By Example

ConstructorsCoffeeScript constructors have explicit syntax:

class Account constructor: ->

Setting instance variables

constructor: (@width) ->

Calling the superclass constructor, passing along all current arguments:

super

Page 13: CoffeeScript By Example

Object instancesReference an instance variable:

@variableName

Reference an instance method:

@methodName()

Page 14: CoffeeScript By Example

Example 5

Function Binding Function Context

Page 15: CoffeeScript By Example

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

Page 16: CoffeeScript By Example

Example 6

Using Jasmine

Page 17: CoffeeScript By Example

Unit testing with JasmineBehavior-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

Page 18: CoffeeScript By Example

Example 7

Backbone.js and CoffeeScriptJasmine testing Sinon.js spying

Page 19: CoffeeScript By Example

Backbone.jsPopular JavaScript MVC framework

Components

Backbone.Model (model)

Backbone.View (view)

Backbone.Collection and Backbone.Router (controller)

CoffeeScript allows easy extension of these classes

Page 21: CoffeeScript By Example

Tools and FrameworksJasmine: 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/