5. <section id="users">
<div class="user" data-user-id="45">
<strong class="user__nickname">
czajkovsky
</strong>
Some info about this user.
</</div>
</section>
HTML
HTMLVSHAML&SLIM
6. <section id="users">
<div class="user" data-user-id="45">
<strong class="user__nickname">
czajkovsky
</strong>
Some info about this user.
</</div>
</section>
HTML
HAML %section#users
.user{ data: { user_id: 45 } }
%strong.user__nickname czajkovsky
Some info about this user.
HTMLVSHAML&SLIM
7. <section id="users">
<div class="user" data-user-id="45">
<strong class="user__nickname">
czajkovsky
</strong>
Some info about this user.
</</div>
</section>
HTML
SLIM section#users
.user data-user-id=45
strong.user__nickname czajkovsky
| Some info about this user.
HAML %section#users
.user{ data: { user_id: 45 } }
%strong.user__nickname czajkovsky
Some info about this user.
HTMLVSHAML&SLIM
8. <section id="users">
<div class="user" data-user-id="45">
<strong class="user__nickname">
czajkovsky
</strong>
Some info about this user.
</</div>
</section>
HTML
SLIM section#users
.user data-user-id=45
strong.user__nickname czajkovsky
| Some info about this user.
HAML %section#users
.user{ data: { user_id: 45 } }
%strong.user__nickname czajkovsky
Some info about this user.
enforcesondevelopercorrect
indent
youdon’thavetorememberabout
closingtags
codeismuchshorter
PROS
HTMLVSHAML&SLIM
9. <section id="users">
<div class="user" data-user-id="45">
<strong class="user__nickname">
czajkovsky
</strong>
Some info about this user.
</</div>
</section>
HTML
SLIM section#users
.user data-user-id=45
strong.user__nickname czajkovsky
| Some info about this user.
HAML %section#users
.user{ data: { user_id: 45 } }
%strong.user__nickname czajkovsky
Some info about this user.
enforcesondevelopercorrect
indent
youdon’thavetorememberabout
closingtags
codeismuchshorter
PROS
newdevelopermustbefamiliarwithit
(learningtakes20minutes)
CONS?
HTMLVSHAML&SLIM
10. <section id="users">
<div class="user" data-user-id="45">
<strong class="user__nickname">
czajkovsky
</strong>
Some info about this user.
</</div>
</section>
HTML
SLIM section#users
.user data-user-id=45
strong.user__nickname czajkovsky
| Some info about this user.
HAML %section#users
.user{ data: { user_id: 45 } }
%strong.user__nickname czajkovsky
Some info about this user.
enforcesondevelopercorrect
indent
youdon’thavetorememberabout
closingtags
codeismuchshorter
PROS
newdevelopermustbefamiliarwithit
(learningtakes20minutes)
CONS?
Slim isfaster
Slim allowsyoutodefinecustom
shortcuts
HAMLismorepopular
HAMLVSSLIM
HTMLVSHAML&SLIM
32. class Lecture
constructor: (@title, @author) ->
@slides = []
display_date: ->
if @date? then @date else "Sorry, no info about date for #{@title}"
add_slide: (name, duration) ->
slide =
name: name
duration: duration
@slides.push slide
long_slides: ->
s.name for s in @slides when s.duration > 3
CoffeeScript
33. class Lecture
constructor: (@title, @author) ->
@slides = []
display_date: ->
if @date? then @date else "Sorry, no info about date for #{@title}"
add_slide: (name, duration) ->
slide =
name: name
duration: duration
@slides.push slide
long_slides: ->
s.name for s in @slides when s.duration > 3
CoffeeScript
codeisshorterby1/3withoutany
incluenceonexecutiontime
(iscompiledtoJS)
syntax(defaultinRoRfromversion3.1)
Pros
34. class Lecture
constructor: (@title, @author) ->
@slides = []
display_date: ->
if @date? then @date else "Sorry, no info about date for #{@title}"
add_slide: (name, duration) ->
slide =
name: name
duration: duration
@slides.push slide
long_slides: ->
s.name for s in @slides when s.duration > 3
CoffeeScript
codeisshorterby1/3withoutany
incluenceonexecutiontime
(iscompiledtoJS)
syntax(defaultinRoRfromversion3.1)
Pros
syntax
Cons
37. CoffeeScript
'foo exists' if foo?
# typeof foo !== "undefined" &&
# foo !== null
foo?.prop?.subprop?
foo.prop ?= 'new val'
Existentialoperator
@id # this.id
class Lecture
@findByTitle: (title) ->
...
@Alias
# foo = { bar: 1, baz: 2 }
foo =
bar: 1
baz: 2
# range = [2, 3, 4, 5]
range = [2..5]
Objects,arrays
38. CoffeeScript
'foo exists' if foo?
# typeof foo !== "undefined" &&
# foo !== null
foo?.prop?.subprop?
foo.prop ?= 'new val'
Existentialoperator
@id # this.id
class Lecture
@findByTitle: (title) ->
...
@Alias
# foo = { bar: 1, baz: 2 }
foo =
bar: 1
baz: 2
# range = [2, 3, 4, 5]
range = [2..5]
Objects,arrays
for name, i in ['HAML', 'CSS', 'JS']
alert "#{i}: #{name}"
# a = [{ name: 'HAML', duration: 5 }, ...]
s.name for s in a when s.duration > 3
Loops&comprehensions
39. CoffeeScript
foo = (bar) ->
# your function body
square = (x) -> x * x
power = (a = 1, b = 2) ->
Math.pow(a, b)
Functions
40. CoffeeScript
foo = (bar) ->
# your function body
square = (x) -> x * x
power = (a = 1, b = 2) ->
Math.pow(a, b)
Functions
foo: ->
# your function body
"this will be returned"
Autoreturn
41. CoffeeScript
foo = (bar) ->
# your function body
square = (x) -> x * x
power = (a = 1, b = 2) ->
Math.pow(a, b)
Functions
'boring...' if sleeping_folks > 2
unless foo is 1
if bar isnt 2 and baz is 7
'mid-length' if 3 < l.duration < 5
Flowcontrol
foo: ->
# your function body
"this will be returned"
Autoreturn
42. CoffeeScript
foo = (bar) ->
# your function body
square = (x) -> x * x
power = (a = 1, b = 2) ->
Math.pow(a, b)
Functions
'boring...' if sleeping_folks > 2
unless foo is 1
if bar isnt 2 and baz is 7
'mid-length' if 3 < l.duration < 5
Flowcontrol
foo: ->
# your function body
"this will be returned"
Autoreturn
"#{title} by #{author}" # OK
'#{title} by #{author}' # won't work
Interpolation
43. CoffeeScript
class Lecture
prepare: (@subject) ->
notify: ->
alert @subject()
class Speaker
constructor: (@name) ->
@lecture = new Lecture
@lecture.prepare () => "New lecture by #{@name}"
p = new Speaker('czajkovsky')
p.lecture.notify()
Fatarrow
44. CoffeeScript
class Lecture
prepare: (@subject) ->
notify: ->
alert @subject()
class Speaker
constructor: (@name) ->
@lecture = new Lecture
@lecture.prepare () => "New lecture by #{@name}"
p = new Speaker('czajkovsky')
p.lecture.notify()
Fatarrow
[month, day, year] = 'June 2 14'.split ' '
Multipleassignment
45. class Lecture
constructor: (@title, @author) ->
@slides = []
display_date: ->
if @date? then @date else "Sorry, no info about date for #{@title}"
long_slides: ->
s.name for s in @slides when s.duration > 3
add_slide: (name, duration) ->
slide =
name: name
duration: duration
@slides.@slides.push slide
CoffeeScript
46. class Lecture
constructor: (@title, @author) ->
@slides = []
display_date: ->
if @date? then @date else "Sorry, no info about date for #{@title}"
long_slides: ->
s.name for s in @slides when s.duration > 3
add_slide: (name, duration) ->
slide =
name: name
duration: duration
@slides.@slides.push slide
l = new Lecture('Front-end tools', 'czajkovsky')
l.display_date()
# Sorry, no info about date for Front-end tools
CoffeeScript
47. class Lecture
constructor: (@title, @author) ->
@slides = []
display_date: ->
if @date? then @date else "Sorry, no info about date for #{@title}"
long_slides: ->
s.name for s in @slides when s.duration > 3
add_slide: (name, duration) ->
slide =
name: name
duration: duration
@slides.@slides.push slide
l = new Lecture('Front-end tools', 'czajkovsky')
l.display_date()
# Sorry, no info about date for Front-end tools
l.date = '02/06/2014 18:30'
l.display_date()
# 02/06/2014 18:30
CoffeeScript
48. class Lecture
constructor: (@title, @author) ->
@slides = []
display_date: ->
if @date? then @date else "Sorry, no info about date for #{@title}"
long_slides: ->
s.name for s in @slides when s.duration > 3
add_slide: (name, duration) ->
slide =
name: name
duration: duration
@slides.@slides.push slide
l = new Lecture('Front-end tools', 'czajkovsky')
l.display_date()
# Sorry, no info about date for Front-end tools
l.date = '02/06/2014 18:30'
l.display_date()
# 02/06/2014 18:30
l.add_slide(l.add_slide('HAML', 2)
l.add_slide('Sass', 5)
l.add_slide('CoffeeScript', 4)
l.long_slides()
# ["Sass", "CoffeeScript"]
CoffeeScript