SlideShare une entreprise Scribd logo
1  sur  39
Télécharger pour lire hors ligne
CoffeeScript
   Petr Pokorný
Obsah


Co to je?
Proč to používat?
Jak to vypadá?
Jak to rozchodit?
Co to je?
Co to je?

CoffeeScript is a little language that compiles
into JavaScript.
Co to je?

CoffeeScript is a little language that compiles
into JavaScript.




The code compiles one-to-one into the
equivalent JS, and there is no interpretation
at runtime.
JavaScript je v jádru docela dobrý jazyk, ale…
JavaScript je v jádru docela dobrý jazyk, ale…


“   JavaScript had to look like Java only less so, be
    Java’s dumb kid brother or boy-hostage sidekick.
JavaScript je v jádru docela dobrý jazyk, ale…


“   JavaScript had to look like Java only less so, be
    Java’s dumb kid brother or boy-hostage sidekick.

    Plus, I had to be done in ten days or something
    worse than JavaScript would have happened.
                                                     ”
                                      — Brendan Eich
Proč to používat?
Proč to používat?


Rychlejší vývoj
Proč to používat?


Rychlejší vývoj

Méně bugů
Proč to používat?


Rychlejší vývoj

Méně bugů

Lepší čitelnost
Jak to vypadá?




JavaScript     CoffeeScript
Functions


var cube, square;
square = function(x) {
   return x * x;           square = (x) -> x * x
};
cube = function(x) {       cube = (x) -> square(x) * x
   return square(x) * x;
};




      JavaScript                CoffeeScript
Objects

var kids = {            kids =
   brother: {             brother:
     name: "Max",           name: "Max"
     age: 11                age: 11
   },                     sister:
   sister: {                name: "Ida"
     name: "Ida",           age: 9
     age: 9
   }
};



      JavaScript              CoffeeScript
If, Else, Conditional Assignment


var date, mood;
if (singing) {                mood = greatlyImproved if singing
  mood = greatlyImproved;
}
if (happy && knowsIt) {       if happy and knowsIt
  clapsHands();                 clapsHands()
  chaChaCha();                  chaChaCha()
} else {                      else
  showIt();                     showIt()
}
date = friday ? sue : jill;   date = if friday then sue else jill



        JavaScript                    CoffeeScript
OOP
var Animal, Horse, Snake, sam, tom;
var __hasProp = Object.prototype.hasOwnProperty, __extends =
                                                                     class Animal
function(child, parent) {                                              constructor: (@name) ->
  for (var key in parent) {
 if (__hasProp.call(parent, key)) child[key] = parent[key]; }
  function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
                                                                       move: (meters) ->
child.prototype = new ctor; child.__super__ = parent.prototype;          alert @name +" moved "+ meters +"m."
return child;};Animal = (function() {
  function Animal(name) { this.name = name; }
  Animal.prototype.move = function(meters) {                         class Snake extends Animal
     return alert(this.name + " moved " + meters + "m.");
  };                                                                   move: ->
return Animal;})();
Snake = (function() { __extends(Snake, Animal); function
                                                                         alert "Slithering..."
Snake() {     Snake.__super__.constructor.apply(this, arguments);        super 5
} Snake.prototype.move = function() {      alert("Slithering...");
return Snake.__super__.move.call(this, 5); }; return
Snake;})();                                                          class Horse extends Animal
Horse = (function() { __extends(Horse, Animal); function
Horse() {                                                              move: ->
  }
    Horse.__super__.constructor.apply(this, arguments);                  alert "Galloping..."
Horse.prototype.move = function() {                                      super 45
    alert("Galloping...");    return
Horse.__super__.move.call(this, 45);
}; return Horse;})();                                                sam = new Snake "Sammy the Python"
sam = new Snake("Sammy the Python");
tom = new Horse("Tommy the Palomino");                               tom = new Horse "Tommy the Palomino"




                  JavaScript                                                    CoffeeScript
Na co jste zvyklí z Pythonu
Loops

# Eat lunch.
for food in ['toast', 'cheese', 'wine']:
  eat(food)



# Eat lunch.
eat food for food in ['toast', 'cheese', 'wine']
Loops

# Eat lunch.
for food in ['toast', 'cheese', 'wine']:
  eat(food)



# Eat lunch.
eat food for food in ['toast', 'cheese', 'wine']


var food, _i, _len, _ref;
_ref = ['toast', 'cheese', 'wine'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  food = _ref[_i];
  eat(food);
}
Loops

for key in {‘foo’: ‘bar’}

for key, value in {‘foo’: ‘bar’}.items()




for key of {foo: ‘bar’}

for key, value of {foo: ‘bar’}
Ranges

for i in range(1, 10)

for i in range(10, 1, -1)

for i in range(1, 10, 2)


for i in [1..9] # nebo [1...10]

for i in [10..2]

for i in [1..9] by 2
Comprehensions


short_names = [name for name in list if len(name) < 5]




shortNames = (name for name in list when name.length < 5)
Slicing

my_list[3:6]

my_list[3:]

my_list[:-3]


myList[3..5]

myList[3..]

myList[...-3]
Slicing

my_list[3:6] = [1, 2, 3]

my_list[3:]

my_list[:-3]


myList[3..5] = [1, 2, 3]

myList[3..]

myList[...-3]
Splats
(a.k.a. argument list unpacking)




def foo(bar, *args): pass




foo = (bar, args...) ->
Chained Comparisons



 cholesterol = 127

 healthy = 200 > cholesterol > 60
more...
The Existential Operator


           alert "I knew it!" if elvis?




if (typeof elvis !== "undefined" && elvis !== null) {
  alert("I knew it!");
}
Destructuring Assignment

theBait = 1000
theSwitch = 0

[theBait, theSwitch] = [theSwitch, theBait]



weatherReport = (location) ->
  # Make an Ajax request to fetch the weather...
  [location, 72, "Mostly Sunny"]

[city, temp, forecast] = weatherReport "Berkeley, CA"
Fat arrow – binding this


Account = (customer, cart) ->
  @customer = customer
  @cart = cart

  $('.shopping_cart').bind 'click', (event) =>
    @customer.purchase @cart
String Interpolation


author = "Wittgenstein"
quote = "A picture is a fact. -- #{author}"

sentence = "#{ 22 / 7 } is a decent approximation of π"
Jak to rozchodit?
The CoffeeScript compiler is
itself written in CoffeeScript.
nodejs.org
Node Package Manager
$ curl http://npmjs.org/install.sh | sh

$ npm install -g coffee-script
$ coffee --watch --compile *.coffee


19:45:31 - compiled   myscript.coffee
19:47:22 - compiled   myscript.coffee
In myscript.coffee,   too many ) on line 39
19:47:40 - compiled   myscript.coffee
19:48:10 - compiled   myscript.coffee
19:48:47 - compiled   myscript.coffee
In myscript.coffee,   Parse error on line 3: Unexpected 'INDENT'
19:49:23 - compiled   myscript.coffee
<script src="/path/to/coffee-script.js"></script>
<script type="text/coffeescript">
  $(document).ready -> alert "Your DOM is ready."
</script>
Odkazy
CoffeeScript homepage
http://jashkenas.github.com/coffee-script/


node.js
http://nodejs.org/

npm
http://npmjs.org/

Introduction to CoffeeScript
http://screencasts.org/episodes/introduction-to-coffeescript/

Js2Coffee – The JavaScript to CoffeeScript compiler.
http://ricostacruz.com/js2coffee/

Contenu connexe

Tendances

Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentationadamcookeuk
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescriptDavid Furber
 
Why (I think) CoffeeScript Is Awesome
Why (I think) CoffeeScript Is AwesomeWhy (I think) CoffeeScript Is Awesome
Why (I think) CoffeeScript Is AwesomeJo Cranford
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Rolessartak
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java DevelopersMichael Galpin
 
P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)lichtkind
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners PerlDave Cross
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Wen-Tien Chang
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScriptTodd Anglin
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkDaniel Spector
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...Fwdays
 
Creating native apps with WordPress
Creating native apps with WordPressCreating native apps with WordPress
Creating native apps with WordPressMarko Heijnen
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016Manoj Kumar
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift OverviewKaz Yoshikawa
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesDragos Ionita
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2Dave Cross
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overviewhesher
 

Tendances (20)

Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
Why (I think) CoffeeScript Is Awesome
Why (I think) CoffeeScript Is AwesomeWhy (I think) CoffeeScript Is Awesome
Why (I think) CoffeeScript Is Awesome
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Roles
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java Developers
 
P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
 
Creating native apps with WordPress
Creating native apps with WordPressCreating native apps with WordPress
Creating native apps with WordPress
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift Overview
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
 
Ruby on Rails for beginners
Ruby on Rails for beginnersRuby on Rails for beginners
Ruby on Rails for beginners
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overview
 

En vedette

CoffeeScript in 5mins
CoffeeScript in 5minsCoffeeScript in 5mins
CoffeeScript in 5minsMasakuni Kato
 
Coffee script grunt
Coffee script gruntCoffee script grunt
Coffee script gruntKien Pham
 
Ruby Made Simple: Blocks Plus Iterators
Ruby Made Simple: Blocks Plus IteratorsRuby Made Simple: Blocks Plus Iterators
Ruby Made Simple: Blocks Plus IteratorsJohn Schmidt
 
Coffeescript: An Opinionated Introduction
Coffeescript: An Opinionated IntroductionCoffeescript: An Opinionated Introduction
Coffeescript: An Opinionated IntroductionJoe Fleming
 
CoffeeScript: A beginner's presentation for beginners copy
CoffeeScript: A beginner's presentation for beginners copyCoffeeScript: A beginner's presentation for beginners copy
CoffeeScript: A beginner's presentation for beginners copyPatrick Devins
 

En vedette (6)

CoffeeScript in 5mins
CoffeeScript in 5minsCoffeeScript in 5mins
CoffeeScript in 5mins
 
Coffee script grunt
Coffee script gruntCoffee script grunt
Coffee script grunt
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Ruby Made Simple: Blocks Plus Iterators
Ruby Made Simple: Blocks Plus IteratorsRuby Made Simple: Blocks Plus Iterators
Ruby Made Simple: Blocks Plus Iterators
 
Coffeescript: An Opinionated Introduction
Coffeescript: An Opinionated IntroductionCoffeescript: An Opinionated Introduction
Coffeescript: An Opinionated Introduction
 
CoffeeScript: A beginner's presentation for beginners copy
CoffeeScript: A beginner's presentation for beginners copyCoffeeScript: A beginner's presentation for beginners copy
CoffeeScript: A beginner's presentation for beginners copy
 

Similaire à CoffeeScript

Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScriptniklal
 
Internal Project: Under the Hood
Internal Project: Under the HoodInternal Project: Under the Hood
Internal Project: Under the HoodVladik Khononov
 
Coffee Scriptでenchant.js
Coffee Scriptでenchant.jsCoffee Scriptでenchant.js
Coffee Scriptでenchant.jsNaoyuki Totani
 
CoffeeScript-Ruby-Tuesday
CoffeeScript-Ruby-TuesdayCoffeeScript-Ruby-Tuesday
CoffeeScript-Ruby-TuesdayEddie Kao
 
FITC CoffeeScript 101
FITC CoffeeScript 101FITC CoffeeScript 101
FITC CoffeeScript 101Faisal Abid
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
OO JS for AS3 Devs
OO JS for AS3 DevsOO JS for AS3 Devs
OO JS for AS3 DevsJason Hanson
 
Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101Roy Yu
 
Coffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotesCoffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotesBrandon Satrom
 
CoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayCoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayLim Chanmann
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlordsheumann
 
CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better! CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better! Jack Franklin
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 
Reasons To Love Ruby
Reasons To Love RubyReasons To Love Ruby
Reasons To Love RubyBen Scheirman
 

Similaire à CoffeeScript (20)

Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScript
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Internal Project: Under the Hood
Internal Project: Under the HoodInternal Project: Under the Hood
Internal Project: Under the Hood
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Coffee Scriptでenchant.js
Coffee Scriptでenchant.jsCoffee Scriptでenchant.js
Coffee Scriptでenchant.js
 
CoffeeScript-Ruby-Tuesday
CoffeeScript-Ruby-TuesdayCoffeeScript-Ruby-Tuesday
CoffeeScript-Ruby-Tuesday
 
FITC CoffeeScript 101
FITC CoffeeScript 101FITC CoffeeScript 101
FITC CoffeeScript 101
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
OO JS for AS3 Devs
OO JS for AS3 DevsOO JS for AS3 Devs
OO JS for AS3 Devs
 
Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101
 
Coffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotesCoffee scriptisforclosers nonotes
Coffee scriptisforclosers nonotes
 
CoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayCoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple way
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
 
JavaScript @ CTK
JavaScript @ CTKJavaScript @ CTK
JavaScript @ CTK
 
CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better! CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better!
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Quick coffeescript
Quick coffeescriptQuick coffeescript
Quick coffeescript
 
Einführung in TypeScript
Einführung in TypeScriptEinführung in TypeScript
Einführung in TypeScript
 
Reasons To Love Ruby
Reasons To Love RubyReasons To Love Ruby
Reasons To Love Ruby
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 

Dernier

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
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 DevelopmentsTrustArc
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
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...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 

Dernier (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
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...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

CoffeeScript

  • 1. CoffeeScript Petr Pokorný
  • 2. Obsah Co to je? Proč to používat? Jak to vypadá? Jak to rozchodit?
  • 4. Co to je? CoffeeScript is a little language that compiles into JavaScript.
  • 5. Co to je? CoffeeScript is a little language that compiles into JavaScript. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime.
  • 6. JavaScript je v jádru docela dobrý jazyk, ale…
  • 7. JavaScript je v jádru docela dobrý jazyk, ale… “ JavaScript had to look like Java only less so, be Java’s dumb kid brother or boy-hostage sidekick.
  • 8. JavaScript je v jádru docela dobrý jazyk, ale… “ JavaScript had to look like Java only less so, be Java’s dumb kid brother or boy-hostage sidekick. Plus, I had to be done in ten days or something worse than JavaScript would have happened. ” — Brendan Eich
  • 11. Proč to používat? Rychlejší vývoj Méně bugů
  • 12. Proč to používat? Rychlejší vývoj Méně bugů Lepší čitelnost
  • 14. Functions var cube, square; square = function(x) { return x * x; square = (x) -> x * x }; cube = function(x) { cube = (x) -> square(x) * x return square(x) * x; }; JavaScript CoffeeScript
  • 15. Objects var kids = { kids = brother: { brother: name: "Max", name: "Max" age: 11 age: 11 }, sister: sister: { name: "Ida" name: "Ida", age: 9 age: 9 } }; JavaScript CoffeeScript
  • 16. If, Else, Conditional Assignment var date, mood; if (singing) { mood = greatlyImproved if singing mood = greatlyImproved; } if (happy && knowsIt) { if happy and knowsIt clapsHands(); clapsHands() chaChaCha(); chaChaCha() } else { else showIt(); showIt() } date = friday ? sue : jill; date = if friday then sue else jill JavaScript CoffeeScript
  • 17. OOP var Animal, Horse, Snake, sam, tom; var __hasProp = Object.prototype.hasOwnProperty, __extends = class Animal function(child, parent) { constructor: (@name) -> for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; move: (meters) -> child.prototype = new ctor; child.__super__ = parent.prototype; alert @name +" moved "+ meters +"m." return child;};Animal = (function() { function Animal(name) { this.name = name; } Animal.prototype.move = function(meters) { class Snake extends Animal return alert(this.name + " moved " + meters + "m."); }; move: -> return Animal;})(); Snake = (function() { __extends(Snake, Animal); function alert "Slithering..." Snake() { Snake.__super__.constructor.apply(this, arguments); super 5 } Snake.prototype.move = function() { alert("Slithering..."); return Snake.__super__.move.call(this, 5); }; return Snake;})(); class Horse extends Animal Horse = (function() { __extends(Horse, Animal); function Horse() { move: -> } Horse.__super__.constructor.apply(this, arguments); alert "Galloping..." Horse.prototype.move = function() { super 45 alert("Galloping..."); return Horse.__super__.move.call(this, 45); }; return Horse;})(); sam = new Snake "Sammy the Python" sam = new Snake("Sammy the Python"); tom = new Horse("Tommy the Palomino"); tom = new Horse "Tommy the Palomino" JavaScript CoffeeScript
  • 18. Na co jste zvyklí z Pythonu
  • 19. Loops # Eat lunch. for food in ['toast', 'cheese', 'wine']: eat(food) # Eat lunch. eat food for food in ['toast', 'cheese', 'wine']
  • 20. Loops # Eat lunch. for food in ['toast', 'cheese', 'wine']: eat(food) # Eat lunch. eat food for food in ['toast', 'cheese', 'wine'] var food, _i, _len, _ref; _ref = ['toast', 'cheese', 'wine']; for (_i = 0, _len = _ref.length; _i < _len; _i++) { food = _ref[_i]; eat(food); }
  • 21. Loops for key in {‘foo’: ‘bar’} for key, value in {‘foo’: ‘bar’}.items() for key of {foo: ‘bar’} for key, value of {foo: ‘bar’}
  • 22. Ranges for i in range(1, 10) for i in range(10, 1, -1) for i in range(1, 10, 2) for i in [1..9] # nebo [1...10] for i in [10..2] for i in [1..9] by 2
  • 23. Comprehensions short_names = [name for name in list if len(name) < 5] shortNames = (name for name in list when name.length < 5)
  • 25. Slicing my_list[3:6] = [1, 2, 3] my_list[3:] my_list[:-3] myList[3..5] = [1, 2, 3] myList[3..] myList[...-3]
  • 26. Splats (a.k.a. argument list unpacking) def foo(bar, *args): pass foo = (bar, args...) ->
  • 27. Chained Comparisons cholesterol = 127 healthy = 200 > cholesterol > 60
  • 29. The Existential Operator alert "I knew it!" if elvis? if (typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }
  • 30. Destructuring Assignment theBait = 1000 theSwitch = 0 [theBait, theSwitch] = [theSwitch, theBait] weatherReport = (location) -> # Make an Ajax request to fetch the weather... [location, 72, "Mostly Sunny"] [city, temp, forecast] = weatherReport "Berkeley, CA"
  • 31. Fat arrow – binding this Account = (customer, cart) -> @customer = customer @cart = cart $('.shopping_cart').bind 'click', (event) => @customer.purchase @cart
  • 32. String Interpolation author = "Wittgenstein" quote = "A picture is a fact. -- #{author}" sentence = "#{ 22 / 7 } is a decent approximation of π"
  • 34. The CoffeeScript compiler is itself written in CoffeeScript.
  • 36. Node Package Manager $ curl http://npmjs.org/install.sh | sh $ npm install -g coffee-script
  • 37. $ coffee --watch --compile *.coffee 19:45:31 - compiled myscript.coffee 19:47:22 - compiled myscript.coffee In myscript.coffee, too many ) on line 39 19:47:40 - compiled myscript.coffee 19:48:10 - compiled myscript.coffee 19:48:47 - compiled myscript.coffee In myscript.coffee, Parse error on line 3: Unexpected 'INDENT' 19:49:23 - compiled myscript.coffee
  • 38. <script src="/path/to/coffee-script.js"></script> <script type="text/coffeescript"> $(document).ready -> alert "Your DOM is ready." </script>
  • 39. Odkazy CoffeeScript homepage http://jashkenas.github.com/coffee-script/ node.js http://nodejs.org/ npm http://npmjs.org/ Introduction to CoffeeScript http://screencasts.org/episodes/introduction-to-coffeescript/ Js2Coffee – The JavaScript to CoffeeScript compiler. http://ricostacruz.com/js2coffee/