SlideShare a Scribd company logo
1 of 44
Download to read offline
JavaScript “bien hecho”
Yeray Darias
CoffeeScript
ydarias@gmail.com
developerscookbook.blogspot.com
ydarias
Antes de CoffeeScript
fue JavaScript
Un poco de historia reciente
Finales del año 1994 ...
En el año 1995 ...
Brendan Eich idea y programa
10Días
Mochaen apenas
Mocha
LiveScript
JavaScript
Allá por el año 1997 ...
A principios del 2000 ...
JavaScript tenía algunos amigos
pero noeramuypopular
En la actualidad ...
JavaScript está en todas partes
Javascript
¿Qué demonios es
CoffeeScript?
Pongamonos en situación
Es una idea que tuvo
“CoffeeScript it’s just
JavaScript”
CoffeeScript es
un lenguaje de programación
que “genera”
JavaScript
JavaScriptWTFs
Douglas Crockford
JavaScript
The good parts
Global variables
CoffeeScriptsolved!
var message = “hola”;!
!
function saluda() {!
return message;!
}
var message = “adios”;!
!
function despidete() {!
return message;!
}
Global variables
Si ambos ficheros están “cargados”,
¿Cuál es la salida esperada en cada
función?
Global variables
CoffeeScript no crea variables globales de
forma implícita
(function() {!
var message, saluda;!
!
message = "hola";!
!
saluda = function() {!
return message;!
};!
}).call(this);
message = "hola"!
!
saluda = () -> !
message
Scopes
Scopes
Todas las variables en una función se
declaran al principio
(function() {!
var message, saluda;!
!
message = "hola";!
!
saluda = function() {!
return message;!
};!
}).call(this);
message = "hola"!
!
saluda = () -> !
message
Semicolons
CoffeeScriptsolved!
Semicolons
CoffeeScript autocompleta los ;
(function() {!
var message, saluda;!
!
message = "hola";!
!
saluda = function() {!
return message;!
};!
}).call(this);
message = "hola"!
!
saluda = () -> !
message
Reserved words
CoffeeScriptsolved!
Reserved words
CoffeeScript escapa las palabras reservadas
automáticamente
var myObject;!
!
myObject = {!
"case": 'keyword',!
tutu: 'allowed name'!
};
myObject = !
case: 'keyword'!
tutu: 'allowed name'
typeof
Equality comparisons
CoffeeScriptsolved!
Equality comparisons
CoffeeScript siempre usa los operadores
como ===, !==, ...
var testFunction;!
!
testFunction =
function(input) {!
if (input === 'string') {!
'string';!
!
}!
if (input === 9) {!
return 'number nine';!
}!
};
testFunction = (input) -> !
if (input == 'string')!
'string'!
if (input == 9)!
'number nine'
eval()
continue
switch fall through
CoffeeScriptsolved!
switch fall through
Bond = (input) -> !
switch input!
when 'Sean Connery', 'Daniel Craig'!
'Fucking crack'!
when 'Roger Moore'!
'A bit boring'
var Bond;!
!
Bond = function(input) {!
switch (input) {!
case 'Sean Connery':!
case 'Daniel Craig':!
return 'Fucking crack';!
case 'Roger Moore':!
return 'A bit boring';!
}!
};!
Todas las opciones acaban con un return
Global variables
Scopes
Semicolons
Reserved words
typeof
Equality comparison
eval()
continue
Switch fall through
CoffeeScript“fabrica”
bloques JavaScript
Control de flujo
mood = greatlyImproved if singing!
!
if happy and knowsIt!
clapsHands()!
else!
showIt()!
!
date = if friday then sue else jill!
!
isToday = yes unless yesterday or tomorrow!
!
// -----------------------------------------------------!
!
cholesterol = 127!
!
healthy = 200 > cholesterol > 60!
Chained comparisons
Everything a expression
Control de flujo
Bond = (input) -> !
switch input!
when 'Sean Connery', 'Daniel Craig'!
'Fucking crack'!
when 'Roger Moore'!
'A bit boring'!
else!
'No comments'
Bucles
# Health conscious meal.!
foods = ['broccoli', 'spinach', 'chocolate']!
eat food for food in foods when food isnt 'chocolate'!
!
countdown = (num for num in [10..1])!
!
# Econ 101!
if this.studyingEconomics!
buy() while supply > demand!
sell() until supply > demand!
!
while age < 18!
canNotSmoke()!
canNotDrink()
Funciones
square = (x) -> x * x!
cube = (x) -> square(x) * x!
!
fill = (container, liquid = "coffee") ->!
"Filling the #{container} with #{liquid}..."!
!
// --------------------------------------------------!
!
awardMedals = (first, second, others...) ->!
gold = first!
silver = second!
rest = others!
!
!
Splats
String interpolation
Operators & aliases
CoffeeScript JavaScript
is ===
isnt !==
not !
and &&
or ||
true, yes, on true
false, no, off false
@, this this
of in
in no JS equivalent
‘The’ operator
?
es el operador existencial en
CoffeeScript
Clases
class Animal!
constructor: (@name) ->!
!
move: (meters) ->!
alert @name + " moved #{meters}m."!
!
class Snake extends Animal!
move: ->!
alert "Slithering..."!
super 5!
!
class Horse extends Animal!
move: ->!
alert "Galloping..."!
super 45
Clases
!
!
sam = new Snake "Sammy the Python"!
tom = new Horse "Tommy the Palomino"!
!
sam.move()!
tom.move()!
Function binding
Account = (customer, cart) ->!
@customer = customer!
@cart = cart!
!
$('.shopping_cart').bind 'click', (event) =>!
@customer.purchase @cart!
!
var Account;!
!
Account = function(customer, cart) {!
var _this = this;!
this.customer = customer;!
this.cart = cart;!
return $('.shopping_cart').bind('click', function(event) {!
return _this.customer.purchase(_this.cart);!
});!
};!
Ahoratocaprogramar

More Related Content

Similar to Coffee Script

When Devs Do Ops
When Devs Do OpsWhen Devs Do Ops
When Devs Do OpsWooga
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptKevin Ball
 
CoffeeScript, An Introduction for Nodejs developers
CoffeeScript, An Introduction for Nodejs developersCoffeeScript, An Introduction for Nodejs developers
CoffeeScript, An Introduction for Nodejs developersMehdi Valikhani
 
Desert Code Camp 2014: C#, the best programming language
Desert Code Camp 2014: C#, the best programming languageDesert Code Camp 2014: C#, the best programming language
Desert Code Camp 2014: C#, the best programming languageJames Montemagno
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsLeonardo Borges
 
Why I will never write JavaScript ever again*
Why I will never write JavaScript ever again*Why I will never write JavaScript ever again*
Why I will never write JavaScript ever again*The Wolff
 

Similar to Coffee Script (10)

Quick coffeescript
Quick coffeescriptQuick coffeescript
Quick coffeescript
 
When Devs Do Ops
When Devs Do OpsWhen Devs Do Ops
When Devs Do Ops
 
test
testtest
test
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Coffee script throwdown
Coffee script throwdownCoffee script throwdown
Coffee script throwdown
 
CoffeeScript, An Introduction for Nodejs developers
CoffeeScript, An Introduction for Nodejs developersCoffeeScript, An Introduction for Nodejs developers
CoffeeScript, An Introduction for Nodejs developers
 
Desert Code Camp 2014: C#, the best programming language
Desert Code Camp 2014: C#, the best programming languageDesert Code Camp 2014: C#, the best programming language
Desert Code Camp 2014: C#, the best programming language
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
Why I will never write JavaScript ever again*
Why I will never write JavaScript ever again*Why I will never write JavaScript ever again*
Why I will never write JavaScript ever again*
 
Coffeescript slides
Coffeescript slidesCoffeescript slides
Coffeescript slides
 

More from Yeray Darias

Runnics at ProductHunt Madrid
Runnics at ProductHunt MadridRunnics at ProductHunt Madrid
Runnics at ProductHunt MadridYeray Darias
 
Otogami at Tetuan Valley
Otogami at Tetuan ValleyOtogami at Tetuan Valley
Otogami at Tetuan ValleyYeray Darias
 
Runnics en Betabeers Madrid
Runnics en Betabeers MadridRunnics en Betabeers Madrid
Runnics en Betabeers MadridYeray Darias
 
Introducción a la arquitectura software
Introducción a la arquitectura softwareIntroducción a la arquitectura software
Introducción a la arquitectura softwareYeray Darias
 
Introducción a jQuery
Introducción a jQueryIntroducción a jQuery
Introducción a jQueryYeray Darias
 
Integración Continua - TLP+i 2K10
Integración Continua - TLP+i 2K10Integración Continua - TLP+i 2K10
Integración Continua - TLP+i 2K10Yeray Darias
 

More from Yeray Darias (9)

Runnics at ProductHunt Madrid
Runnics at ProductHunt MadridRunnics at ProductHunt Madrid
Runnics at ProductHunt Madrid
 
Otogami at Tetuan Valley
Otogami at Tetuan ValleyOtogami at Tetuan Valley
Otogami at Tetuan Valley
 
Runnics en Betabeers Madrid
Runnics en Betabeers MadridRunnics en Betabeers Madrid
Runnics en Betabeers Madrid
 
Agile and Scrum
Agile and ScrumAgile and Scrum
Agile and Scrum
 
Introducción a la arquitectura software
Introducción a la arquitectura softwareIntroducción a la arquitectura software
Introducción a la arquitectura software
 
Scrum
ScrumScrum
Scrum
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
Introducción a jQuery
Introducción a jQueryIntroducción a jQuery
Introducción a jQuery
 
Integración Continua - TLP+i 2K10
Integración Continua - TLP+i 2K10Integración Continua - TLP+i 2K10
Integración Continua - TLP+i 2K10
 

Recently uploaded

SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 

Recently uploaded (20)

SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 

Coffee Script