Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A TGIF about CoffeeScript

A TGIF about CoffeeScript

TGIF about CoffeeScript @MeLi by Salvador Roibon.

Salvador Roibon

April 20, 2015
Tweet

Other Decks in Programming

Transcript

  1. WHAT IS COFFEESCRIPT ? .COFFEE CoffeeScript is a little language

    that compiles into JavaScript. Is an attempt to expose the good parts of JavaScript in a simple way.
  2. SYNTAX BASICS First, the basics: CoffeeScript uses significant whitespace to

    delimit blocks of code. You don't need to use semicolons ; to terminate expressions. Don't use { } , use indentation. You don't need to use parentheses to invoke a function if you're passing arguments. The implicit call wraps forward to the end of the line or block expression.
  3. VARIABLE DECLARACTION & COMMENTS myVar = value # ### The

    CoffeeScript compiler takes care to make sure that all of your variables are properly declared within lexical scope — you never need to write var yourself.
  4. Functions are defined by an optional list of parameters in

    parentheses, an arrow, and the function body. The empty function looks like this: ->
  5. The CoffeeScript literals for objects and arrays look very similar

    to their JavaScript cousins. When each property is listed on its own line, the commas are optional. Objects may be created using indentation instead of explicit braces.
  6. LEXICAL SCOPING AND VARIABLE SAFETY The CoffeeScript compiler takes care

    to make sure that all of your variables are properly declared within lexical scope — you never need to write var yourself.
  7. EVERYTHING IS AN EXPRESSION... at least, as much as possible.

    You might have noticed how even though we don't add return statements to CoffeeScript functions, they nonetheless return their final value. The CoffeeScript compiler tries to make sure that all statements in the language can be used as expressions. Watch how the return gets pushed down into each possible branch of execution in the function below.
  8. OPERATORS AND ALIASES Because the == operator frequently causes undesirable

    coercion, is intransitive, and has a different meaning than in other languages, CoffeeScript compiles == into ===, and != into !==. In addition, is compiles into ===, and isnt into !==.
  9. THE EXISTENTIAL OPERATOR It's a little difficult to check for

    the existence of a variable in JavaScript. if (variable) ... comes close, but fails for zero, the empty string, and false. CoffeeScript's existential operator ? returns true unless a variable is null or undefined, which makes it analogous to Ruby's nil? It can also be used for safer conditional assignment than ||= provides, for cases where you may be handling numbers or strings.
  10. CLASSES @ === THIS Instead of repetitively attaching functions to

    a prototype, CoffeeScript provides a basic class structure that allows you to name your class, set the superclass, assign prototypal properties, and define the constructor, in a single assignable expression.
  11. "PROS": (and some facts about CoffeeScript) It covers up javascript's

    warts It reduces "empty" lines of code You type less code Javascript is way more popular than Coffeescript, at least a 6-to-1 ratio Coffeescript isn't alone — there are lots of languages that compile to Javascript Code is more readable It has BETTER syntax than plain Javascript If both JS and Coffeescript were invented right now, and were competing for marketshare, Coffeescript would win. You like JS only because it's the status quo, and you're familiar with it.
  12. "CONS": (and other facts about CoffeeScript) CoffeeScript is for people

    who hate JavaScript (What? NO!) JavaScript warts should be avoided by education and understanding of the core language Writing Eloquent JavaScript Without CoffeeScript is posible Is harder to maintain (more difficult to find people who knows the language) you need to compile it (one more step added to de development process) Javascript is more understandable, for sure...