SlideShare uma empresa Scribd logo
1 de 64
Baixar para ler offline
S

CoffeeScript

No really, it’s just Javascript
Brian Mann
@be_mann
Atlanta, GA
What is CoffeeScript?

• Exposes the good parts
• Transpiles directly to JS
• Removes / Adds new syntax
• Enhances readability, and productivity
• Clearly expresses intents
Agenda

• An Emotional Assessment
• Dive Into Features
• Real World Experiences
• Challenges / Critiques
I am Biased.
CoffeeScript
is one of the most
BEAUTIFUL

/

UGLY

LOVED

/

HATED

EMBRACED

/

FEARED

LANGUAGES.

/

TOOLS.

/

SYNTAX.
Javascript
Landscape.
Golden Age
S

Golden Age

CoffeeScript
Why the hate?

• CoffeeScript looks different
• CoffeeScript acts different
FUD.
Fear. Uncertainty. Doubt.
Crutch.
Absolutely

NOT.
S
Nothing To Fear.
Its just Javascript.
Okay thx.
Code plz.
Before we begin

•
• No more commas
• Optional parenthesis
• Optional curly braces
• White space sensitive

No more semi-colons ;
,
()
{}
Vars.
var is, no, more;
Vars are long gone
var count, increment;

JAVASCRIPT

count = 0;
increment = function() {
var total;
count += 1;
return total = "The total is: " + count;
};

count = 0

COFFEESCRIPT

increment = ->
count += 1
total = "The total is: " + count
Rule:

/
100

99
Object Literals.
Objects Simplified
JAVASCRIPT

var person = {
name: "Toby Ho",
language: "javascript",
likes: ["node", "testem", "jazz"]
};

COFFEESCRIPT

person =
name: "Toby Ho"
language: "javascript"
likes: ["node", "testem", "jazz"]
Functions.
Functions in JS
function eat(){
console.log("eating");
};

var eat = function(){
console.log("eating");
};

DECLARATION

EXPRESSION
Functions in CS
COFFEESCRIPT
eat = ->
console.log "eating"

JAVASCRIPT
var eat;
eat = function() {
return console.log("eating");
};
Function Arguments
COFFEESCRIPT
eat = (options = {}) ->

JAVASCRIPT
var eat;
eat = function(options) {
if (options == null) {
options = {};
}
};
Function Context
JAVASCRIPT

COFFEESCRIPT

var person;
person = {
name: "Toby Ho",
energy: 0,
eat: function() {
return this.energy += 10;
}
};

> person.eat();
# 10

person =
name: "Toby Ho"
energy: 0
eat: ->
@energy += 10
Function Binding
JAVASCRIPT
var person;
person = {
name: "Toby Ho",
energy: 0,
work: function() {
var _this = this;
$("#stop").click(function() {
return _this.energy -= 20;
});
}
};

COFFEESCRIPT
person =
name: "Toby Ho"
energy: 0
work: ->
$("#stop").click =>
@energy -= 20
Function Binding
JAVASCRIPT
var person;

COFFEESCRIPT

person =
name: "Toby Ho"
person = {
energy: 0
name: "Toby Ho",
person = {
work: ->
energy: 0,
name: "Toby Ho",
$("#stop").click =>
energy: 0, work: function() {
@energy -= 20
return $("#stop").click((function(_this) {
work: function() {
return function() {
var _this = this;
return _this.energy -= 20;
$("#stop").click(function() {
};
return _this.energy -= 20;
})(this));
});
}
}
};
};
var person;
Function Splats
JAVASCRIPT
var person;

COFFEESCRIPT
person =
name: "Toby Ho"
dislikes: []
addDislikes: (args...) ->
@dislikes.push args...

person = {
name: "Toby Ho",
dislikes: [],
addDislikes: function() {
var args = [].slice.call(arguments);
return [].push.apply(this.dislikes, args);
}
};

> person.addDislikes(“frameworks”, “long plane rides”);
Ranges / Slicing
Array + String Slicing
COFFEESCRIPT

JAVASCRIPT

nums = [1..10]

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

nums[0...5]

nums.slice(0, 5);

[1,2,3,4,5]

nums[2..3] = [-3, -4]

[].splice.apply(nums,
[2,2].concat([-3, -4])
);

[1,2,-3,-4,5,
6,7,8,9,10]

"Toby Ho"[0..3]

"Toby Ho".slice(0, 4);

Toby
If, Else, Unless.
Flow Control
person =
name: "Toby Ho"
tired: true
work: ->
return if @tired
@program "javascript"
relax: ->

var person;

person = {
name: "Toby Ho",
tired: true,
work: function() {
if (this.tired) {
if moment.hour() > 10 then @read() else @tv()
return;
sleep: ->
}
@relax() unless @tired
return this.program("javascript");
},
relax: function() {
moment.hour() > 10 ? this.read() : this.tv();
},
sleep: function() {
if (!this.tired) {
return this.relax();
}
}
};

COFFEESCRIPT

JAVASCRIPT
Operators.
Operator Comparison
COFFEESCRIPT

JAVASCRIPT

is

===

true is true

isnt

!==

@name isnt "randall"

not

!

and

&&

or

||

true yes

on

true

false no

off

false

relax() if not working()
exercise() unless @tired and @sleepy

happy() if @happy or @energy

> 50
Existential
Operator.
Do You Really Exist?
person =
name: "Toby Ho"
energy: 0
address:
city: "Alpharetta"
state: "GA"
zip: 30307
phone: null
mail: -> "mailing..."

var _ref;
if ((_ref = person.address) != null) {
_ref.city;
}
if (typeof person.call === "function") {
person.call();
}

> person.address?.city

=>

Alpharetta

> person.phone?.cell

=>

undefined

> person.call?( )

=>

undefined

> person.call?( ) ? person.mail( )

=>

mailing...
String
Interpolation.
No more awkward ‘+’
JAVASCRIPT
"Hey, " + person.name + " is " + person.age + " years young.";

COFFEESCRIPT
"Hey, #{person.name} is #{person.age} years young."
Destructuring
Assignment.
Easy var extraction
person =
name: "Toby Ho"
energy: 0
address:
city: "Alpharetta"
state: "GA"
zip: 30307

COFFEESCRIPT
{name, energy, address} = person

JAVASCRIPT
var address, energy, name;
name
= person.name;
energy = person.energy;
address = person.address;
IIFE’s.
Module Pattern
JAVASCRIPT
App = (function() {
var privateVar = "can't get to me!";
var privateFn = function() {
return "can't invoke me either";
};
var obj = {
data: [],
props: {},
publicFn: function() {}
};
return obj;
})();

COFFEESCRIPT
App = do ->
privateVar = "can't get to me!"
privateFn = -> "can't invoke me either"
obj =
data: []
props: {}
publicFn: ->
return obj
Local Dependencies
JAVASCRIPT
(function($, BB) {
//reference jQuery as $
//reference Backbone as BB
})(jQuery, Backbone);

COFFEESCRIPT
do ($ = jQuery, BB = Backbone) ->
#reference jQuery as $
#reference Backbone as BB
Switch / Case.
...minus the case
COFFEESCRIPT
switch
when
when
when
else

state
"GA"
then "humid"
"WA"
then "rainy"
"AZ", "UT", "NV" then "dry"
"moderate"

JAVASCRIPT
switch (state) {
case "GA":
"humid";
break;
case "WA":
"rainy";
break;
case "AZ":
case "UT":
case "NV":
"dry";
break;
default:
"moderate";
}
No Control Expression
COFFEESCRIPT
energyLevel = switch
when @energy < 10 then "exhausted"
when @energy < 20 then "okay"
when @energy < 40 then "caffeinated"
else "bouncing off walls"

JAVASCRIPT
var energyLevel;
energyLevel = (function() {
switch (false) {
case !(this.energy < 10):
return "exhausted";
case !(this.energy < 20):
return "okay";
case !(this.energy < 40):
return "caffeinated";
default:
return "bouncing off walls";
}
}).call(this);
Loops /
Comprehensions.
For Loops
person =
name: "Toby Ho"
language: "javascript"
likes: ["node", "testem", "jazz"]

COFFEESCRIPT
for like, num in person.likes
console.log "#{num + 1}. #{like}"
var like, num, _i, _len, _ref;

=>

1. node
2. testem
3. jazz

JAVASCRIPT

_ref = person.likes;
for (num = _i = 0, _len = _ref.length; _i < _len; num = ++_i) {
like = _ref[num];
console.log("" + (num + 1) + ". " + like);
}
Property Iteration
heros = { john: "resig", ryan: "dahl" };

COFFEESCRIPT
for first, last of heros
console.log first, last

var first, last;

JAVASCRIPT

for (first in heros) {
last = heros[first];
console.log(first, last);
}

=>

john resig
ryan dahl
Array ‘in’ checking
person =
name: "Toby Ho"
dislikes: ["frameworks", "long plane rides"]

COFFEESCRIPT
if "coffeescript" in person.dislikes then "hater" else "friend :-)"

var __indexOf = [].indexOf || function(item) { for (var i = 0, l
= this.length; i < l; i++) { if (i in this && this[i] === item)
return i; } return -1; };
if (__indexOf.call(this.dislikes, "coffeescript") >= 0) {
"hater";
} else {
"friend :-)";
}

JAVASCRIPT
Comprehensions
{100 Objects}

COFFEESCRIPT
nums = (num: i for i in [1..100])

var i, nums;

=>

JAVASCRIPT

nums = (function() {
var _i, _results;
_results = [];
for (i = _i = 1; _i <= 100; i = ++_i) {
_results.push({
num: i
});
}
return _results;
})();

-------------------------[{
num: 1
},{
num: 2
}]
What was missed?

•
• Block Regular Expressions
Some Operators
•
• Chained Comparisons
Trailing Commas
•
• Reserved Words
Literate Coffeescript
•
• Classes, Inheritance, & Super
Block Strings
Debugging.
Syntax Errors
@PE.module "HeaderApp", +>

SUBLIME TEXT
Source Maps
CoffeeConsole
Literal CoffeeScript
Criticisms.
Dispelling the Myths

• Compiler Errors (WAT?)
• Performance
• Restrictions*
• Harder to Debug
• Less Readability
• You can skip learning JS
Lies,
Damned Lies,
& Statistics.
Productivity Gains
LINES OF
CODE

34,014

84,681

2.49X

CHARS

1,198,518

2,704,135

2.25X
S

CoffeeScript
The End
Brian Mann
@be_mann

Mais conteúdo relacionado

Mais procurados

Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.js
Jarod Ferguson
 
Mulberry: A Mobile App Development Toolkit
Mulberry: A Mobile App Development ToolkitMulberry: A Mobile App Development Toolkit
Mulberry: A Mobile App Development Toolkit
Rebecca Murphey
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Thomas Fuchs
 

Mais procurados (20)

5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Why ruby
Why rubyWhy ruby
Why ruby
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.js
 
Ruby
RubyRuby
Ruby
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To Closure
 
Testing javascriptwithjasmine sydjs
Testing javascriptwithjasmine sydjsTesting javascriptwithjasmine sydjs
Testing javascriptwithjasmine sydjs
 
Rails on Oracle 2011
Rails on Oracle 2011Rails on Oracle 2011
Rails on Oracle 2011
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Proxies are Awesome!
Proxies are Awesome!Proxies are Awesome!
Proxies are Awesome!
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
 
Mulberry: A Mobile App Development Toolkit
Mulberry: A Mobile App Development ToolkitMulberry: A Mobile App Development Toolkit
Mulberry: A Mobile App Development Toolkit
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Roles
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
 
A New Baseline for Front-End Devs
A New Baseline for Front-End DevsA New Baseline for Front-End Devs
A New Baseline for Front-End Devs
 

Semelhante a Coffeescript: No really, it's just Javascript

Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
jeffz
 
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony CodeGroovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
stasimus
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 

Semelhante a Coffeescript: No really, it's just Javascript (20)

TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
Coffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotesCoffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotes
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Introduction to ECMAScript 2015
Introduction to ECMAScript 2015
 
Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScript
 
Fantom and Tales
Fantom and TalesFantom and Tales
Fantom and Tales
 
Coffee script
Coffee scriptCoffee script
Coffee script
 
Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
ES6 at PayPal
ES6 at PayPalES6 at PayPal
ES6 at PayPal
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
SOLID Ruby, SOLID Rails
SOLID Ruby, SOLID RailsSOLID Ruby, SOLID Rails
SOLID Ruby, SOLID Rails
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
 
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony CodeGroovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Coffeescript: No really, it's just Javascript