Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CoffeeScript

1,034 views

Published on

This is just a quick introduction of CoffeeScript for people to get started

Published in: Technology
  • Be the first to comment

  • Be the first to like this

CoffeeScript

  1. 1. Getting Started with Akshay Mathur
  2. 2. CoffeeScript • A language with simple syntax – No semicolons and braces – Resembles to English – Indentation decides the code blocks • Compiles into Javascript – Provides syntactic sugar for boilerplate code • Manage variable scope • Class instead of prototype – Generates good quality, error free code @akshaymathu 2
  3. 3. Insallation • Install Node JS and Package Manager (npm) – See http://nodejs.org/download/ for details • Install CoffeeScript – See http://coffeescript.org/ for details sudo npm install -g coffee- script
  4. 4. Coffee to JS • Compile a .coffee file into .js file using coffee compiler coffee -c myfile.coffee Creates myfile.js – See http://coffeescript.org/ for more options and examples
  5. 5. Cofeescript to Javascript greet_me = (name) -> greeting_word = 'Hello!' alert "#{greeting_word} #{name}” Compiles to greet_me = function(name) { var greeting_word; greeting_word = 'Hello!'; return alert("" + greeting_word + " " + name); }; @akshaymathu 5
  6. 6. Variable Scoping • Defines all variables in the first line of the function – This all variables become private by default • Adds an anonymous wrapper function at the top – Even global remains within the closure – If you want to make something available outside, attach it to an object outside (e.g. window object) @akshaymathu 6
  7. 7. Functions • No ‘function’ keyword – Use -> instead • No curly braces {} – Indent the code instead • Last statement always returns • No parenthesis while calling the function if argument is being passed • Default values can be used in function signature @akshaymathu 7
  8. 8. Function Examples Fn = -> Add = (x, y=0) -> x + y Add 3 var Fn, Add; Fn = function(){}; Add = function(x, y) { if ( y == null){ y = 0; } return x + y; }; Add(3); @akshaymathu 8
  9. 9. Conditional Statements mood = Improved if singing if happy and knowsIt clapsHands() chaChaCha() else showIt() date = if friday then sue else jill var date, mood; if (singing) { mood = Improved; } if (happy && knowsIt) { clapsHands(); chaChaCha(); } else { showIt(); } date = friday ? sue : jill; @akshaymathu 9
  10. 10. Looping on Arrays using ‘in’ foods = [ 'broccoli’ 'spinach’ 'chocolate’ ] eat food for food in foods when food isnt 'chocolate' foods = ['broccoli', 'spinach', 'chocolate']; for (_k = 0, _len2 = foods.length; _k < _len2; _k++) { food = foods[_k]; if (food !== 'chocolate') { eat(food); } } @akshaymathu 10
  11. 11. Looping on Objects using ‘of’ yearsOld = max: 10 ida: 9 tim: 11 ages = for child, age of yearsOld "#{child} is #{age}" var age, ages, child, yearsOld; yearsOld = { max: 10, ida: 9, tim: 11 }; ages = (function() { var _results; _results = []; for (child in yearsOld) { age = yearsOld[child]; _results.push( "" + child + " is " + age ); } return _results; })(); @akshaymathu 11
  12. 12. Operators is === isnt !== not ! and && or || true, yes, on true false, no, off false @, this this of in in No JS equivalent @akshaymathu 12
  13. 13. Thanks @akshaymathu 13

×