160 likes | 408 Views
CoffeeScript. Katie Soto Casey Foster. Origin. 2009 , Jeremy Ashkenas made the first Git commit of CoffeeScript Compiler originally written in Ruby ; pure CoffeeScript Compiler in 2010. 2010 stable release Jeremy Ashkenas .. worked for DocumentCloud
E N D
CoffeeScript Katie Soto Casey Foster
Origin • 2009, Jeremy Ashkenas made the first Git commit of CoffeeScript • Compileroriginally written in Ruby; pure CoffeeScript Compiler in 2010. • 2010 stable release • Jeremy Ashkenas.. • worked for DocumentCloud • produced several open source projects • backbone.js • underscore.js. • two-time winner of the Apps for America contest • Know Thy Congressman • Quakespotter • created Ruby-Processing
About • Compiles into JavaScript • Is an attempt to expose the good parts of JavaScript in a simple way. • The golden rule: "It's just JavaScript" • Code compiles one-to-one into the equivalent JS • No interpretation at runtime • Can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa).
Example • CoffeeScript allows you to: • Imply curly braces with indentation • Use natural words like "and, then, else or" • Leave off semi-colons • It's like if Ruby and Python had a lovechild
More examples JavaScript Multiply each item in a list by 2 vari, num, nums;nums= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];for (iinnums) { num =nums[i];nums[i] = num * 2;} Sum a list of numbers var n, sum;sum = 0;for (n = 1; n <= 100; n++) { sum += n;} CoffeeScript Multiply each item in a list by 2 nums= [1..10] fori, num ofnums nums[i] = num*2 Sum a list of numbers sum = 0 for n in [1..100] sum += n
Classes CoffeeScript class Animal constructor: (@name) -> who: -> alert @name goat = new Animal 'Billy' goat.who() JavaScript var Animal, goat;Animal = (function() { function Animal(name) { this.name = name; }Animal.prototype.who = function() { return alert(this.name); }; return Animal;})();goat = new Animal('Billy');goat.who();
Inheritance CoffeeScript class Goat extends Animal who: -> alert "I am #{@name} the Goat!" JavaScript varGoat, __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; }; Goat = (function(_super) { __extends(Goat, _super); function Goat() {Goat.__super__.constructor.apply(this, arguments); } Goat.prototype.who = function() { return alert("I am " + this.name + " the Goat!"); }; return Goat; })(Animal);
Summary • CoffeeScript is awesome • It accentuates the best parts of JavaScript • It is Ruby and Python's lovechild, stealing every shortcut it can from them • It makes classes and inheritance tolerable to read • And underneath it all... It's just JavaScript!
Equivalent JavaScript Demo CoffeeScript