SlideShare a Scribd company logo
Intro to CoffeeScript
A beginner’s presentation for beginners
Don’t I have to be a JSWiz?
Not even close. (But it does help)
Developer/Designer/Unicorn
Little Experience required
Simple syntax
Better transition from other language patterns
Some cool points
- Classes
- No semicolons (YAY!)
- Commas optional
- Arrow function notation
What is CoffeeScript?
CoffeeScript is an alternate syntax that compiles into Javascript. According to
the author, it is an ‘attempt to expose the good parts of JavaScript in a
simple way.
Popularity
CoffeeScript has gained much popularity since it’s inception as a GitHub gist in
2009. It has become the ‘x’-script of choice for some projects you might
have heard of, including Dropbox and GitHub, as well as many other
projects you’ve never heard of.
Usage
The widespread usage across many web apps makes this language a ‘should
do’ for any designer or developer. Even if you don’t decide to use it, you will
most likely come across it in a future project. So, DTRT and learn a thing-or-
two.
# Assignments
num = -5
isPositive = true
# Objects
Setup = ->
num: num
add: (i) ->
num + i
# Conditionals
num = 5 if isPositive
# Arrays
arr = [1,2,3,4,5]
var num, isPositive, Setup, arr,
plusOne;
num = -5;
isPositive = true;
Setup = function() {
num: num,
add: function(i) {
return num + i
}
};
if(isPositive) {
num = 5;
}
arr = [1,2,3,4,5];
So, what does it look like?
# Assignments
num = -5
isPositive = true
# Objects
Setup = ->
num: num
add: (i) ->
num + i
# Conditionals
num = 5 if isPositive
# Arrays
arr = [1,2,3,4,5]
- # -- for Comments
and
- ###
words -- for Block Comments
###
- Semicolons not required! (W00t!!
11!)
- Commas are not required, but can
be helpful.
- Indentation/syntax depended.
(Similar in nature to Ruby or Python)
- Concise syntax.
- Parentheses and Curlies are not
required, but can be helpful.
- Fun to type!
- Easier to read!
Example examined.
Installation
CoffeeScript requires Node.js and the NPM Package Manager or Ruby, which are
installed thusly:
Node.js --
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
-- OR --
Mac OS X : brew install node
Ubuntu? : apt-get install node
Everything : http://nodejs.org/download/ for Installer ackages
NPM --
curl https://npmjs.org/install.sh | sh
Ruby --
Mac OS X : comes preinstalled
Linux : may come preinstalled
Windows : http://rubyinstaller.org/
Installation, Continued
Now we can actually install CoffeeScript! YAY!!
Global :
$ sudo npm install -g coffee-script
Project :
$ npm install coffee-script
Alternatively, through GitHub (I’ve never done this one)
$ npm install -g http://github.com/jashkenas/coffee-script/tarball/master
and then, go to the coffee-script directory and type:
$ sudo bin/cake install
Now what?
Let’s play around with some ideas.
JavaScript, as of EC5, doesn’t have defined class structures and is mostly a
functional, object-oriented language. “But,” I can hear some protest, “you
can use things like Traceur and the EC6 Module Transpiler to write
JavaScript with classes and other new constructs available in Ecmascript 6.”
Well. If you’re using those things, awesome. Even you can benefit from CS!
Classes
class Car
constructor: (@name) ->
engine: (valves) ->
console.log(@name + “ is a V-#{valves}.”
class Ford extends Car
engine: ->
super 6
class Honda extends Car
engine: ->
super 4
slowCar = new Honda “Prius”
fastCar = new Ford “Cobra”
slowCar.engine()
And? In JavaScript?
var Car, Ford, Honda, fastCar, slowCar, _ref, _ref1,
__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; };
Car = (function() {
function Car(name) {
this.name = name;
({
engine: function(valves) {
return console.log(this.name + ' is a V#{valves}.');
}
});
}
return Car;
})();
(etc...)
[
http://coffeescript.org/#try:class%20Car%0A%20%20constructor%3A%20(%40name)%20-%3E%0A%20%20%20%20engine%3
]
So that's cool...
What other neat things can be done this
easily?
JSON:
teachers =
teacher:
name: "Patrick D."
company: "Rightleaf, LLC"
teacher:
name: "Phil P."
company: "Rightleaf, LLC"
Ranges:
days = [1..7]
Existential Operator:
login() if not user?
This and This!
Splats:
giveShips = (one, two, three, rest...) ->
addOnesToShip one, 10
addOnesToShip two, 6
addOnesToShip three, 2
for ship in rest
addOnesToShip ship, 1
ships = ['rat', 'dog', 'cat', 'bull', 'horse']
giveShips ships...
(need ... to ensure assignment)
One last
"Pretty Cool Thing"
"Pretty Cool Thing"
Modules!!!!
module.exports = (engine, options = {}) ->
options.blacklist ?= []
engine = require(engine) if typeof engine is 'string'
compile: (template, data) ->
template = engine.compile(template, data)
(data) ->
for k, v of data.params
if typeof data[k] is 'undefined' and k not in options.blacklist
data[k] = v
template(data)
(sample taken and modified from
https://github.com/mauricemach/zappa/blob/master/src/zappa.coffee for effect)
Arg(h)s!!
(The good ones)
-o Output Directory
-c Compile Directory
-w Watch this Directory
-m Map JS to CS files for Debugging
-b Remove safety wrapper (a good thing IMHO)
It'll look something like this:
v--Command v---to here v--- In all these CS files
coffee -mj -o /javascripts/allYourJase.js -cw /coffeescripts/*.coffee
^^--^--Map, Join and Output ^---Compile and Watch for saves
Arguments for 'coffee', or
Compiling like a pirate
Resources
CoffeeScript Home Page:
http://coffeescript.org
Using CoffeeScript with Node and such:
http://ristrettolo.gy (online book)
Examples:
http://en.wikipedia.org/wiki/CoffeeScript
http://amix.dk/blog/post/19612 (neet post about CoffeeScript stuff, in general)
Cool Tuts:
http://blog.teamtreehouse.com/the-absolute-beginners-guide-to-coffeescript
This guy hates it, just in case you want the other point of view.
CoffeeScript: less typing, bad readability…
http://ceronman.com/2012/09/17/coffeescript-less-typing-bad-readability/

More Related Content

PDF
Coffeescript: An Opinionated Introduction
PDF
CoffeeScript in 5mins
KEY
End to-End CoffeeScript
PDF
Happy Programming with CoffeeScript
PDF
Boxen: How to Manage an Army of Laptops and Live to Talk About It
PDF
CoffeeScript
PDF
JavaScript OOP Pattern
PDF
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Coffeescript: An Opinionated Introduction
CoffeeScript in 5mins
End to-End CoffeeScript
Happy Programming with CoffeeScript
Boxen: How to Manage an Army of Laptops and Live to Talk About It
CoffeeScript
JavaScript OOP Pattern
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...

What's hot (20)

PDF
Go Mobile with Apache Cordova, Zagreb 2014
PDF
World of Logging
PDF
Selenium sandwich-3: Being where you aren't.
PDF
Perl Dancer for Python programmers
PDF
A Gentle Introduction to Event Loops
PDF
Plumbin Pipelines - A Gulp.js workshop
PDF
Asynchronous programming patterns in Perl
PDF
Automating Front-End Workflow
PDF
Angular2 ecosystem
PDF
Lies, Damn Lies, and Benchmarks
PDF
Advanced JavaScript build pipelines using Gulp.js
PDF
PerlDancer for Perlers (FOSDEM 2011)
PDF
Chat bot-automation-hubot
PDF
CasperJs Enjoy Functional Testing
PDF
Ops for everyone - John Britton
ODP
Perl Dancer, FPW 2010
PPTX
Reasons To Love Ruby
PDF
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
PPTX
ECMAScript 6 and the Node Driver
PDF
Testing MeteorJS using CasperJS
Go Mobile with Apache Cordova, Zagreb 2014
World of Logging
Selenium sandwich-3: Being where you aren't.
Perl Dancer for Python programmers
A Gentle Introduction to Event Loops
Plumbin Pipelines - A Gulp.js workshop
Asynchronous programming patterns in Perl
Automating Front-End Workflow
Angular2 ecosystem
Lies, Damn Lies, and Benchmarks
Advanced JavaScript build pipelines using Gulp.js
PerlDancer for Perlers (FOSDEM 2011)
Chat bot-automation-hubot
CasperJs Enjoy Functional Testing
Ops for everyone - John Britton
Perl Dancer, FPW 2010
Reasons To Love Ruby
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
ECMAScript 6 and the Node Driver
Testing MeteorJS using CasperJS
Ad

Viewers also liked (6)

PDF
Introduction to CoffeeScript
PDF
Coffee script grunt
PDF
CoffeeScript
PDF
CoffeeScript
ODP
Ruby Made Simple: Blocks Plus Iterators
PDF
Coffee Script
Introduction to CoffeeScript
Coffee script grunt
CoffeeScript
CoffeeScript
Ruby Made Simple: Blocks Plus Iterators
Coffee Script
Ad

Similar to CoffeeScript: A beginner's presentation for beginners copy (20)

PDF
Damn Fine CoffeeScript
PDF
CoffeeScript Design Patterns
PDF
CoffeeScript, An Introduction for Nodejs developers
PDF
CoffeeScript-Ruby-Tuesday
PDF
CoffeeScript: The Good Parts
PDF
CoffeeScript
PPTX
Overview of CoffeeScript
PDF
Building Fast, Modern Web Applications with Node.js and CoffeeScript
PPTX
CoffeeScript - An Introduction
PPTX
Coffee script
PDF
Coffeescript
PPTX
Coffee scriptisforclosers nonotes
PDF
SNP STEAM Academy 2017 Class #12
PDF
Seattle.rb 6.4
PDF
CoffeeScript - TechTalk 21/10/2013
KEY
Quick coffeescript
PDF
Coffee script
PPT
Node js
PDF
Javascript status 2016
PDF
Advanced Java Script.pdf
Damn Fine CoffeeScript
CoffeeScript Design Patterns
CoffeeScript, An Introduction for Nodejs developers
CoffeeScript-Ruby-Tuesday
CoffeeScript: The Good Parts
CoffeeScript
Overview of CoffeeScript
Building Fast, Modern Web Applications with Node.js and CoffeeScript
CoffeeScript - An Introduction
Coffee script
Coffeescript
Coffee scriptisforclosers nonotes
SNP STEAM Academy 2017 Class #12
Seattle.rb 6.4
CoffeeScript - TechTalk 21/10/2013
Quick coffeescript
Coffee script
Node js
Javascript status 2016
Advanced Java Script.pdf

Recently uploaded (20)

PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
master seminar digital applications in india
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Sports Quiz easy sports quiz sports quiz
PDF
RMMM.pdf make it easy to upload and study
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Institutional Correction lecture only . . .
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
TR - Agricultural Crops Production NC III.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
VCE English Exam - Section C Student Revision Booklet
Microbial diseases, their pathogenesis and prophylaxis
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Renaissance Architecture: A Journey from Faith to Humanism
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
master seminar digital applications in india
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Sports Quiz easy sports quiz sports quiz
RMMM.pdf make it easy to upload and study
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Structure & Organelles in detailed.
Microbial disease of the cardiovascular and lymphatic systems
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
PPH.pptx obstetrics and gynecology in nursing

CoffeeScript: A beginner's presentation for beginners copy

  • 1. Intro to CoffeeScript A beginner’s presentation for beginners
  • 2. Don’t I have to be a JSWiz? Not even close. (But it does help) Developer/Designer/Unicorn Little Experience required Simple syntax Better transition from other language patterns Some cool points - Classes - No semicolons (YAY!) - Commas optional - Arrow function notation
  • 3. What is CoffeeScript? CoffeeScript is an alternate syntax that compiles into Javascript. According to the author, it is an ‘attempt to expose the good parts of JavaScript in a simple way. Popularity CoffeeScript has gained much popularity since it’s inception as a GitHub gist in 2009. It has become the ‘x’-script of choice for some projects you might have heard of, including Dropbox and GitHub, as well as many other projects you’ve never heard of. Usage The widespread usage across many web apps makes this language a ‘should do’ for any designer or developer. Even if you don’t decide to use it, you will most likely come across it in a future project. So, DTRT and learn a thing-or- two.
  • 4. # Assignments num = -5 isPositive = true # Objects Setup = -> num: num add: (i) -> num + i # Conditionals num = 5 if isPositive # Arrays arr = [1,2,3,4,5] var num, isPositive, Setup, arr, plusOne; num = -5; isPositive = true; Setup = function() { num: num, add: function(i) { return num + i } }; if(isPositive) { num = 5; } arr = [1,2,3,4,5]; So, what does it look like?
  • 5. # Assignments num = -5 isPositive = true # Objects Setup = -> num: num add: (i) -> num + i # Conditionals num = 5 if isPositive # Arrays arr = [1,2,3,4,5] - # -- for Comments and - ### words -- for Block Comments ### - Semicolons not required! (W00t!! 11!) - Commas are not required, but can be helpful. - Indentation/syntax depended. (Similar in nature to Ruby or Python) - Concise syntax. - Parentheses and Curlies are not required, but can be helpful. - Fun to type! - Easier to read! Example examined.
  • 6. Installation CoffeeScript requires Node.js and the NPM Package Manager or Ruby, which are installed thusly: Node.js -- https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager -- OR -- Mac OS X : brew install node Ubuntu? : apt-get install node Everything : http://nodejs.org/download/ for Installer ackages NPM -- curl https://npmjs.org/install.sh | sh Ruby -- Mac OS X : comes preinstalled Linux : may come preinstalled Windows : http://rubyinstaller.org/
  • 7. Installation, Continued Now we can actually install CoffeeScript! YAY!! Global : $ sudo npm install -g coffee-script Project : $ npm install coffee-script Alternatively, through GitHub (I’ve never done this one) $ npm install -g http://github.com/jashkenas/coffee-script/tarball/master and then, go to the coffee-script directory and type: $ sudo bin/cake install
  • 8. Now what? Let’s play around with some ideas. JavaScript, as of EC5, doesn’t have defined class structures and is mostly a functional, object-oriented language. “But,” I can hear some protest, “you can use things like Traceur and the EC6 Module Transpiler to write JavaScript with classes and other new constructs available in Ecmascript 6.” Well. If you’re using those things, awesome. Even you can benefit from CS!
  • 9. Classes class Car constructor: (@name) -> engine: (valves) -> console.log(@name + “ is a V-#{valves}.” class Ford extends Car engine: -> super 6 class Honda extends Car engine: -> super 4 slowCar = new Honda “Prius” fastCar = new Ford “Cobra” slowCar.engine()
  • 10. And? In JavaScript? var Car, Ford, Honda, fastCar, slowCar, _ref, _ref1, __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; }; Car = (function() { function Car(name) { this.name = name; ({ engine: function(valves) { return console.log(this.name + ' is a V#{valves}.'); } }); } return Car; })(); (etc...) [ http://coffeescript.org/#try:class%20Car%0A%20%20constructor%3A%20(%40name)%20-%3E%0A%20%20%20%20engine%3 ]
  • 11. So that's cool... What other neat things can be done this easily?
  • 12. JSON: teachers = teacher: name: "Patrick D." company: "Rightleaf, LLC" teacher: name: "Phil P." company: "Rightleaf, LLC" Ranges: days = [1..7] Existential Operator: login() if not user? This and This! Splats: giveShips = (one, two, three, rest...) -> addOnesToShip one, 10 addOnesToShip two, 6 addOnesToShip three, 2 for ship in rest addOnesToShip ship, 1 ships = ['rat', 'dog', 'cat', 'bull', 'horse'] giveShips ships... (need ... to ensure assignment)
  • 13. One last "Pretty Cool Thing" "Pretty Cool Thing" Modules!!!! module.exports = (engine, options = {}) -> options.blacklist ?= [] engine = require(engine) if typeof engine is 'string' compile: (template, data) -> template = engine.compile(template, data) (data) -> for k, v of data.params if typeof data[k] is 'undefined' and k not in options.blacklist data[k] = v template(data) (sample taken and modified from https://github.com/mauricemach/zappa/blob/master/src/zappa.coffee for effect)
  • 14. Arg(h)s!! (The good ones) -o Output Directory -c Compile Directory -w Watch this Directory -m Map JS to CS files for Debugging -b Remove safety wrapper (a good thing IMHO) It'll look something like this: v--Command v---to here v--- In all these CS files coffee -mj -o /javascripts/allYourJase.js -cw /coffeescripts/*.coffee ^^--^--Map, Join and Output ^---Compile and Watch for saves Arguments for 'coffee', or Compiling like a pirate
  • 15. Resources CoffeeScript Home Page: http://coffeescript.org Using CoffeeScript with Node and such: http://ristrettolo.gy (online book) Examples: http://en.wikipedia.org/wiki/CoffeeScript http://amix.dk/blog/post/19612 (neet post about CoffeeScript stuff, in general) Cool Tuts: http://blog.teamtreehouse.com/the-absolute-beginners-guide-to-coffeescript This guy hates it, just in case you want the other point of view. CoffeeScript: less typing, bad readability… http://ceronman.com/2012/09/17/coffeescript-less-typing-bad-readability/