The document discusses CoffeeScript, a programming language that compiles to JavaScript. It provides examples of CoffeeScript syntax for functions, conditionals, loops, classes, and other constructs. It explains how CoffeeScript aims to have less syntactic noise than JavaScript while remaining true to the underlying JavaScript semantics through compilation.
18. Conditions
if happy and knowsIt
clapsHands()
chaChaCha()
else
showIt()
date = if friday then sue else jill
options or= defaults
number = -42 if opposite
19. Functions
square = (x) -> x * x
cube = (x) -> square(x) * x
fill = (container, liquid="coffee") ->
"Filling the #{container} with #{liquid}..."
noop = ->
20. In JQuery
$('span').each ->
it = $(this)
if it.is('.style-1, .style-20') or
not it.attr('class')
it.replaceWith(it.html())
21. Data
# Arrays:
list = [1, 2, 3, 4, 5]
# Objects:
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
38. Closing Over Loops
for filename in list
do (filename) ->
fs.readFile filename, (err, contents) ->
compile filename, contents.toString()
39. Operator Names
CoffeeScript JavaScript
is, isnt ===, !==
and, or, not &&, ||, !
true, yes, on true
false, no, off false
in, of in, N/A
@, this this
:: prototype
41. Classes
class Animal
constructor: (@name) ->
move: (meters) ->
alert @name + " moved " + meters + "m."
class Snake extends Animal
move: ->
alert "Slithering..."
super 5
class Horse extends Animal
move: ->
alert "Galloping..."
super 45
sam = new Snake "Sammy the Python"
tom = new Horse "Tommy the Palomino"
sam.move()
tom.move()
42. var Animal, Horse, Snake, sam, tom;
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;
};
60. Image Tributes (CC)
"Black as midnight on a moonless night."
Knight of the Holy Grail
Reflecting on a Sunday afternoon
In The Red Room
Fall down mountains, just don't fall on me
Midnight Black
Orrery Steam Punk Assemblage by urbandon
Steampunk Time Contraption