10. “
I need to come up with a scripting language
for web browsers.
I know! I'll make it cool and Lispy! ”
Dramatic Re-enactment (1995)
12. “ Yeah, So... Java is getting really popular.
So we're going to need you to rewrite your
language into something a bit more Java-
esque and name it something like JavaScript.
Yeah, and we're going to need it in a week.
Thanks, that'd be great. ”
Dramatic Re-enactment (1995)
13. var MyApp.Models.Product = Backbone.Model.extend({
isLiked: function() {
var _ref;
return ! ((_ref = this.like()) != null ? _ref.isNew() : void 0);
};
like: function() {
return new MyApp.Models.Like(this.get("like"));
};
image: function(size) {
var img;
if (size == null) {
size = "full";
}
if (this.get("image") != null) {
img = this.get("image")[size];
}
if (img == null) {
img = "/images/fallback/product_" + size + "_default.png";
}
return img;
};
});
21. What is CoffeeScript?
“A little language that compiles into JavaScript.”
Easily integrates with your current JavaScript
22. What is CoffeeScript?
“A little language that compiles into JavaScript.”
Easily integrates with your current JavaScript
Easier to read, write, maintain, refactor, etc...
23. What is CoffeeScript?
“A little language that compiles into JavaScript.”
Easily integrates with your current JavaScript
Easier to read, write, maintain, refactor, etc...
A Hybrid of Ruby and Python.
24. What is CoffeeScript?
“A little language that compiles into JavaScript.”
Easily integrates with your current JavaScript
Easier to read, write, maintain, refactor, etc...
A Hybrid of Ruby and Python.
Helpful.
30. .MODEL SMALL DISP:
MOV BL,VAL1
.STACK 64 ADD BL,VAL2
.DATA MOV AH,00H
VAL1 DB 01H MOV AL,BL
VAL2 DB 01H MOV LP,CL
LP DB 00H MOV CL,10
V1 DB 00H DIV CL
V2 DB 00H MOV CL,LP
NL DB 0DH,0AH,'$'
MOV V1,AL
.CODE MOV V2,AH
MAIN PROC MOV DL,V1
MOV AX,@DATA ADD DL,30H
MOV DS,AX MOV AH,02H
INT 21H
MOV AH,01H
INT 21H MOV DL,V2
MOV CL,AL ADD DL,30H
SUB CL,30H MOV AH,02H
SUB CL,2 INT 21H
MOV AH,02H MOV DL,VAL2
MOV DL,VAL1 MOV VAL1,DL
ADD DL,30H MOV VAL2,BL
INT 21H
MOV AH,09H
MOV AH,09H LEA DX,NL
LEA DX,NL INT 21H
INT 21H
LOOP DISP
MOV AH,02H
MOV DL,VAL2 MOV AH,4CH
ADD DL,30H INT 21H
INT 21H
MAIN ENDP
MOV AH,09H END MAIN
LEA DX,NL
INT 21H
31. .MODEL SMALL DISP:
.STACK 64
MOV BL,VAL1
ADD BL,VAL2 Assembly
.DATA MOV AH,00H
VAL1 DB 01H MOV AL,BL
VAL2 DB 01H MOV LP,CL
LP DB 00H MOV CL,10
V1 DB 00H DIV CL
V2 DB 00H MOV CL,LP
NL DB 0DH,0AH,'$'
MOV V1,AL
.CODE MOV V2,AH
MAIN PROC MOV DL,V1
MOV AX,@DATA ADD DL,30H
MOV DS,AX MOV AH,02H
INT 21H
MOV AH,01H
INT 21H MOV DL,V2
MOV CL,AL ADD DL,30H
SUB CL,30H MOV AH,02H
SUB CL,2 INT 21H
MOV AH,02H MOV DL,VAL2
MOV DL,VAL1 MOV VAL1,DL
ADD DL,30H MOV VAL2,BL
INT 21H
MOV AH,09H
MOV AH,09H LEA DX,NL
LEA DX,NL INT 21H
INT 21H
LOOP DISP
MOV AH,02H
MOV DL,VAL2 MOV AH,4CH
ADD DL,30H INT 21H
INT 21H
MAIN ENDP
MOV AH,09H END MAIN
LEA DX,NL
INT 21H
32. #include <stdio.h> C
int fibonacci()
{
int n = 100;
int a = 0;
int b = 1;
int sum;
int i;
for (i = 0; i < n; i++)
{
printf("%dn", a);
sum = a + b;
a = b;
b = sum;
}
return 0;
}
33. public static void fibonacci() { Java
int n = 100;
int a = 0;
int b = 1;
for (int i = 0; i < n; i++) {
System.out.println(a);
a = a + b;
b = a - b;
}
}
34. def fibonacci Ruby
a = 0
b = 1
100.times do
printf("%dn", a)
a, b = b, a + b
end
end
35. “
I can write an app just as well in Java
as I can in Ruby, but damn it if Ruby
isn’t nicer to read and write! ”
38. $(function() { $ ->
success = (data) ->
success = function(data) { if data.errors?
if (data.errors != null) { alert "There was an error!"
alert("There was an error!"); else
} else { $("#content").text(data.message)
$("#content").text(data.message);
} $.get('/users', success, 'json')
};
$.get('/users', success, 'json');
}); JavaScript CoffeeScript
39. Syntax Rules
No semi-colons (ever!)
No curly braces*
No ‘function’ keyword
Relaxed parentheses
Whitespace significant formatting
40. Parentheses Rules
# Not required without arguments:
noArg1 = ->
# do something
# Not required without arguments:
noArg2 = () ->
# do something
# Required without arguments:
# Required with Arguments: noArg1()
withArg = (arg) -> noArg2()
# do something
# Not required with
arguments:
withArg("bar")
withArg "bar"
43. Whitespace
$(function() {
success = function(data) {
if (data.errors != null) {
alert("There was an error!");
} else {
$("#content").text(data.message);
}
};
$.get('/users', success, 'json');
});
44. Whitespace
$(function() { def fibonacci
success = function(data) { a = 0
if (data.errors != null) { b = 1
alert("There was an error!"); 100.times do
} else { printf("%dn", a)
$("#content").text(data.message); a, b = b, a + b
} end
}; end
$.get('/users', success, 'json');
});
45. Whitespace
$(function() { def fibonacci
a = 0
success = function(data) { b = 1
if (data.errors != null) {
alert("There was an error!"); 100.times do
} else { printf("%dn", a)
$("#content").text(data.message); a, b = b, a + b
} end
};
end
$.get('/users', success, 'json');
});
46. Whitespace
$ ->
success = (data) ->
if data.errors?
alert "There was an error!"
else
$("#content").text(data.message)
$.get('/users', success, 'json')
73. Extending Classes
var Manager, manager,
__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; };
Manager = (function(_super) {
__extends(Manager, _super);
Manager.name = 'Manager';
function Manager() {
var _base;
Manager.__super__.constructor.apply(this, arguments);
}
Manager.prototype.salary = function() {
return "" + Manager.__super__.salary.apply(this, arguments) + " w/ $10k Bonus";
};
return Manager;
})(Employee);
manager = new Manager();
console.log(manager.salary());
74. Bound Functions
class User
constructor: (@name) ->
sayHi: ->
console.log "Hello #{@name}"
bob = new User('bob')
mary = new User('mary')
log = (callback)->
console.log "about to execute callback..."
callback()
console.log "...executed callback"
log(bob.sayHi)
log(mary.sayHi)
75. Bound Functions
about to execute callback...
Hello undefined
...executed callback
about to execute callback...
Hello undefined
...executed callback
76. Bound Functions
class User
constructor: (@name) ->
sayHi: ->
console.log "Hello #{@name}"
bob = new User('bob')
mary = new User('mary')
log = (callback)->
console.log "about to execute callback..."
callback()
console.log "...executed callback"
log(bob.sayHi)
log(mary.sayHi)
77. Bound Functions
class User
constructor: (@name) ->
sayHi: ->
console.log "Hello #{@name}"
bob = new User('bob')
mary = new User('mary')
log = (callback)->
console.log "about to execute callback..."
callback()
console.log "...executed callback"
log(bob.sayHi)
log(mary.sayHi)
78. Bound Functions
class User
constructor: (@name) ->
sayHi: =>
console.log "Hello #{@name}"
bob = new User('bob')
mary = new User('mary')
log = (callback)->
console.log "about to execute callback..."
callback()
console.log "...executed callback"
log(bob.sayHi)
log(mary.sayHi)
79. Bound Functions
about to execute callback...
Hello bob
...executed callback
about to execute callback...
Hello mary
...executed callback
92. The Raven
Once upon a mignight dreary while I pondered, weak and weary,
Over many quaint and curious volume of forgotten lore -
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door
"'Tis some visiter". I muttered, "tapping at my chamber door" -
"only this and nothing more."
Ah distinctly I remember it was in the bleak December;
And each separate dying ember wrought its ghost upon the floor.
Eagerly I wished the morrow - vainly I had sought to borrow,
From my books surcease of sorrow - sorrow For the lost Lenore -
For the rare and radiant maiden whom the angels name Lenore -
Nameless here For evermore
93. The Raven
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; };
while (I(pondered, weak && weary, Over(many(quaint && curious(volume in
forgotten(lore - While(I(nodded, nearly(napping, suddenly(there(came(a(tapping,
As in some(one(gently(rapping, rapping(at(my(chamber(door)))))))))))))))))))) {
Once(upon(a(mignight(dreary))));
}
"'Tis some visiter".I(muttered, "tapping at my chamber door" - "only this and
nothing more.");
Ah(distinctly(I(remember(it(__indexOf.call(the(bleak(December)), was) >= 0)))));
And(each(separate(dying(ember(wrought(its(ghost(upon(the(floor.Eagerly(I(wished(
the(morrow - vainly(I(had(sought(to(borrow, From(my(books(surcease in sorrow -
sorrow(For(the(lost(Lenore - For(the(rare &&
radiant(maiden(whom(the(angels(name(Lenore -
Nameless(here(For(evermore)))))))))))))))))))))))))))))))))))));