5. Co to je?
CoffeeScript is a little language that compiles
into JavaScript.
The code compiles one-to-one into the
equivalent JS, and there is no interpretation
at runtime.
7. JavaScript je v jádru docela dobrý jazyk, ale…
“ JavaScript had to look like Java only less so, be
Java’s dumb kid brother or boy-hostage sidekick.
8. JavaScript je v jádru docela dobrý jazyk, ale…
“ JavaScript had to look like Java only less so, be
Java’s dumb kid brother or boy-hostage sidekick.
Plus, I had to be done in ten days or something
worse than JavaScript would have happened.
”
— Brendan Eich
16. If, Else, Conditional Assignment
var date, mood;
if (singing) { mood = greatlyImproved if singing
mood = greatlyImproved;
}
if (happy && knowsIt) { if happy and knowsIt
clapsHands(); clapsHands()
chaChaCha(); chaChaCha()
} else { else
showIt(); showIt()
}
date = friday ? sue : jill; date = if friday then sue else jill
JavaScript CoffeeScript
17. OOP
var Animal, Horse, Snake, sam, tom;
var __hasProp = Object.prototype.hasOwnProperty, __extends =
class Animal
function(child, parent) { constructor: (@name) ->
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
move: (meters) ->
child.prototype = new ctor; child.__super__ = parent.prototype; alert @name +" moved "+ meters +"m."
return child;};Animal = (function() {
function Animal(name) { this.name = name; }
Animal.prototype.move = function(meters) { class Snake extends Animal
return alert(this.name + " moved " + meters + "m.");
}; move: ->
return Animal;})();
Snake = (function() { __extends(Snake, Animal); function
alert "Slithering..."
Snake() { Snake.__super__.constructor.apply(this, arguments); super 5
} Snake.prototype.move = function() { alert("Slithering...");
return Snake.__super__.move.call(this, 5); }; return
Snake;})(); class Horse extends Animal
Horse = (function() { __extends(Horse, Animal); function
Horse() { move: ->
}
Horse.__super__.constructor.apply(this, arguments); alert "Galloping..."
Horse.prototype.move = function() { super 45
alert("Galloping..."); return
Horse.__super__.move.call(this, 45);
}; return Horse;})(); sam = new Snake "Sammy the Python"
sam = new Snake("Sammy the Python");
tom = new Horse("Tommy the Palomino"); tom = new Horse "Tommy the Palomino"
JavaScript CoffeeScript
19. Loops
# Eat lunch.
for food in ['toast', 'cheese', 'wine']:
eat(food)
# Eat lunch.
eat food for food in ['toast', 'cheese', 'wine']
20. Loops
# Eat lunch.
for food in ['toast', 'cheese', 'wine']:
eat(food)
# Eat lunch.
eat food for food in ['toast', 'cheese', 'wine']
var food, _i, _len, _ref;
_ref = ['toast', 'cheese', 'wine'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
food = _ref[_i];
eat(food);
}
21. Loops
for key in {‘foo’: ‘bar’}
for key, value in {‘foo’: ‘bar’}.items()
for key of {foo: ‘bar’}
for key, value of {foo: ‘bar’}
22. Ranges
for i in range(1, 10)
for i in range(10, 1, -1)
for i in range(1, 10, 2)
for i in [1..9] # nebo [1...10]
for i in [10..2]
for i in [1..9] by 2