CoffeeScript
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,616
On Slideshare
1,611
From Embeds
5
Number of Embeds
1

Actions

Shares
Downloads
14
Comments
0
Likes
0

Embeds 5

http://webexpo.innit.cz 5

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. CoffeeScript Petr Pokorný
  • 2. ObsahCo to je?Proč to používat?Jak to vypadá?Jak to rozchodit?
  • 3. Co to je?
  • 4. Co to je?CoffeeScript is a little language that compilesinto JavaScript.
  • 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. JavaScript je v jádru docela dobrý jazyk, ale…
  • 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
  • 9. Proč to používat?
  • 10. Proč to používat?Rychlejší vývoj
  • 11. Proč to používat?Rychlejší vývojMéně bugů
  • 12. Proč to používat?Rychlejší vývojMéně bugůLepší čitelnost
  • 13. Jak to vypadá?JavaScript CoffeeScript
  • 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. 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. 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. 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. Na co jste zvyklí z Pythonu
  • 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. Loopsfor key in {‘foo’: ‘bar’}for key, value in {‘foo’: ‘bar’}.items()for key of {foo: ‘bar’}for key, value of {foo: ‘bar’}
  • 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. Comprehensionsshort_names = [name for name in list if len(name) < 5]shortNames = (name for name in list when name.length < 5)
  • 24. Slicingmy_list[3:6]my_list[3:]my_list[:-3]myList[3..5]myList[3..]myList[...-3]
  • 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. Splats(a.k.a. argument list unpacking)def foo(bar, *args): passfoo = (bar, args...) ->
  • 27. Chained Comparisons cholesterol = 127 healthy = 200 > cholesterol > 60
  • 28. more...
  • 29. The Existential Operator alert "I knew it!" if elvis?if (typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!");}
  • 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. Fat arrow – binding thisAccount = (customer, cart) -> @customer = customer @cart = cart $(.shopping_cart).bind click, (event) => @customer.purchase @cart
  • 32. String Interpolationauthor = "Wittgenstein"quote = "A picture is a fact. -- #{author}"sentence = "#{ 22 / 7 } is a decent approximation of π"
  • 33. Jak to rozchodit?
  • 34. The CoffeeScript compiler isitself written in CoffeeScript.
  • 35. nodejs.org
  • 36. Node Package Manager$ curl http://npmjs.org/install.sh | sh$ npm install -g coffee-script
  • 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. <script src="/path/to/coffee-script.js"></script><script type="text/coffeescript"> $(document).ready -> alert "Your DOM is ready."</script>
  • 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/