-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
350 lines (295 loc) · 13.2 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Modern JS isn't as bad as you think</title>
<meta name="author" content="Pierre-Emile Ferron">
<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, minimal-ui">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="reveal.js/css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/monokai.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<!-- Custom styles -->
<style>
.reveal table {
border-collapse: separate;
border-spacing: 10px;
}
.reveal table td {
border: 0;
}
.reveal pre code {
max-height: 500px;
}
/* Used in the slide "Today's app". */
.app-iframe {
width: 500px;
height: 200px;
}
/* Used in the slide "Arrow functions (2/2)". */
.reveal div.replaceable, .reveal div.replaceable.visible + div.replaceable {
display: none;
}
.reveal div.replaceable.visible {
display: block;
}
</style>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Modern JS isn't as bad as you think</h1>
<p>
<small>by <a href="http://peferron.com">@peferron</a></small>
</p>
</section>
<section>
<h2>What is modern JS?</h2>
<p>JS is an implementation of the ECMAScript standard.</p>
<table>
<tr>
<td>ECMAScript 3 (1999)</td>
<td class="fragment" data-fragment-index="1" style="color: red; border-left: 5px solid red;">
Last version supported by IE8
</td>
</tr>
<tr>
<td>ECMAScript 5 (2009)</td>
<td rowspan="3" class="fragment" data-fragment-index="1" style="color: #8CC63F; vertical-align: middle; border-left: 5px solid #8CC63F;">
Modern JS
</td>
</tr>
<tr>
<td>ECMAScript 2015</td>
</tr>
<tr>
<td>ECMAScript 2016</td>
</tr>
</table>
<p class="fragment" data-fragment-index="1" style="color: #8CC63F;">
Client-side: Windows XP support ended in April 2014.<br>
Server-side: Node uses a modern JS engine (V8).<br>
Can compile to older ES versions with <a href="http://babeljs.io">Babel</a>.
</p>
</section>
<section>
<h2>Today's app</h2>
<p>Suggest days with nice weather for visiting San Francisco.</p>
<iframe class="app-iframe" src="app/index.html"></iframe>
<p class="fragment">
Written in <span style="color: red;">ECMAScript 3</span>.
<br>
We will update it to <span style="color: #8CC63F;">ECMAScript 2016</span> together.
</p>
</section>
<section>
<h2>How the app works</h2>
<ol>
<li>
<code>HTTP GET sf-forecast.json</code>
<pre style="margin-left: 0; width: auto;"><code data-trim class="json" data-fill="app/sf-forecast.json"></code></pre>
</li>
<li>Keep only the days with humidity below 90%.</li>
<li>Write days to the webpage.</li>
</ol>
</section>
<section>
<h2>Look ma, no slides!</h2>
<p>Open app.js in text editor.</p>
</section>
<section data-background="#B71C1C">
<h2>Array methods</h2>
<pre><code data-trim class="js" data-fill="examples/array-methods.js"></code></pre>
<p>And <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Iteration_methods">others</a>.</p>
</section>
<section data-background="#B71C1C">
<p>Open app.js and replace <code>for</code> loops with <code>Array</code> methods.</p>
</section>
<section data-background="#880E4F">
<h2>Arrow functions (1/2)</h2>
<pre><code data-trim class="js" data-fill="examples/fat-arrow.js"></code></pre>
</section>
<section data-background="#880E4F">
<h2>Arrow functions (2/2)</h2>
<div class="fragment replaceable" data-fragment-index="4">
<p>But arrow functions work out of the box.</p>
<pre><code data-trim class="js" data-fill="examples/fat-arrow-this-4.js"></code></pre>
</div>
<div class="fragment replaceable" data-fragment-index="3">
<p>Awkward workarounds exist:</p>
<pre><code data-trim class="js" data-fill="examples/fat-arrow-this-3.js"></code></pre>
</div>
<div class="fragment replaceable" data-fragment-index="2">
<p>This should work:</p>
<pre><code data-trim class="js" data-fill="examples/fat-arrow-this-2.js"></code></pre>
</div>
<div class="visible replaceable">
<p>This should work:</p>
<pre><code data-trim class="js" data-fill="examples/fat-arrow-this-1.js"></code></pre>
</div>
<div class="fragment" data-fragment-index="1">
<div class="fragment fade-out" data-fragment-index="3">
<p>But it doesn't.</p>
<pre><code class="markdown">Uncaught TypeError: Cannot read property 'name' of undefined</code></pre>
</div>
</div>
</section>
<section data-background="#880E4F">
<p>Open app.js and replace <code>function</code> with <code>=></code>.</p>
</section>
<section data-background="#4A148C">
<h2>Template strings</h2>
<pre><code data-trim class="js" data-fill="examples/template-strings.js"></code></pre>
</section>
<section data-background="#4A148C">
<p>Open app.js and replace string concatenation with interpolation.</p>
</section>
<section data-background="#311B92">
<h2>Fetch API</h2>
<p>Uses <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promises</a> instead of callbacks.</p>
<pre><code data-trim class="js" data-fill="examples/fetch.js"></code></pre>
</section>
<section data-background="#311B92">
<p>Open app.js and replace <code>XMLHttpRequest</code> with <code>fetch</code>.</p>
</section>
<section data-background="#01579B">
<h2>async / await</h2>
<pre><code data-trim class="js" data-fill="examples/async-await.js"></code></pre>
</section>
<section data-background="#01579B">
<p>Open app.js and replace callbacks with <code>async</code> / <code>await</code>.</p>
</section>
<section data-background="#006064">
<h2>Optional parameters (1/2)</h2>
<p>How to make maximum humidity and minimum temperature configurable?</p>
<div class="fragment" data-fragment-index="1">
<pre class="fragment fade-out" data-fragment-index="3"><code data-trim class="js">
async function getLovelyDays(maxHumidity, minTemperature) {
...
}
</code></pre>
</div>
<pre class="fragment" data-fragment-index="2"><code data-trim class="js">
var lovelyDays = await getLovelyDays(75, 80);
</code></pre>
<p class="fragment" data-fragment-index="3">Unreadable for someone not familiar with the code.</p>
</section>
<section data-background="#006064">
<h2>Optional parameters (2/2)</h2>
<p>This line was in our app before we moved to <code>fetch.</code><br>
Can you infer what <code>true</code> means?</p>
<pre><code data-trim class="js">
request.open('GET', 'sf-forecast.json', true);
</code></pre>
<div class="fragment">
<p>And now?</p>
<pre><code data-trim class="js">
request.open('GET', 'sf-forecast.json', {async: true});
</code></pre>
</div>
</section>
<section data-background="#006064">
<p>Open app.js and add the options <code>maxHumidity</code> and <code>minTemperature</code>.</p>
</section>
<section data-background="#1B5E20">
<h2>Default parameter values</h2>
<pre><code data-trim class="js" data-fill="examples/default-parameter-values.js"></code></pre>
</section>
<section data-background="#1B5E20">
<p>Open app.js and make <code>options</code> default to <code>{}</code>.</p>
</section>
<section data-background="#827717">
<h2>Destructuring</h2>
<pre><code data-trim class="js" data-fill="examples/destructuring.js"></code></pre>
<p>Deeply nested patterns are supported, but can hurt readability. Use responsibly.</p>
</section>
<section data-background="#827717">
<p>Open app.js and use destructuring with default values for <code>maxHumidity</code> and <code>minTemperature</code>.</p>
</section>
<section>
<h2>Before / after</h2>
<ul>
<li>30 lines → 13 lines</li>
<li>Expresses intent much more clearly</li>
<li>Eliminates bug-prone mutation and manual loops</li>
</ul>
</section>
<section>
<h2>Other awesome features</h2>
<ul>
<li><a href="http://www.2ality.com/2015/02/es6-scoping.html"><code>let</code> and <code>const</code></a>
<li><a href="https://github.com/lukehoban/es6features#modules">Modules</a></li>
<li><a href="https://github.com/lukehoban/es6features#classes">Classes</a></li>
<li><a href="http://www.2ality.com/2015/06/tail-call-optimization.html">Tail call optimization</a></li>
<li><a href="https://github.com/lukehoban/es6features#iterators--forof">Iterators</a></li>
<li><a href="https://github.com/lukehoban/es6features#generators">Generators</a></li>
<li><a href="https://github.com/lukehoban/es6features#map--set--weakmap--weakset"><code>Map</code>, <code>Set</code>, <code>WeakMap</code> and <code>WeakSet</code></a></li>
<li><a href="https://github.com/lukehoban/es6features#math--number--string--array--object-apis">New methods in <code>Math</code>, <code>String</code> and <code>Number</code></a></li>
</ul>
</section>
<section>
<p>
<h2>Slides and source code</h2>
<a href="https://github.com/peferron/modern-js-talk">github.com/peferron/modern-js-talk</a>
</p>
<p>
<h2>Credits</h2>
<ul>
<li><a href="https://youtu.be/wf-BqAjZb8M">Raymond Hettinger - Beyond PEP 8, PyCon 2015</a></li>
<li><a href="http://www.2ality.com">Axel Rauschmayer / 2ality.com</a></li>
<li><a href="https://github.com/lukehoban/es6features">github.com/lukehoban/es6features</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web">Mozilla Developer Network</a></li>
</ul>
</p>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector('pre code'); }, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
<script>
Reveal.addEventListener('slidechanged', function(event) {
var iframe = event.currentSlide.getElementsByClassName('app-iframe')[0];
if (iframe) {
iframe.contentWindow.location.reload();
}
});
var toFill = document.querySelectorAll('[data-fill]');
[].forEach.call(toFill, function(el) {
var request = new XMLHttpRequest();
request.open('GET', el.getAttribute('data-fill'), true);
request.addEventListener('load', function() {
el.textContent = request.responseText;
});
request.send();
});
</script>
</body>
</html>