CoffeeScript, the Rise of "Build Your Own JavaScript"

1,304 views
1,201 views

Published on

http://www.infoq.com/presentations/CoffeeScript

Published in: Technology
2 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
1,304
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
9
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

CoffeeScript, the Rise of "Build Your Own JavaScript"

  1. 1. CoffeeScriptThursday, September 22, 2011
  2. 2. The State of CoffeeScriptThursday, September 22, 2011
  3. 3. “It’s just JavaScript”Thursday, September 22, 2011
  4. 4. Under the HoodThursday, September 22, 2011
  5. 5. Scope.prototype.find = function(name, options) {   if (this.check(name, options)) {     return true;   }   this.add(name, var);   return false; };     Thursday, September 22, 2011
  6. 6. Syntax + Semantics + Goodies var square = function(x) { return x * x; };Thursday, September 22, 2011
  7. 7. # Functions...square = (x) -> x * x
  8. 8. # Literals.stooges = [Moe, Curly, Larry]elements = { hydrogen: 1, silicon: 14, uranium: 92}
  9. 9. # Whitespace for blocks.today = "Monday"if today is "Monday" console.log "Strange Loop!"else console.log "Awww..."
  10. 10. # Array, Range, and Object comprehensions.list = [a, b, c]console.log "Hi " + letter for letter in listconsole.log("Hi " + letter for letter in list)for name of process console.log namefor i in [0..10] console.log i
  11. 11. # Even conditional statements.console.log if false 100else 200# Even crazy things like a try/catch.tryResult = try missing.objectcatch err "And the error is: #{err}"console.log tryResult
  12. 12. bestActor = (winner, others...) -> console.log "And the Oscar goes to ... #{winner}!" console.log "(with #{others.length} runners up)"bestActor Gypo Nolan, Clark Gable, Paul Muni, Ludwig Satz
  13. 13. # The existential operator.sue = name: Sue Jones age: 27console.log sue.name.lengthconsole.log sue.non?.existent.propertyconsole.log sue.name.split().reverse?()
  14. 14. # Class literals.shanty = """ Now let every man drink off his full bumper, And let every man drink off his full glass; We will drink and be jolly and drown melancholy, And heres to the health of each true-hearted lass. """class Mariner sing: -> console.log shantyclass UptightSailor extends Mariner sing: -> console.log "Id rather not."class Pirate extends Mariner sing: -> super() console.log AAAAARRRRRRRRRRRRR!new Pirate().sing()
  15. 15. class Person constructor: (@name) -> # body of constructor. introduce: => console.log "Hi, Im #{@name}."groucho = new Person "Groucho Marx"sayHi = groucho.introducesayHi()
  16. 16. futurists = sculptor: "Umberto Boccioni" painter: "Vladimir Burliuk" poet: name: "F.T. Marinetti" address: [ "Via Roma 42R" "Bellagio, Italy 22021" ]{poet: {name: name, address: [street, city]}} = futuristsconsole.log "#{name} lives on #{street}."
  17. 17. # Davids web server example:http = require httpserver = http.createServer (req, res) -> res.writeHead 200, Content-Type: text/plain res.end Ahoy, Strange Loop!server.listen 3000console.log Listening on 3000
  18. 18. # Design Decisions
  19. 19. # Significant Whitespaceif condition do actionif condition then do actiondo action if condition# Other whitespace constructs, like try/catch.try thingcatch error recovertry thing catch error then recover
  20. 20. # Bound vs. Unbound Functions.class Book save: -> jQuery.ajax this.url, this.data, (response) -> merge this.data, response.data# Why are unbound functions necessary in JavaScript?
  21. 21. # Executable Classesclass Pirate loot: -> say "Give me the gold!"# ... and with if/else.class Pirate if century > 1700 loot: -> say "Give me the gold!" else loot: -> say "¡Dame el oro!"# ... and with wrapped functions.class Pirate loot: heartily -> say "Give me the gold!"
  22. 22. # Comprehensions (a little bit of everything)for item in list process itemfor key, value of object process value# Own keys...for own key, value of object process value# Filtering keys and values.for num in list when num % 2 is 0 even num# Value of a comprehension?
  23. 23. B.Y.O.J.Thursday, September 22, 2011
  24. 24. Build Your Own JavaScriptThursday, September 22, 2011
  25. 25. Thursday, September 22, 2011
  26. 26. It’s OK to Cheat.Thursday, September 22, 2011
  27. 27. p4js ReParse Canopy OMeta Cruiser.Parse JS/CC Antlr jsparse JSGLRThursday, September 22, 2011
  28. 28. });Thursday, September 22, 2011

×