coffeescript - en introduksjon

22

Upload: kamikaze-media-as

Post on 05-Dec-2014

805 views

Category:

Documents


3 download

DESCRIPTION

En introduksjon til CoffeeScript.

TRANSCRIPT

Page 1: CoffeeScript - en introduksjon
Page 2: CoffeeScript - en introduksjon

Det er bare JavaSript

Page 3: CoffeeScript - en introduksjon

Kompilert

Page 4: CoffeeScript - en introduksjon

hello.coffee:

alert “hei”

Kompilert

Page 5: CoffeeScript - en introduksjon

Live kompilering

<script type="text/javascript" src="coffeescript.js"></script>

<script type="text/javascript" src="hello.coffee"></script>

Page 6: CoffeeScript - en introduksjon

Live kompilering

<script type="text/javascript" src="coffeescript.js"></script>

<script type="text/coffeescript"> alert “hei”</script>

Page 7: CoffeeScript - en introduksjon

$ coffee -c hello.coffee$ lshello.coffee hello.js

Page 8: CoffeeScript - en introduksjon

Indent

grade: (student) -> if student.excellent_work “A+” else if student.okay_stuff if student.tried_hard then “B” else “B-” else “C”

Page 9: CoffeeScript - en introduksjon

var grade = function(student) { if (student.excellent_work) { return “A + ”; } else if (student.okay_stuff) { if (student.tried_hard) { return “B”; } else { return “B - ”; } } else { return “C”; }};

Page 10: CoffeeScript - en introduksjon

Funksjonersquare = (x) -> x * xcube = (x) -> square(x) * x

var cube, square;square = function(x) { return x * x;};cube = function(x) { return square(x) * x;};

Page 11: CoffeeScript - en introduksjon

Verdisetting

Page 12: CoffeeScript - en introduksjon

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;})();

Page 13: CoffeeScript - en introduksjon

Operators/aliaser

Page 14: CoffeeScript - en introduksjon

CoffeeScript

isisntnotandor

true, yes, onfalse, no, off

@, thisofin

JavaScript

===!==

!&&!!

truefalsethisin

finnes ikke

Page 15: CoffeeScript - en introduksjon

launch() if ignition is onif (ignition === true) launch();

volume = 10 if band isnt SpinalTapif (band !== SpinalTap) volume = 10;

letTheWildRumpusBegin() unless answer is noif (answer !== false) letTheWildRumpusBegin();

Page 16: CoffeeScript - en introduksjon

if car.speed < limit then accelerate()if (car.speed < limit) accelerate();

winner = yes if pick in [47, 92, 13]if (pick === 47 || pick === 92 || pick === 13) winner = true;

print "My name is #{@name}"print("My name is " + this.name);

Page 17: CoffeeScript - en introduksjon

The existential operator

if hungry? eat()

if (typeof hungry !== “undefined” && hungry !== null) { eat();}

Page 18: CoffeeScript - en introduksjon

launch() if ignition is onif (ignition === true) launch();

volume = 10 if band isnt SpinalTapif (band !== SpinalTap) volume = 10;

letTheWildRumpusBegin() unless answer is noif (answer !== false) letTheWildRumpusBegin();

Page 19: CoffeeScript - en introduksjon

Klasser

Page 20: CoffeeScript - en introduksjon

class Animal constructor: (@name) ->

move: (meters) -> alert @name + " moved #{meters}m."

class Snake extends Animal move: -> alert "Slithering..." super 5

Page 21: CoffeeScript - en introduksjon

var Animal, Snake;var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child;};Animal = (function() { function Animal(name) { this.name = name; } Animal.prototype.move = function(meters) { return alert(this.name + (" moved " + meters + "m.")); }; return Animal;})();Snake = (function() { __extends(Snake, Animal); function Snake() { Snake.__super__.constructor.apply(this, arguments); } Snake.prototype.move = function() { alert("Slithering..."); return Snake.__super__.move.call(this, 5); }; return Snake;})();

Page 22: CoffeeScript - en introduksjon

coffeescript.com