coffeescript

39
CoffeeScript Petr Pokorný

Upload: none

Post on 20-Jan-2015

1.310 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: CoffeeScript

CoffeeScript

Petr Pokorný

Page 2: CoffeeScript

Obsah

Co to je?

Proč to používat?

Jak to vypadá?

Jak to rozchodit?

Page 3: CoffeeScript

Co to je?

Page 4: CoffeeScript

Co to je?

CoffeeScript is a little language that compilesinto JavaScript.

Page 5: CoffeeScript

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.

Page 6: CoffeeScript

JavaScript je v jádru docela dobrý jazyk, ale…

Page 7: CoffeeScript

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.

Page 8: CoffeeScript

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

Page 9: CoffeeScript

Proč to používat?

Page 10: CoffeeScript

Proč to používat?

Rychlejší vývoj

Page 11: CoffeeScript

Proč to používat?

Rychlejší vývoj

Méně bugů

Page 12: CoffeeScript

Proč to používat?

Rychlejší vývoj

Méně bugů

Lepší čitelnost

Page 13: CoffeeScript

Jak to vypadá?

JavaScript CoffeeScript

Page 14: 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

Page 15: CoffeeScript

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

Page 16: CoffeeScript

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

Page 17: CoffeeScript

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"

Page 18: CoffeeScript

Na co jste zvyklí z Pythonu

Page 19: CoffeeScript

Loops

# Eat lunch.for food in ['toast', 'cheese', 'wine']:

eat(food)

# Eat lunch.eat food for food in ['toast', 'cheese', 'wine']

Page 20: CoffeeScript

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

}

Page 21: CoffeeScript

Loops

for key in {‘foo’: ‘bar’}

for key, value in {‘foo’: ‘bar’}.items()

for key of {foo: ‘bar’}

for key, value of {foo: ‘bar’}

Page 22: CoffeeScript

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

Page 23: CoffeeScript

Comprehensions

short_names = [name for name in list if len(name) < 5]

shortNames = (name for name in list when name.length < 5)

Page 24: CoffeeScript

Slicing

my_list[3:6]

my_list[3:]

my_list[:-3]

myList[3..5]

myList[3..]

myList[...-3]

Page 25: CoffeeScript

Slicing

my_list[3:6] = [1, 2, 3]

my_list[3:]

my_list[:-3]

myList[3..5] = [1, 2, 3]

myList[3..]

myList[...-3]

Page 26: CoffeeScript

Splats(a.k.a. argument list unpacking)

def foo(bar, *args): pass

foo = (bar, args...) ->

Page 27: CoffeeScript

Chained Comparisons

cholesterol = 127

healthy = 200 > cholesterol > 60

Page 28: CoffeeScript

more...

Page 29: CoffeeScript

The Existential Operator

alert "I knew it!" if elvis?

if (typeof elvis !== "undefined" && elvis !== null) {alert("I knew it!");

}

Page 30: CoffeeScript

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"

Page 31: CoffeeScript

Fat arrow – binding this

Account = (customer, cart) ->@customer = customer@cart = cart

$('.shopping_cart').bind 'click', (event) =>@customer.purchase @cart

Page 32: CoffeeScript

String Interpolation

author = "Wittgenstein"quote = "A picture is a fact. -- #{author}"

sentence = "#{ 22 / 7 } is a decent approximation of π"

Page 33: CoffeeScript

Jak to rozchodit?

Page 34: CoffeeScript

The CoffeeScript compiler is itself written in CoffeeScript.

Page 35: CoffeeScript

nodejs.org

Page 36: CoffeeScript

Node Package Manager

$ curl http://npmjs.org/install.sh | sh

$ npm install -g coffee-script

Page 37: CoffeeScript

$ 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

Page 38: CoffeeScript

<script src="/path/to/coffee-script.js"></script>

<script type="text/coffeescript">

$(document).ready -> alert "Your DOM is ready."

</script>