7. “
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)
9. “ 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)
10. 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;
};
});
18. What is CoffeeScript?
“A little language that compiles into JavaScript.”
Easily integrates with your current JavaScript
19. What is CoffeeScript?
“A little language that compiles into JavaScript.”
Easily integrates with your current JavaScript
Easier to read, write, maintain, refactor, etc...
20. 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.
21. 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.
26. .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
27. .MODEL SMALL DISP:
Assembly 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
28. #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;
}
29. 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;
}
}
30. def fibonacci Ruby
a = 0
b = 1
100.times do
printf("%dn", a)
a, b = b, a + b
end
end
31. “
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! ”
34. $(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
35. Syntax Rules
No semi-colons (ever!)
No curly braces*
No ‘function’ keyword
Relaxed parentheses
Whitespace significant formatting
36. 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"
40. Whitespace
$(function() {
success = function(data) {
if (data.errors != null) {
alert("There was an error!");
} else {
$("#content").text(data.message);
}
};
$.get('/users', success, 'json');
});
41. 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');
});
42. 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');
});
43. Whitespace
$ ->
success = (data) ->
if data.errors?
alert "There was an error!"
else
$("#content").text(data.message)
$.get('/users', success, 'json')
44. Whitespace
$(function() {
var success;
success = function(data) {
if (data.errors != null) {
return alert("There was an error!");
} else {
return $("#content").text(data.message);
}
};
return $.get('/users', success, 'json');
});
63. Default Arguments
createElement = (name, attributes = {}) ->
obj = document.createElement name
for key, val of attributes
obj.setAttribute key, val
obj
64. Default Arguments
var createElement;
createElement = function(name, attributes) {
var key, obj, val;
if (attributes == null) {
attributes = {};
}
obj = document.createElement(name);
for (key in attributes) {
val = attributes[key];
obj.setAttribute(key, val);
}
return obj;
};
79. Extending Classes
class Manager extends Employee
constructor: ->
super
@options.salary ?= "$500,000"
manager = new Manager()
console.log manager.salary() # "$500,000"
80. 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);
if ((_base = this.options).salary == null) {
_base.salary = "$500,000";
}
}
return Manager;
})(Employee);
manager = new Manager();
console.log(manager.salary());
81. Extending Classes
class Manager extends Employee
constructor: ->
super
@options.salary ?= "$500,000"
salary: ->
"#{super} + $10k"
manager = new Manager()
console.log manager.salary() # "$500,000 + $10k"
82. 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);
if ((_base = this.options).salary == null) {
_base.salary = "$500,000";
}
}
Manager.prototype.salary = function() {
return "" + Manager.__super__.salary.apply(this, arguments) + " + $10k";
};
return Manager;
})(Employee);
manager = new Manager();
console.log(manager.salary());
83. 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)
84. Bound Functions
about to execute callback...
Hello undefined
...executed callback
about to execute callback...
Hello undefined
...executed callback
85. 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)
86. 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)
87. 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)
88. Bound Functions
about to execute callback...
Hello bob
...executed callback
about to execute callback...
Hello mary
...executed callback