Starting Coffeescript

  • Installation
  • Named Functions (global) are not available, Function Expressions with:
    • coffee = (message) ->
    • last line is returned, interpolation just like ruby
    • can be called without parentheses
      • convention is to use parentheses everywhere except on outermost function calls
    • optional parameters just like python
    • anonymous functions: (argument) -> and everything else is the same (sans curly braces and semicolons)
    • instead of this
      • @name instead of this.name
  • compiling
    • once: coffee -c file.coffee
    • watch: coffee -cw file.coffee
    • bunch of coffee files: coffee -c src -o output_dir
      • -o means output into output_dir
  • Conditionals
    • parentheses are optional
    • ruby style works: do_stuff if this_is_true
    • No ternaries! :( instead: if ___ then ___ else
    • no type conversion :(
    • on/off, yes/no
    • unless exists in CoffeeScript!
    • switch statements have gotten a makeover
      • no need to use case or return
    • can use question marks which convert to check whether a variable is undefined or null
    • Ruby’s ||= exists as ?=
      • similarly, if you want to run a method on something only if it exists: in_question?.do_if_true()
        • or, call a function only if it exists: thing.function_in_q?()
        • this is, however, considered a code smell, and shouldn’t be used too much
  • Iteration
    • ruby ranges
    • new lines instead of commas for creating arrays
    • Python for loops rather than JS forEach
      • do_thing for apple in basket when apple isnt rotten
        • yes! isnt is a keyword
        • list comprehension instead of ruby map
        • parentheses are important for list comprehensions, blocks seem to work like parentheses
    • splat is a trailing ellipsis:
    • objects don’t need curly braces or commas
      • do_hash_thingy for key, value of hash
  • OO
    • can declare classes
    • constructor: (@instanceVar1, @instanceVar2) ->
      • will assign args passed in into this.instanceVar1 and 2
      • called property arguments
    • can use extends like extend in Backbone
      • class Child extends Parent
      • super works like in Ruby
    • Fat arrow =>
      • binds current this as the inner closure’s this
  • After CodeSchool
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s