CoffeeScript

A programming language that transcompiles to JavaScript.

Inspired by Ruby, Python and Haskell in the name of brevity and readability.

Specific additional features include list comprehension and pattern matching.

Installation

npm install -g coffee-script

Basics

# Assignment:
number   = 42
opposite = true

Conditions:

number = -42 if opposite

Functions:

square = (x) -> x * x

Arrays:

list = [1, 2, 3, 4, 5]

Objects:

math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x

Splats:

race = (winner, runners...) ->
  print winner, runners

Existence:

alert "I knew it!" if elvis?

Array comprehensions:

cubes = (math.cube num for num in list)

Functions

square = (x) -> x * x
cube   = (x) -> square(x) * x

vs JS

var cube, square;

square = function(x) {
  return x * x;
};

cube = function(x) {
  return square(x) * x;
};

Default Values

fill = (container, liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."

Examples

MyPackageView = require './my-package-view'

module.exports =
  myPackageView: null

  activate: (state) ->
    @myPackageView = new MyPackageView(state.myPackageViewState)

  deactivate: ->
    @myPackageView.destroy()

  serialize: ->
    myPackageViewState: @myPackageView.serialize()

References

results matching ""

    No results matching ""