3-2-operators.coffee 999 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ###
  2. Operators
  3. ---------
  4. CoffeScript JavaScript
  5. == is ===
  6. != isnt !==
  7. not !
  8. and &&
  9. or ||
  10. true yes on true
  11. false no off false
  12. ###
  13. paid = coffee = pour = ->
  14. if paid() and coffee() is on then pour()
  15. addCaffeine = Decaf = ->
  16. addCaffeine() if not Decaf()
  17. addCaffeine() unless Decaf()
  18. # Combined range checks
  19. level = 4
  20. if 2 < level < 5
  21. alert "In range"
  22. # Functional switch.
  23. message = switch cupsOfCoffee
  24. when 0 then 'Asleep'
  25. when 1 then 'Eyes open'
  26. when 2 then 'Buzzed'
  27. else 'Dangerous'
  28. # Existential check: not undefined and not null.
  29. if cupsOfCoffee?
  30. alert('Exists')
  31. alert 'Exists' if cupsOfCoffee?
  32. # Not strictly identical to the two shorter forms below.
  33. if not cupsOfCoffee?
  34. cupsOfCoffee = 0
  35. cupsOfCoffee = 0 unless cupsOfCoffee?
  36. cupsOfCoffee ?= 0
  37. coffeePot?.brew()
  38. # Like Ruby "try"
  39. vehicle.start_engine?().shift_gear?().crank?().press_gas?()