This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
/
index.html
539 lines (501 loc) · 20.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to CoffeeScript</title>
<meta name="description" content="CFCamp 2013 presentation - an introduction to the CoffeeScript language">
<meta name="author" content="Marcin Szczepanski">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/beige.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<link rel="stylesheet" href="plugin/coffeecode/codemirror.css">
<link rel="stylesheet" href="plugin/coffeecode/solarized.css">
<link rel="stylesheet" href="css/custom.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Introduction to CoffeeScript</h1>
<p>Marcin Szczepanski, webqem</p>
<p>CFCamp 2013</p>
</section>
<section>
<h1>G'Tag!</h1>
</section>
<section>
<h2>Agenda</h2>
<ul>
<li>What is CoffeeScript?</li>
<li>CoffeeScript syntax</li>
<li>Using CoffeeScript</li>
</ul>
</section>
<section>
<section>
<h2>What is CoffeeScript?</h2>
<blockquote class="fragment">“A better way of writing JavaScript”</blockquote>
</section>
<section>
<h2>Alt.JS</h2>
<p>Like TypeScript</p>
<p>Not like ClojureScript, Roy, etc</p>
</section>
<section>
<h2>It's a transpiler!</h2>
</section>
</section>
<section>
<h2>So what is it?</h2>
<ul>
<li class="fragment">It's like a mix of Ruby, Python and Javascript</li>
<li class="fragment">No curly braces</li>
<li class="fragment">No semi-colons</li>
<li class="fragment">(Mostly) optional parentheses</li>
<li class="fragment">Syntactically significant whitespace</li>
</ul>
<aside class="notes">It's like a mix of Ruby, Python and Javascript.
No curly braces. (Mostly) optional parentheses.
Syntactically significant whitespace.
</aside>
</section>
<section>
<h2>Why not just use JavaScript?</h2>
<ul>
<li class="fragment">It's just more fun to write CoffeeScript!</li>
<li class="fragment">It can be easier to read</li>
<li class="fragment">No technical reason</li>
</ul>
</section>
<section>
<h2>So what are the drawbacks?</h2>
<ul>
<li class="fragment">More difficult to debug <span class="fragment">(Source Maps help)</span></li>
<li class="fragment">Requires a compile step <span class="fragment">(Tools make this less painful)</span></li>
<li class="fragment">Potential clashes with ES6 Harmony functionality</li>
<li class="fragment"><strong>It's not a substitute for knowing JavaScript!</strong></li>
</ul>
</section>
<section>
<h2>Should I use it?</h2>
</section>
<section>
<section>
<h2>So what does it look like‽</h2>
<p class="fragment">I'll get to that...</p>
</section>
<section>
<h2>Common pattern:</h2>
<pre><code class="javascript" data-trim contenteditable>
$(function () {
$(".link").click(function () {
console.log("You clicked a link: " + this.href);
});
});</code></pre>
</section>
<section>
<h2>In CoffeeScript</h2>
<pre><code class="javascript" data-trim>$(function () {
$(".link").click(function () {
console.log("You clicked a link: " + this.href);
});
});</code></pre>
<p>would be written as..</p>
<pre><code class="coffeescript">$ ->
$(".link").click ->
console.log "You clicked a link: #{@href}"</code></pre>
</section>
<section>
<h2>Function declaration</h2>
<textarea class="source compile-only">func = (param1, param2) ->
console.log param1
func = () ->
console.log "Hello!"
func = ->
console.log "Hello!"
sum = (a, b) -> a + b
</textarea>
</section>
<section>
<h2>CoffeeScript output</h2>
<textarea class="source compile-only iffe short">$ ->
$(".link").click ->
console.log "You clicked a link: #{@href}"</textarea>
<p><em>iffy</em>: Immediately-Invoked Function Expression (IIFE)</p>
</section>
</section>
<section>
<section>
<h2>CoffeeScript Constructs</h2>
</section>
<section>
<h3>Variable, Object and Array Definitions</h3>
</section>
<section>
<h4>Variables</h4>
<textarea class="source short">name = "Marcin"
console.log @location
</textarea>
</section>
<section>
<h4>Variables: Warning!</h4>
<textarea class="source">name = "Marcin"
a = ->
something = true
name = "Lucas" # DANGER!
a()
console.log name
</textarea>
</section>
<section>
<h4>Arrays</h4>
<textarea class="source compile-only">cities = ["Sydney", "Munich"]
cities = [
"Sydney"
"Munich"
]</textarea>
</section>
<section>
<h4>Objects</h4>
<textarea class="source compile-only">cities =
Munich:
population: 1388308
region: 'Bavaria'
districts: [
"Altstadt-Lehel"
"Isarvorstadt"
"..."
]</textarea>
</section>
</section>
<section>
<section>
<h2>Conditionals</h2>
</section>
<section>
<h3>if</h3>
<textarea class="source compile-only">if x == 2
console.log "x is 2"</textarea>
</section>
<section>
<h3>if</h3>
<textarea class="source compile-only">console.log "x is 2" if x == 2</textarea>
</section>
<section>
<h3>if: assignment</h3>
<textarea class="source">i = 2
result = if i % 2 == 0
"even"
else
"odd"
console.log result</textarea>
</section>
<section>
<h3>unless</h3>
<pre><code class="coffeescript">console.log "x is NOT 2" if x != 2</code></pre>
<div class="fragment">
<textarea class="source compile-only">console.log "x is NOT 2" unless x == 2
console.log "x is NOT 2" if x != 2</textarea>
</div>
</section>
<section>
<h3>unless</h3>
<textarea class="source compile-only">unless x == 2
console.log "x is NOT 2"</textarea>
</section>
<section>
<h3>unless</h3>
<textarea class="source compile-only">unless x == 2
console.log "x is NOT 2"
else
console.log "x is 2"</textarea>
<p><strong>NOT</strong> good practice!</p>
</section>
<section>
<h3>Aliases</h3>
<textarea class="source compile-only">console.log "x is 2" if x is 2
console.log "x is 2" if x == 2</textarea>
</section>
<section>
<h3>Aliases</h3>
<img src="images/coffeescript-aliases.png">
</section>
</section>
<section>
<section>
<h2>Loops and Comprehensions</h2>
</section>
<section>
<h2>Array Comprehensions</h2>
<pre><code class="coffeescript">result = (expression for object in array)</code></pre>
<pre class="fragment"><code class="coffeescript">result = (expression for object in array when condition)</code></pre>
</section>
<section>
<h2>Array Comprehensions</h2>
<textarea class="source short">doubled = (x * 2 for x in [1, 2, 3])
console.log doubled</textarea>
</section>
<section>
<h2>Array Comprehensions: Filter</h2>
<textarea class="source short">even = (x for x in [1, 2, 3 ,4] when x % 2 is 0)
console.log even</textarea>
</section>
<section>
<h2>Object Comprehensions</h2>
<pre><code class="coffeescript">result = (expression for key of object)</code></pre>
<pre class="fragment"><code class="coffeescript">result = (expression for own key, value of object)</code></pre>
</section>
<section>
<h2>Object Comprehensions</h2>
<textarea class="source">cities = {Munich: {population: 1388308},Sydney: {population: 4627345}}
console.log (value.population for own key, value of cities)
console.log (city for city of cities)
</textarea>
</section>
<section><h2>Comprehensions: Complex Expressions</h2>
<textarea class="source short">x = (for i in [1,2,3,4]
if i % 2
i *= 2
else
i += 1
)
console.log x
</textarea>
</section>
<section><h2>Comprehensions: Complex Expressions</h2>
<textarea class="source short">x = (for i in [1,2,3,4] when i % 2 isnt 0
i *= 2
)
console.log x
</textarea>
</section>
</section>
<section>
<section>
<h2>Ranges</h2>
</section>
<section>
<h2>Ranges</h2>
<textarea class="source">even = (x for x in [1..10] when x % 2 is 0)
console.log even</textarea>
</section>
<section>
<h2>Ranges</h2>
<textarea class="source compile-only">for x in [0...10]
console.log x</textarea>
</section>
<section>
<h2>Ranges: Splicing</h2>
<textarea class="source">cities = [
"Sydney"
"Munich"
"London"
"San Francisco"
]
console.log cities[0..1]</textarea>
<aside class="notes"><pre>cities[0..1]
is equivalent to
cities[..1]
You can use negative indexes:
cities[-1..]
As you can see all these options translate to simple Javascript array splices
As a nice bonus you can also replace the result of a slice:
cities[-1..] = "New York"
</pre></aside>
</section>
<section>
<h2>Ranges: Splice assignment</h2>
<textarea class="source">cities = [
"Sydney"
"Munich"
"London"
"San Francisco"
]
cities[-2..] = "Berlin"
console.log cities</textarea>
</section>
</section>
<section>
<section>
<h2>Classes</h2>
<div class="fragment">
<textarea class="source short">class City
constructor: (@name, @population) ->
toString: ->
"#{@name} has population #{@population}"
city = new City('Munich', 12345)
console.log city.toString()</textarea>
</div>
</section>
<section>
<h2>Classes: Inheritance</h2>
<textarea class="source short">class Animal
constructor: (@name) ->
makeSound: (sound) ->
"#{@name} goes #{sound}"
class Dog extends Animal
makeSound: ->
super "Woof!"
lucy = new Dog('Lucy')
console.log lucy.makeSound()</textarea>
</section>
</section>
<section>
<section>
<h2>Miscellany</h2>
<p>Syntax stuff that didn't fit in the other categories!</p>
</section>
<section>
<h2><em>this</em></h2>
<textarea class="source compile-only">$(".link").click ->
console.log @id
</textarea>
</section>
<section>
<h2><em>this:</em> Fat Arrow</h2>
<textarea class="source compile-only">$(".link").click ->
$(".foo").click =>
console.log @id
</textarea>
</section>
<section>
<h2>Splats</h2>
<textarea class="source">
math = (operator, numbers...) ->
console.log numbers
math "+", 1, 2, 3
</textarea>
</section>
<section>
<h2>Destructuring Assignment</h2>
<textarea class="source">
foo = ->
["one", "two"]
[a, b] = foo()
console.log a</textarea>
</section>
<section>
<h2>Destructuring Assignment and Splats</h2>
<textarea class="source">
foo = ->
["one", "two", "three"]
[a, b...] = foo()
console.log b</textarea>
</section>
</section>
<section>
<section>
<h2>Using CoffeeScript</h2>
</section>
<section>
<h2>Installing CoffeeScript</h2>
<pre><code class="shell">npm install -g coffee-script</code></pre>
</section>
<section>
<h2>Running CoffeeScript</h2>
<pre class="fragment">coffee # REPL</pre>
<pre class="fragment">coffee file.coffee # run script</pre>
<pre class="fragment">coffee -c file.coffee # compile</pre>
<pre class="fragment">coffee -wc file.coffee # watch & compile</pre>
<pre class="fragment">coffee -cm file.coffee # sourcemap</pre>
<pre class="fragment">coffee -wmc *.coffee # ALL THE THINGS!</pre>
</section>
<section>
<h2>coffee-script.js</h2>
<pre><code class="html"><script type="text/javascript" src="js/coffee-script.js"></script>
<script type="text/coffeescript">window.alert "Hello World!"</script></code></pre>
<pre class="fragment"><code class="javascript">var compiled = CoffeeScript.compile(source);</code></pre>
</section>
<section>
<h2>Using CoffeeScript with ColdFusion</h2>
<ul>
<li class="fragment">Pre-compile: <em>coffee</em>, GruntJS</li>
<li class="fragment">CFStatic (Demo)</li>
</ul>
</section>
</section>
<section>
<h2>Summary</h2>
<ul>
<li class="fragment">CoffeeScript makes writing JavaScript fun!</li>
<li class="fragment">Useful syntactic sugar</li>
</ul>
<h3 class="fragment"><strong>But..</strong></h3>
<ul>
<li class="fragment">It adds complexity to your dev process</li>
<li class="fragment">Potential clashes with future JS functionality</li>
<li class="fragment">Might be solving a problem you don't have</li>
</ul>
</section>
<section>
<h2>The End</h2>
<ul>
<li>Resources
<ul>
<li><a href="#">coffeescript.org</a> - Documentation, downloads, etc</li>
<li><a href="https://github.com/DominicWatson/cfstatic">github.com/DominicWatson/cfstatic</a> - CFStatic</li>
<li><a href="#">gruntjs.com</a> - GruntJS</li>
</ul>
</li>
<li>Get in touch
<ul>
<li>marcins@webqem.com</li>
<li>@MarcinS - Twitter</li>
</ul>
</li>
<li>Presentation will be on Github soon: <a href="https://github.com/marcins/cfcamp-coffeescript">https://github.com/marcins/cfcamp-coffeescript</a></li>
</ul>
</section>
</div> <!-- end slides -->
</div> <!-- end reveal -->
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script src="plugin/coffeecode/coffee-script.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
keyboard: {
"67": function () {
CoffeeCode.compileAndRun();
},
"83": function () {
RevealNotes.open();
}
},
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/coffeecode/coffeecode.js', async: true, condition: function () { return !!document.body.classList;}}
// { src: 'plugin/search/search.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
<script src="plugin/coffeecode/codemirror.js"></script>
<script src="plugin/coffeecode/coffeescript.js"></script>
</body>
</html>