4-4-objects.coffee 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. coffee = name: 'French', strength: 1
  2. console.log coffee
  3. # Spaced-out writing
  4. coffee =
  5. name: 'French'
  6. strength: 1
  7. console.log coffee
  8. coffee =
  9. name: 'French'
  10. strength: 1
  11. brew: -> console.log "Brewing #{@name}"
  12. pour: (amount = 1) ->
  13. if amount is 1
  14. "Poured a single cup"
  15. else
  16. "Poured #{amount} cups"
  17. coffee.brew()
  18. console.log coffee.pour()
  19. console.log coffee.pour(2)
  20. coffees =
  21. french:
  22. strength: 1
  23. in_stock: 20
  24. italian:
  25. strength: 2
  26. in_stock: 12
  27. decaf:
  28. strength: 0
  29. in_stock: 0
  30. console.log coffees
  31. # For key, value of object. Notice how the "for" applies to everything to its
  32. # left, including the console.log.
  33. console.log "#{coffee} has #{attrs.in_stock}" for coffee, attrs of coffees
  34. # It compiles exactly like this:
  35. for coffee, attrs of coffees
  36. console.log "#{coffee} has #{attrs.in_stock}"
  37. to_print = for coffee, attrs of coffees when attrs.in_stock > 0
  38. "#{coffee} has #{attrs.in_stock}"
  39. console.log to_print.join(", ")