CoffeeScript   Petr Pokorný
ObsahCo 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 theequivalent JS...
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 bro...
JavaScript je v jádru docela dobrý jazyk, ale…“   JavaScript had to look like Java only less so, be    Java’s dumb kid bro...
Proč to používat?
Proč to používat?Rychlejší vývoj
Proč to používat?Rychlejší vývojMéně bugů
Proč to používat?Rychlejší vývojMéně bugůLepší čitelnost
Jak to vypadá?JavaScript     CoffeeScript
Functionsvar cube, square;square = function(x) {   return x * x;           square = (x) -> x * x};cube = function(x) {    ...
Objectsvar kids = {            kids =   brother: {             brother:     name: "Max",           name: "Max"     age: 11...
If, Else, Conditional Assignmentvar date, mood;if (singing) {                mood = greatlyImproved if singing  mood = gre...
OOPvar Animal, Horse, Snake, sam, tom;var __hasProp = Object.prototype.hasOwnProperty, __extends =                        ...
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 fo...
Loopsfor key in {‘foo’: ‘bar’}for key, value in {‘foo’: ‘bar’}.items()for key of {foo: ‘bar’}for key, value of {foo: ‘bar’}
Rangesfor 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...
Comprehensionsshort_names = [name for name in list if len(name) < 5]shortNames = (name for name in list when name.length <...
Slicingmy_list[3:6]my_list[3:]my_list[:-3]myList[3..5]myList[3..]myList[...-3]
Slicingmy_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): passfoo = (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) {  aler...
Destructuring AssignmenttheBait = 1000theSwitch = 0[theBait, theSwitch] = [theSwitch, theBait]weatherReport = (location) -...
Fat arrow – binding thisAccount = (customer, cart) ->  @customer = customer  @cart = cart  $(.shopping_cart).bind click, (...
String Interpolationauthor = "Wittgenstein"quote = "A picture is a fact. -- #{author}"sentence = "#{ 22 / 7 } is a decent ...
Jak to rozchodit?
The CoffeeScript compiler isitself written in CoffeeScript.
nodejs.org
Node Package Manager$ curl http://npmjs.org/install.sh | sh$ npm install -g coffee-script
$ coffee --watch --compile *.coffee19:45:31 - compiled   myscript.coffee19:47:22 - compiled   myscript.coffeeIn myscript.c...
<script src="/path/to/coffee-script.js"></script><script type="text/coffeescript">  $(document).ready -> alert "Your DOM i...
OdkazyCoffeeScript homepagehttp://jashkenas.github.com/coffee-script/node.jshttp://nodejs.org/npmhttp://npmjs.org/Introduc...
Upcoming SlideShare
Loading in...5
×

CoffeeScript

1,378

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,378
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "CoffeeScript"

  1. 1. CoffeeScript Petr Pokorný
  2. 2. ObsahCo to je?Proč to používat?Jak to vypadá?Jak to rozchodit?
  3. 3. Co to je?
  4. 4. Co to je?CoffeeScript is a little language that compilesinto JavaScript.
  5. 5. Co to je?CoffeeScript is a little language that compilesinto JavaScript.The code compiles one-to-one into theequivalent JS, and there is no interpretationat runtime.
  6. 6. JavaScript je v jádru docela dobrý jazyk, ale…
  7. 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. 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
  9. 9. Proč to používat?
  10. 10. Proč to používat?Rychlejší vývoj
  11. 11. Proč to používat?Rychlejší vývojMéně bugů
  12. 12. Proč to používat?Rychlejší vývojMéně bugůLepší čitelnost
  13. 13. Jak to vypadá?JavaScript CoffeeScript
  14. 14. Functionsvar cube, square;square = function(x) { return x * x; square = (x) -> x * x};cube = function(x) { cube = (x) -> square(x) * x return square(x) * x;}; JavaScript CoffeeScript
  15. 15. Objectsvar kids = { kids = brother: { brother: name: "Max", name: "Max" age: 11 age: 11 }, sister: sister: { name: "Ida" name: "Ida", age: 9 age: 9 }}; JavaScript CoffeeScript
  16. 16. If, Else, Conditional Assignmentvar 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. 17. OOPvar Animal, Horse, Snake, sam, tom;var __hasProp = Object.prototype.hasOwnProperty, __extends = class Animalfunction(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); }; returnSnake;})(); class Horse extends AnimalHorse = (function() { __extends(Horse, Animal); functionHorse() { move: -> } Horse.__super__.constructor.apply(this, arguments); alert "Galloping..."Horse.prototype.move = function() { super 45 alert("Galloping..."); returnHorse.__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
  18. 18. Na co jste zvyklí z Pythonu
  19. 19. Loops# Eat lunch.for food in [toast, cheese, wine]: eat(food)# Eat lunch.eat food for food in [toast, cheese, wine]
  20. 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. 21. Loopsfor key in {‘foo’: ‘bar’}for key, value in {‘foo’: ‘bar’}.items()for key of {foo: ‘bar’}for key, value of {foo: ‘bar’}
  22. 22. Rangesfor 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
  23. 23. Comprehensionsshort_names = [name for name in list if len(name) < 5]shortNames = (name for name in list when name.length < 5)
  24. 24. Slicingmy_list[3:6]my_list[3:]my_list[:-3]myList[3..5]myList[3..]myList[...-3]
  25. 25. Slicingmy_list[3:6] = [1, 2, 3]my_list[3:]my_list[:-3]myList[3..5] = [1, 2, 3]myList[3..]myList[...-3]
  26. 26. Splats(a.k.a. argument list unpacking)def foo(bar, *args): passfoo = (bar, args...) ->
  27. 27. Chained Comparisons cholesterol = 127 healthy = 200 > cholesterol > 60
  28. 28. more...
  29. 29. The Existential Operator alert "I knew it!" if elvis?if (typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!");}
  30. 30. Destructuring AssignmenttheBait = 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"
  31. 31. Fat arrow – binding thisAccount = (customer, cart) -> @customer = customer @cart = cart $(.shopping_cart).bind click, (event) => @customer.purchase @cart
  32. 32. String Interpolationauthor = "Wittgenstein"quote = "A picture is a fact. -- #{author}"sentence = "#{ 22 / 7 } is a decent approximation of π"
  33. 33. Jak to rozchodit?
  34. 34. The CoffeeScript compiler isitself written in CoffeeScript.
  35. 35. nodejs.org
  36. 36. Node Package Manager$ curl http://npmjs.org/install.sh | sh$ npm install -g coffee-script
  37. 37. $ coffee --watch --compile *.coffee19:45:31 - compiled myscript.coffee19:47:22 - compiled myscript.coffeeIn myscript.coffee, too many ) on line 3919:47:40 - compiled myscript.coffee19:48:10 - compiled myscript.coffee19:48:47 - compiled myscript.coffeeIn myscript.coffee, Parse error on line 3: Unexpected INDENT19:49:23 - compiled myscript.coffee
  38. 38. <script src="/path/to/coffee-script.js"></script><script type="text/coffeescript"> $(document).ready -> alert "Your DOM is ready."</script>
  39. 39. OdkazyCoffeeScript homepagehttp://jashkenas.github.com/coffee-script/node.jshttp://nodejs.org/npmhttp://npmjs.org/Introduction to CoffeeScripthttp://screencasts.org/episodes/introduction-to-coffeescript/Js2Coffee – The JavaScript to CoffeeScript compiler.http://ricostacruz.com/js2coffee/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×