coffeescript intro

21
CoffeeScript CoffeeScript - Intro @d_danailov

Upload: dimitar-danailov

Post on 10-May-2015

1.491 views

Category:

Technology


1 download

DESCRIPTION

Demos created for public lecture in VarnaLab (02 October 2013) Github Demos : https://github.com/dimitardanailov/coffeescript-intro Agenda : Introduction Installing CoffeeScript Using CoffeeScript Files Learning the Core Functionality Data Types Comments and Functions Operators Control Structures Loops and Comprehensions Scope and Context Stirring in Advanced Concepts Object Prototypes CoffeeScript Classes Heregexes Using CoffeeScript in the Browser

TRANSCRIPT

Page 3: Coffeescript   intro

Topics Today

● Introduction● Installing CoffeeScript● Using CoffeeScript Files● Learning the Core Functionality● Data Types● Comments and Functions● Operators

Page 4: Coffeescript   intro

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

Page 5: Coffeescript   intro

Installing CoffeeScript

1. Node.js2. NPMsudo apt-get install nodejs npm &&

3. sudo npm install -g coffee-script4. coffee -v

Page 6: Coffeescript   intro

Using CoffeeScript Files

node.js file.jscoffee file.coffeecoffee -c file.coffeecoffee -cw file.coffeecoffee (read–eval–print loop (REPL))

Page 7: Coffeescript   intro

Variables

Booleans :● true/false● yes/no● on/off

Page 8: Coffeescript   intro

Variables (2)

Destructuring Assignment :

theBait = 1000

theSwitch = 0

[theBait, theSwitch ] = [theSwitch, theBait]

Page 9: Coffeescript   intro

Variables (3)

Destructuring Assignment :weatherReport = (location) ->

# Make an Ajax request to fetch the weather...

[location, 72, "Mostly Sunny"]

[city, temp, forecast ] = weatherReport "Berkeley, CA"

Page 10: Coffeescript   intro

Comments

# Single line comment

###

Multi line comment

###

Page 11: Coffeescript   intro

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

Page 12: Coffeescript   intro

Operators● JS : == / Coffee : is ● JS : != / Coffee : isnt● JS : if (!false) / Coffee : if not false ● JS : && / Coffee : AND● JS : || / Coffee : OR

Page 13: Coffeescript   intro

Condition statements

● if● if not● unless● switch and when

Page 14: Coffeescript   intro

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.

Page 15: Coffeescript   intro

Loops

● for● while● until ● loop used with break !!!

Page 16: Coffeescript   intro

Scope

● coffee -c --bare file.coffee● this● root = (export ? window)

Page 19: Coffeescript   intro

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.

Page 20: Coffeescript   intro

Using CoffeeScript in the Browser

● coffee -c --watch *.coffee● coffee -c --watch o js src/*.coffee● http://coffeescript.org/#scripts