Post on 10-May-2015
description
transcript
CoffeeScript
CoffeeScript - Intro
@d_danailov
CoffeeScript : Intro
Dimitar DanailovSenior Developer at 158ltd.com
dimityr.danailov[at]gmail.comGithub
Founder at VarnaITSenior Developer at 158ltd.com
Cleaner Code With CoffeeScriptGithub Code
Topics Today
● Introduction● Installing CoffeeScript● Using CoffeeScript Files● Learning the Core Functionality● Data Types● Comments and Functions● Operators
Topics Today (2)
● Control Structures● Loops and Comprehensions● Scope and Context● Stirring in Advanced Concepts● Object Prototypes● CoffeeScript Classes● Heregexes● Using CoffeeScript in the Browser
Installing CoffeeScript
1. Node.js2. NPMsudo apt-get install nodejs npm &&
3. sudo npm install -g coffee-script4. coffee -v
Using CoffeeScript Files
node.js file.jscoffee file.coffeecoffee -c file.coffeecoffee -cw file.coffeecoffee (read–eval–print loop (REPL))
Variables (2)
Destructuring Assignment :
theBait = 1000
theSwitch = 0
[theBait, theSwitch ] = [theSwitch, theBait]
Variables (3)
Destructuring Assignment :weatherReport = (location) ->
# Make an Ajax request to fetch the weather...
[location, 72, "Mostly Sunny"]
[city, temp, forecast ] = weatherReport "Berkeley, CA"
Comments
# Single line comment
###
Multi line comment
###
Functions
Splats :
The JavaScript arguments object is a useful way to work with functions that
accept variable numbers of arguments. CoffeeScript provides splats ..., both
for function definition as well as invocation, making variable numbers of
arguments a little bit more palatable.
# Splats:
race = (winner, runners...) ->
print winner, runners
Operators● JS : == / Coffee : is ● JS : != / Coffee : isnt● JS : if (!false) / Coffee : if not false ● JS : && / Coffee : AND● JS : || / Coffee : OR
Condition statements
● if● if not● unless● switch and when
Loops and Comprehensions
Most of the loops you'll write in CoffeeScript will be comprehensions over arrays, objects, and ranges. Comprehensions replace (and compile into) for loops, with optional guard clauses and the value of the current array index. Unlike for loops, array comprehensions are expressions, and can be returned and assigned.
Loops
● for● while● until ● loop used with break !!!
Scope
● coffee -c --bare file.coffee● this● root = (export ? window)
Regular Expressions
Similar to block strings and comments, CoffeeScript supports block regexes — extended regular expressions that ignore internal whitespace and can contain comments and interpolation. Modeled after Perl's /x modifier, CoffeeScript's block regexes are delimited by /// and go a long way towards making complex regular expressions readable.
Using CoffeeScript in the Browser
● coffee -c --watch *.coffee● coffee -c --watch o js src/*.coffee● http://coffeescript.org/#scripts
Questions ?
Dimitar DanailovSenior Developer at 158ltd.com
dimityr.danailov[at]gmail.comGithub
Founder at VarnaITSenior Developer at 158ltd.com
Cleaner Code With CoffeeScriptGithub Code