Share
Embed
Introducción a CoffeeScript
Plática para el FSL en Puerto Vallarta México 2012
Mario Alberto Chávez
November 01, 2012
Transcript
41 slides
-
Presentado en 1995 por Netscape sintaxis influenciada por C enfocado
al lado del cliente y servidor(1994) estandarizado por ECMAScript popular y utilizado en todos los navegadores (1996) -
Lenguaje de script basado en prototipos dinámico y relajado en
tipos orientado a objetos*, imperativo y funcional sintaxis llena de (), {} y ; un slot del prototipo puede contener datos y código saltó fuera del navegador -
podemos usar librerías de Javascript (uff podemos usar jQuery) Foto:
http://www.tumblr.com/tagged/googiefaces -
Funciones square = (x) -> x * x square =
function(x){ return x * x; } -
Funciones square = (x = 0) -> x * x
square = function(x){ if (x == null) { x = 0; } return x * x; } -
Funciones names = ['mario', 'edwin', 'paul'] presenters = (first, others...)
-> first presenters names... var names, presenters, __slice = [].slice; names = ['mario', 'edwin', 'paul']; presenters = function() { var first, others; first = arguments[0], others = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return first; }; presenters.apply(null, names); -
Funciones names = ['mario', 'edwin', 'paul'] presenters = (first, others...)
-> [first, others] [current, next] = presenters(names...) current var current, names, next, presenters, _ref, __slice = [].slice; names = ['mario', 'edwin', 'paul']; presenters = function() { var first, others; first = arguments[0], others = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return [first, others]; }; _ref = presenters.apply(null, names), current = _ref[0], next = _ref[1]; current; -
Sintaxis thursday = true alert ‘Jueves’ if thursday day =
4 alert ‘Jueves’ if day is 4 var thursday = true; if (thursday) { alert('Jueves'); } var day = 4; if (day === 4) { alert('Jueves'); } -
Sintaxis thursday = true alert if thursday then 'Jueves' else
'Otro dia' alert 'Otro dia' unless thursday alert 'Jueves' if today? var thursday = true; alert(thursday ? 'Jueves' : 'Otro dia'); if (!thursday) { alert('Otro dia'); } if (typeof today !== "undefined" && today ! == null) { alert('Jueves'); } -
Ciclos numbers = (num for num in [1..10]) var numbers
= (function() { var _i, _results; _results = []; for (num = _i = 1; _i <= 10; num = ++_i) { _results.push(num); } return _results; })(); -
Comprehensions numbers = (num for num in [1..10]) odds =
(number for number in numbers when number%2 isnt 0) odds var num, number, numbers, odds; odds = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = numbers.length; _i < _len; _i+ +) { number = numbers[_i]; if (number % 2 !== 0) { _results.push(number); } } return _results; })(); odds; -
Objetos presenters = mario: 1 edwin: 2 paul: 2 day:
(name) -> @[name] presenters.day 'edwin' var presenters; presenters = { mario: 1, edwin: 2, paul: 2, day: function(name) { return this[name]; } }; presenters.day('edwin'); -
For each presenters = mario: 1 edwin: 2 paul: 2
present = for name, day of presenters "#{name} presenta el dia #{day}" var day, name, present, presenters; present = (function() { var _results; _results = []; for (name in presenters) { day = presenters[name]; _results.push("" + name + " presenta el dia " + day); } return _results; })(); -
Clases class Vehicle constructor: (@name) -> maxSpeed: -> "no speed
for #{@name}" class Car extends Vehicle maxSpeed: -> "210 k/h for #{@name}" class Airplane extends Vehicle maxSpeed: -> "885 k/h for #{@name}" car = new Car 'My car' airplane = new Airplane 'My plane' car.maxSpeed() airplane.maxSpeed() -
Clases var Airplane, Car, Vehicle, airplane, car, __hasProp = {}.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; }; Vehicle = (function() { function Vehicle(name) { this.name = name; } Vehicle.prototype.maxSpeed = function() { return "no speed for " + this.name; }; return Vehicle; })(); Car = (function(_super) { __extends(Car, _super); function Car() { return Car.__super__.constructor.apply(this, arguments); } Car.prototype.maxSpeed = function() { return "210 k/h for " + this.name; }; return Car; })(Vehicle); -
{ } } { { { } } } }{
{ { }{ } } _____ __ __ ( }{ }{ { ) / ____| / _|/ _| .- { { } { }} -. | | ___ | |_| |_ ___ ___ ( ( } { } { } } ) | | / _ \| _| _/ _ \/ _ \ |`-..________ ..-'| | |___| (_) | | | || __/ __/ | | \_____\___/|_| |_| \___|\___| | ;--. | (__ \ _____ _ _ | | ) ) / ____| (_) | | | |/ / | (___ ___ _ __ _ _ __ | |_ | ( / \___ \ / __| '__| | '_ \| __| | |/ ____) | (__| | | | |_) | |_ | | |_____/ \___|_| |_| .__/ \__| `-.._________..-' | | |_| Es Open Source -
{ } } { { { } } } }{
{ { }{ } } _____ __ __ ( }{ }{ { ) / ____| / _|/ _| .- { { } { }} -. | | ___ | |_| |_ ___ ___ ( ( } { } { } } ) | | / _ \| _| _/ _ \/ _ \ |`-..________ ..-'| | |___| (_) | | | || __/ __/ | | \_____\___/|_| |_| \___|\___| | ;--. | (__ \ _____ _ _ | | ) ) / ____| (_) | | | |/ / | (___ ___ _ __ _ _ __ | |_ | ( / \___ \ / __| '__| | '_ \| __| | |/ ____) | (__| | | | |_) | |_ | | |_____/ \___|_| |_| .__/ \__| `-.._________..-' | | |_| Es Open Source -
Github: https://github.com/jashkenas/ coffee-script CoffeeScript: http://coffeescript.org/ js2cs: http://js2coffee.org/ CoffeeScript Little book:
http:// arcturo.github.com/library/ coffeescript/