Upload
kamikaze-media-as
View
805
Download
3
Embed Size (px)
DESCRIPTION
En introduksjon til CoffeeScript.
Citation preview
Det er bare JavaSript
Kompilert
hello.coffee:
alert “hei”
Kompilert
Live kompilering
<script type="text/javascript" src="coffeescript.js"></script>
<script type="text/javascript" src="hello.coffee"></script>
Live kompilering
<script type="text/javascript" src="coffeescript.js"></script>
<script type="text/coffeescript"> alert “hei”</script>
$ coffee -c hello.coffee$ lshello.coffee hello.js
Indent
grade: (student) -> if student.excellent_work “A+” else if student.okay_stuff if student.tried_hard then “B” else “B-” else “C”
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”; }};
Funksjonersquare = (x) -> x * xcube = (x) -> square(x) * x
var cube, square;square = function(x) { return x * x;};cube = function(x) { return square(x) * x;};
Verdisetting
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;})();
Operators/aliaser
CoffeeScript
isisntnotandor
true, yes, onfalse, no, off
@, thisofin
JavaScript
===!==
!&&!!
truefalsethisin
finnes ikke
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();
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);
The existential operator
if hungry? eat()
if (typeof hungry !== “undefined” && hungry !== null) { eat();}
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();
Klasser
class Animal constructor: (@name) ->
move: (meters) -> alert @name + " moved #{meters}m."
class Snake extends Animal move: -> alert "Slithering..." super 5
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;})();
coffeescript.com