CoffeeScript: JavaScript onSteroids
By Jordan Dichev
CoffeeScriptis

• “A littlelanguangethat compiles into
JavaScript”
• Easiertoreadandwritethan JavaScript
• Syntacticallycl...
Main Features
• Significant whitespace and highly expressive
• Bound functional literal and class sugar for prototypes
• L...
Functions

square = (x) ->x * x
(input arguments) -> output definition

var square = function(x) {
return x * x;
}
Literals
guys = ['barry', 'larry', 'harry']
console.log guys[0]
elements =
hydrogen = 1

silicon = 14
uranium = 92
console...
Literals
var elements, guys;

guys = ['barry', 'larry', 'harry'];

console.log(guys[0]);

elements = {
hydrogen: 1,
silico...
Whitespace
today = 'tuesday'

if today is 'tuesday'
console.log 'UXEG'
else

console.log 'noop'
Whitespace
var today;

today = 'tuesday';
if (today === 'tuesday') {

console.log('UXEG');
} else {
console.log('noop');

...
Loops and comprehensions
list = ['a', 'b', 'c']
console.log letter for letter in list
process =
good: 'good'

bad: 'bad'
c...
List comprehensions
vari, letter, list, name, process, _i, _j, _len;
list = ['a', 'b', 'c'];
for (_i = 0, _len = list.leng...
Existential operator
person =
name: 'John Doe'

age: 28
console.logperson.name.length

console.logperson.non?.existent.pro...
Existential operator
varperson, _base, _ref;
person = {

name: 'John Doe',
age: 28
};

console.log(person.name.length);
co...
Classes
class Message
show: ->

console.log 'Foo'
class BarMessage extends Message

show: ->
console.log 'Bar’

new BarMes...
Conditionals
if a
console.log "a is true"

else
console.log "a is false”

console.log "a is false" unless a
Scope
a = 'A'
myFunc = ->

a = 'AAA'
b = 'B'
Scope
(function() {
var a, myFunc;

a = 'A’;
myFunc = function() {
varb;

a = 'AAA';
return b = 'B';
};

}).call(this);
String Interpolation
name = "John Doe"

console.log "Hello #{name}"
console.log 'Hello #{name}'
String Interpolation
var name;

name = "John Doe";
console.log("Hello " + name);
console.log('Hello #{name}');
Building
coffee – a command line tool
cake – a simple build tool
coffee -w -o lib/ -jmergedSource.js-csrc/
-w – to watch f...
Upcoming SlideShare
Loading in...5
×

Coffee script

281

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
281
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Coffee script

  1. 1. CoffeeScript: JavaScript onSteroids By Jordan Dichev
  2. 2. CoffeeScriptis • “A littlelanguangethat compiles into JavaScript” • Easiertoreadandwritethan JavaScript • SyntacticallyclosetoRubyand Python • Easytointegratewithexistingcode • Just JavaScript afterall
  3. 3. Main Features • Significant whitespace and highly expressive • Bound functional literal and class sugar for prototypes • Loop, comprehensions and both • An existential operator • Splats (variadic parameters) • Default Parameters • Heredocs, heregexes • Automatic lexical scoping
  4. 4. Functions square = (x) ->x * x (input arguments) -> output definition var square = function(x) { return x * x; }
  5. 5. Literals guys = ['barry', 'larry', 'harry'] console.log guys[0] elements = hydrogen = 1 silicon = 14 uranium = 92 console.logelements.silicon
  6. 6. Literals var elements, guys; guys = ['barry', 'larry', 'harry']; console.log(guys[0]); elements = { hydrogen: 1, silicon: 14, uranium: 92 }; console.log(elements.silicon);
  7. 7. Whitespace today = 'tuesday' if today is 'tuesday' console.log 'UXEG' else console.log 'noop'
  8. 8. Whitespace var today; today = 'tuesday'; if (today === 'tuesday') { console.log('UXEG'); } else { console.log('noop'); }
  9. 9. Loops and comprehensions list = ['a', 'b', 'c'] console.log letter for letter in list process = good: 'good' bad: 'bad' console.log name for name of process console.logi for i in [0..10]
  10. 10. List comprehensions vari, letter, list, name, process, _i, _j, _len; list = ['a', 'b', 'c']; for (_i = 0, _len = list.length; _i< _len; _i++) { letter = list[_i]; console.log(letter); } process = { good: 'good', bad: 'bad’ }; for (name in process) { console.log(name); } for (i = _j = 0; _j<= 10; i = ++_j) { console.log(i); }
  11. 11. Existential operator person = name: 'John Doe' age: 28 console.logperson.name.length console.logperson.non?.existent.property console.logperson.name.reverse?()
  12. 12. Existential operator varperson, _base, _ref; person = { name: 'John Doe', age: 28 }; console.log(person.name.length); console.log((_ref = person.non) != null ? _ref.existent.property : void 0); console.log(typeof (_base = person.name).reverse === "function" ? _base.reverse() : void 0);
  13. 13. Classes class Message show: -> console.log 'Foo' class BarMessage extends Message show: -> console.log 'Bar’ new BarMessage().show()
  14. 14. Conditionals if a console.log "a is true" else console.log "a is false” console.log "a is false" unless a
  15. 15. Scope a = 'A' myFunc = -> a = 'AAA' b = 'B'
  16. 16. Scope (function() { var a, myFunc; a = 'A’; myFunc = function() { varb; a = 'AAA'; return b = 'B'; }; }).call(this);
  17. 17. String Interpolation name = "John Doe" console.log "Hello #{name}" console.log 'Hello #{name}'
  18. 18. String Interpolation var name; name = "John Doe"; console.log("Hello " + name); console.log('Hello #{name}');
  19. 19. Building coffee – a command line tool cake – a simple build tool coffee -w -o lib/ -jmergedSource.js-csrc/ -w – to watch for changes while we write -o – to specify output location -j – to merge all files into one -c – to specify input location
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×