CoffeeScript - en introduksjon

821 views
736 views

Published on

En introduksjon til CoffeeScript.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
821
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

CoffeeScript - en introduksjon

  1. 1. Det er bare JavaSript
  2. 2. Kompilert
  3. 3. Kompilert hello.coffee: alert “hei”
  4. 4. Live kompilering<script type="text/javascript" src="coffeescript.js"></script><script type="text/javascript" src="hello.coffee"></script>
  5. 5. Live kompilering<script type="text/javascript" src="coffeescript.js"></script><script type="text/coffeescript"> alert “hei”</script>
  6. 6. $ coffee -c hello.coffee$ lshello.coffee hello.js
  7. 7. Indentgrade: (student) -> if student.excellent_work “A+” else if student.okay_stuff if student.tried_hard then “B” else “B-” else “C”
  8. 8. 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”; }};
  9. 9. Funksjonersquare = (x) -> x * xcube = (x) -> square(x) * xvar cube, square;square = function(x) { return x * x;};cube = function(x) { return square(x) * x;};
  10. 10. Verdisetting
  11. 11. yearsOld = max: 10, ida: 9, tim: 11ages = 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;})();
  12. 12. Operators/aliaser
  13. 13. CoffeeScript JavaScript is === isnt !== not ! and && or !!true, yes, on truefalse, no, off false @, this this of in in finnes ikke
  14. 14. 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();
  15. 15. 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);
  16. 16. The existential operatorif hungry? eat()if (typeof hungry !== “undefined” && hungry !== null) { eat();}
  17. 17. 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();
  18. 18. Klasser
  19. 19. class Animal constructor: (@name) -> move: (meters) -> alert @name + " moved #{meters}m."class Snake extends Animal move: -> alert "Slithering..." super 5
  20. 20. 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;
  21. 21. coffeescript.com

×