coffeescript
DESCRIPTION
TRANSCRIPT
CoffeeScript
Petr Pokorný
Obsah
Co to je?
Proč to používat?
Jak to vypadá?
Jak to rozchodit?
Co to je?
Co to je?
CoffeeScript is a little language that compilesinto JavaScript.
Co to je?
CoffeeScript is a little language that compilesinto JavaScript.
The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime.
JavaScript je v jádru docela dobrý jazyk, ale…
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.
“
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
“
”
Proč to používat?
Proč to používat?
Rychlejší vývoj
Proč to používat?
Rychlejší vývoj
Méně bugů
Proč to používat?
Rychlejší vývoj
Méně bugů
Lepší čitelnost
Jak to vypadá?
JavaScript CoffeeScript
Functions
JavaScript CoffeeScript
var cube, square; square = function(x) {
return x * x;}; cube = function(x) {
return square(x) * x;};
square = (x) -> x * x
cube = (x) -> square(x) * x
Objects
JavaScript CoffeeScript
var kids = { brother: {name: "Max",age: 11
}, sister: {name: "Ida",age: 9
}};
kids =brother:name: "Max"age: 11
sister:name: "Ida"age: 9
If, Else, Conditional Assignment
JavaScript CoffeeScript
var date, mood;if (singing) {mood = greatlyImproved;
}if (happy && knowsIt) {clapsHands();chaChaCha();
} else {showIt();
}date = friday ? sue : jill;
mood = greatlyImproved if singing
if happy and knowsItclapsHands()chaChaCha()
elseshowIt()
date = if friday then sue else jill
OOP
JavaScript CoffeeScript
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;};Animal = (function() { function Animal(name) { this.name = name; } Animal.prototype.move = function(meters) {return alert(this.name + " moved " + meters + "m.");
}; return Animal;})();Snake = (function() { __extends(Snake, Animal); function Snake() { Snake.__super__.constructor.apply(this, arguments); } Snake.prototype.move = function() { alert("Slithering..."); return Snake.__super__.move.call(this, 5); }; return Snake;})();Horse = (function() { __extends(Horse, Animal); function Horse() {
Horse.__super__.constructor.apply(this, arguments);}
Horse.prototype.move = function() { alert("Galloping..."); return
Horse.__super__.move.call(this, 45);}; return Horse;})();sam = new Snake("Sammy the Python");tom = new Horse("Tommy the Palomino");
class Animalconstructor: (@name) ->
move: (meters) ->alert @name +" moved "+ meters +"m."
class Snake extends Animalmove: ->alert "Slithering..."super 5
class Horse extends Animalmove: ->alert "Galloping..."super 45
sam = new Snake "Sammy the Python"tom = new Horse "Tommy the Palomino"
Na co jste zvyklí z Pythonu
Loops
# Eat lunch.for food in ['toast', 'cheese', 'wine']:
eat(food)
# Eat lunch.eat food for food in ['toast', 'cheese', 'wine']
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);
}
Loops
for key in {‘foo’: ‘bar’}
for key, value in {‘foo’: ‘bar’}.items()
for key of {foo: ‘bar’}
for key, value of {foo: ‘bar’}
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
Comprehensions
short_names = [name for name in list if len(name) < 5]
shortNames = (name for name in list when name.length < 5)
Slicing
my_list[3:6]
my_list[3:]
my_list[:-3]
myList[3..5]
myList[3..]
myList[...-3]
Slicing
my_list[3:6] = [1, 2, 3]
my_list[3:]
my_list[:-3]
myList[3..5] = [1, 2, 3]
myList[3..]
myList[...-3]
Splats(a.k.a. argument list unpacking)
def foo(bar, *args): pass
foo = (bar, args...) ->
Chained Comparisons
cholesterol = 127
healthy = 200 > cholesterol > 60
more...
The Existential Operator
alert "I knew it!" if elvis?
if (typeof elvis !== "undefined" && elvis !== null) {alert("I knew it!");
}
Destructuring Assignment
theBait = 1000theSwitch = 0
[theBait, theSwitch] = [theSwitch, theBait]
weatherReport = (location) -># Make an Ajax request to fetch the weather...[location, 72, "Mostly Sunny"]
[city, temp, forecast] = weatherReport "Berkeley, CA"
Fat arrow – binding this
Account = (customer, cart) ->@customer = customer@cart = cart
$('.shopping_cart').bind 'click', (event) =>@customer.purchase @cart
String Interpolation
author = "Wittgenstein"quote = "A picture is a fact. -- #{author}"
sentence = "#{ 22 / 7 } is a decent approximation of π"
Jak to rozchodit?
The CoffeeScript compiler is itself written in CoffeeScript.
nodejs.org
Node Package Manager
$ curl http://npmjs.org/install.sh | sh
$ npm install -g coffee-script
$ coffee --watch --compile *.coffee
19:45:31 - compiled myscript.coffee
19:47:22 - compiled myscript.coffee
In myscript.coffee, too many ) on line 39
19:47:40 - compiled myscript.coffee
19:48:10 - compiled myscript.coffee
19:48:47 - compiled myscript.coffee
In myscript.coffee, Parse error on line 3: Unexpected 'INDENT'
19:49:23 - compiled myscript.coffee
<script src="/path/to/coffee-script.js"></script>
<script type="text/coffeescript">
$(document).ready -> alert "Your DOM is ready."
</script>
Odkazy
node.js
http://nodejs.org/
npm
http://npmjs.org/
Introduction to CoffeeScript
http://screencasts.org/episodes/introduction-to-coffeescript/
Js2Coffee – The JavaScript to CoffeeScript compiler.
http://ricostacruz.com/js2coffee/
CoffeeScript homepagehttp://jashkenas.github.com/coffee-script/