-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
727 lines (668 loc) · 25.3 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web Engineering with ES6</title>
<link rel="stylesheet" href="bower_components/reveal-js/css/reveal.min.css"/>
<link rel="stylesheet" href="bower_components/reveal-js/css/theme/night.css"/>
<link rel="stylesheet" href="bower_components/reveal-js/lib/css/zenburn.css"/>
<style>
.es5>code {
border: 1px solid indianred;
}
.es6>code {
border: 1px solid mediumseagreen;
}
.es7>code {
border: 1px solid orange;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="title">
<h2>Web Engineering with ES6</h2>
<h4>by Mihail Mikov</h4>
</section>
<section id="intro-es6">
<h3>What is ES6?</h3>
<ul>
<li>JavaScript language specification, released in June 2015</li>
<li>Incrementally implemented in browsers and other environments</li>
<li>Introduces a plethora of new features and improvements</li>
</ul>
</section>
<section id="es6-features">
<h3>Some of the new features in ES6</h3>
<ul>
<li>Arrow functions</li>
<li>Classes</li>
<li>Object literals syntax</li>
<li>Destructing</li>
<li>Default parameters</li>
<li>Array spreading</li>
<li>Variable declarations</li>
<li>Generators</li>
</ul>
<ul style="margin-left: 100px;">
<li>Promises</li>
<li>Iterators</li>
<li>Symbols</li>
<li>Number methods</li>
<li>Template strings</li>
<li>Sets</li>
<li>Maps</li>
<li>WeakMap</li>
</ul>
</section>
<section id="mind-blown">
<img src="images/mind-blown.jpeg" />
</section>
<section id="intro-engineering">
<h3>How does ES6 help engineering?</h3>
<ul>
<li>Readability</li>
<li>Quality</li>
<li>Abstraction</li>
<li>Evolution</li>
</ul>
</section>
<section id="readability-quote">
<h3>Readability</h3>
<div class="fragment fade-in">
<div class="fragment fade-out">IFYOUWANTYOURCODETOBEEASYTOWRITEMAKEITEASYTOREAD</div>
<div class="fragment">If you want your code to be easy to write, make it easy to read.
<br/><div style="text-align: right;">--Uncle Bob</div>
</div>
</div>
<aside class="notes">
fun fact: the romans wrote all caps with no spaces.
that is hard to read. it is even harder to correctly copy by hand.
The scribes of the middle ages found that introducing word gaps and punctuation helped reduce the number of error in copying.
Readability drives the way you think about your code.
</aside>
</section>
<section id="quality-quote">
<h3>Quality</h3>
<div class="fragment">Beware of bugs in the above code;
<br/>I have only proved it correct, not tried it.
<br/><div style="text-align: right"> --Donald Knuth</div></div>
</section>
<section id="abstraction-quote">
<h3>Abstraction</h3>
<div class="fragment">The art of programming is the art of organizing complexity
<br/><div style="text-align: right">--Edsger Dijkstra</div></div>
</section>
<section id="evolution-quote">
<h3>Evolution</h3>
<div class="fragment">“The best way to predict the future is to implement it.”
<br/><div style="text-align: right;">--David Hansson</div></div>
</section>
<section id="code-ahead">
<h4 style="margin-bottom:0">Attention! Code ahead!</h4>
<img src="images/jsatt.jpg" />
<h4>Attention! Code ahead!</h4>
</section>
<section id="let-const-declarations">
<h4>Variable declaration</h4>
<pre class="es5"><code class="javascript">
//es5
function incCounter() {
counter += five;
if (false) {
var five = 5;
}
//logical error! - the value of five is undefined
}
var counter = 0;
//this code "works" because of var hoisting
</code></pre>
<pre class="es6"><code class="javascript">
//es6
if (condition) {
let j = 12; //j exists only inside the if block
}
console.log(j); //throws an Error
const pi = { value: 3.14 }; //pi cannot be reassigned
pi = 3.14; //throws an Error
pi.value = 4; //works because the value itself is not immutable
const me = Object.freeze({ name: 'Mihail' }); //true constant
</code></pre>
</section>
<section id="why-let-const-declarations">
<h4>How do const and let help?</h4>
<ul>
<li>
Readability
<ul>
<li>Clear intent</li>
</ul>
</li>
<br/>
<li>
Quality
<ul>
<li>Block scope (let)</li>
<li>Immutable binding (const)</li>
</ul>
</li>
</ul>
</section>
<section id="arrow-functions">
<h4>Arrow functions</h4>
<pre class="es5"><code class="javascript">
//es5
[1, 2, 3, 4].map(function square(i) {
return i*i;
}); // [1, 4, 9, 16]
getNumbersAsync(function callback(a, b) {
return a + b;
});
fancyObject.coolMethod = function () {
var self = this;
this.elements.forEach(function (el) {
self.processElement(el); //different "this", must use self
});
}
</code></pre>
<pre class="es6"><code class="javascript">
//es6
[1, 2, 3, 4].map(i => i*i); // [1, 4, 9, 16]
getNumbersAsync((a, b) => a + b);
fancyObject.coolMethod = () => {
this.elements.forEach(el => {
this.processElement(el);
//logical error! - "this" doesn't point to fancyObject
});
}
</code></pre>
</section>
<section id="why-arrows-functions">
<h4>How do arrow functions help?</h4>
<ul>
<li>Readability
<ul>
<li>concise</li>
</ul>
</li>
<br/>
<li>Quality
<ul>
<li>use the outer "this"</li>
<li>no arguments meta parameter</li>
<li style="color: red">not a full substitute of functions</li>
</ul>
</li>
</ul>
</section>
<section id="classes">
<h4>Classes</h4>
<pre class="es5"><code class="javascript">
//es5
function Car(model, color) {
this.model = model;
this.color = color;
}
Car.prototype.startEngine = function () {
return "Started the engine of the " + this.model;
};
var myCar = new Car('Tesla', 'white');
</code></pre>
<pre class="es6"><code class="javascript">
//es6
class Car {
constructor(model, color) {
this.model = model;
this.color = color;
}
startEngine() {
return "Started the engine of the " + this.model;
}
}
let myCar = new Car('Tesla', 'white');
</code></pre>
</section>
<section id="why-classes">
<h4>How do classes help?</h4>
<ul>
<li>
Readability
<ul>
<li>Concise</li>
<li>All declarations stay together</li>
</ul>
</li>
<br/>
<li>
Quality
<ul>
<li>Methods on the prototype by default</li>
</ul>
</li>
</ul>
</section>
<section id="object-literal-syntax">
<h4>Object literals syntax</h4>
<pre class="es5"><code class="javascript">
//es5
var name = 'Mihail',
myObject = {
name: name,
getFriends: function () { ... }
};
myObject[getPropertyName()] = 'some value';
</code></pre>
<pre class="es6"><code class="javascript">
//es6
let name = 'Mihail',
myObject = {
name,
getFriends() { ... },
[getPropertyName()]: 'some value'
};
</code></pre>
</section>
<section id="why-object-literal-syntax">
<h4>How does object literals syntax help?</h4>
<ul>
<li>
Readability
<ul>
<li>Concise</li>
<li>All declarations stay together</li>
</ul>
</li>
</ul>
</section>
<section id="destructing">
<h4>Destructing</h4>
<pre class="es5"><code class="javascript">
//es5
var coordinates = [23, 14],
x = coordinates[0],
y = coordinates[1];
var personalInfo = getPersonalInfo(),
name = personalInfo.name,
age = personalInfo.age,
petName = personalInfo.pet.name;
</code></pre>
<pre class="es6"><code class="javascript">
//es6
let [x, y] = [23, 14];
let { name, age, pet: { name: petName } } = getPersonalInfo();
//with function parameters
function sayHi({ name }) {
console.log('Hi, ' + name);
}
sayHi(getPersonalInfo());
</code></pre>
</section>
<section id="why-destructing">
<h4>How does destructing help?</h4>
<ul>
<li>
Readability
<ul>
<li>Clear intent</li>
</ul>
</li>
<br/>
<li>
Evolution
<ul>
<li>Pattern Matching</li>
</ul>
</li>
</ul>
</section>
<section id="spreading">
<h4>Array spreading</h4>
<pre class="es5"><code class="javascript">
//es5
function variableNumberOfArguments() {
//hack to get the arguments as an array
var args = Array.prototype.splice.call(arguments, 0);
}
var numbers = [1, 2, 3],
letters = ['a', 'b', 'c'],
both = numbers.concat(letters);
</code></pre>
<pre class="es6"><code class="javascript">
//es6
function variableNumberOfArguments(...args) {
//args is already an array with all arguments
}
let numbers = [1, 2, 3],
letters = ['a', 'b', 'c'],
both = [...numbers, ...letters];
</code></pre>
</section>
<section id="why-spreading">
<h4>How does spreading help?</h4>
<ul>
<li>
Readability
<ul>
<li>Concise and composable</li>
</ul>
</li>
<br/>
<li>
Quality
<ul>
<li>Removes the need for the<br/>"arguments" meta parameter</li>
</ul>
</li>
</ul>
</section>
<section id="default-parameters">
<h4>Default parameters</h4>
<pre class="es5"><code class="javascript">
//es5
function makeCaption(text, style) {
text = text || 'default caption';
style = typeof style === 'object' ? style : { color: 'red' };
myH1.style.color = style.color;
myH1.text = text;
}
</code></pre>
<pre class="es6"><code class="javascript">
//es6
function makeCaption(text = 'default caption', { color } = { color: 'red' }) {
myH1.style.color = color;
myH1.innerHTML = text;
}
</code></pre>
</section>
<section id="why-default-parameters">
<h4>How do default parameters help?</h4>
<ul>
<li>
Readability
<ul>
<li>Concise</li>
<li>Clear intent</li>
</ul>
</li>
</ul>
</section>
<section id="string-templates">
<h4>Template strings</h4>
<pre class="es5"><code class="javascript">
//es5
"A " + car.color + " " + car.model + " just drove by"
</code></pre>
<pre class="es6"><code class="javascript">
//es6
`A ${car.color} ${car.model} just drove by`
</code></pre>
</section>
<section id="why-string-templates">
<h4>How do template strings help?</h4>
<ul>
<li>
Abstraction
<ul>
<li>Built-in template engine</li>
</ul>
</li>
<br/>
<li>
Evolution
<ul>
<li>Customizable through functions</li>
</ul>
</li>
</ul>
</section>
<section id="maps-and-sets">
<h4>Sets & Maps</h4>
<pre class="es6"><code class="javascript">
//es6
//Sets
var key = 'any type of value',
set = new Set();
set.add(key);
set.delete(key);
set.has(key) //returns true or false
//Maps
let key = { a: 1 },
value = { b: 2 },
map = new Map();
map.set(key, value);
map.delete(key);
map.get(key); //returns value
</code></pre>
</section>
<section id="why-maps-and-sets">
<h4>How do sets & maps help?</h4>
<ul>
<li>
Abstraction
<ul>
<li>Ability to relate any values types</li>
<li>No more abusing objects and arrays<br/> for sets and maps</li>
</ul>
</li>
<br/>
<li>
Quality
<ul>
<li>WeakMap and WeakSet help stop memory leaks</li>
</ul>
</li>
<br/>
<li>
Evolution
<ul>
<li>Built-in implementations improve performance</li>
</ul>
</li>
</ul>
</section>
<section id="promises">
<h4>Promises</h4>
<pre class="es6"><code class="javascript">
//es6
//how to create a promise
let getData = (url) => new Promise((resolve, reject) => {
ajax(url, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
//using the promise
getData('http://somesite.com/data')
.then(doSomethingClever)
.catch(logError);
</code></pre>
</section>
<section id="promises-continued">
<h4>Promises</h4>
<pre class="es6"><code class="javascript">
//es6
//chaining promises
getNumbersAsync()
.then((a,b) => a + b)
.then(checkSumWithServer)
.then(handleServerResponse)
.catch(logError);
//promise helper methods
Promise.resolve(value) //returns a "thenable" promise
Promise.reject(error) //returns a "catchable" promise
Promise.all([array, of, promises]) // waits for all
Promise.race([array, of, promises]) //the first to resolve wins
</code></pre>
</section>
<section id="why-promises">
<h4>How do promises help?</h4>
<ul>
<li>
Readability
<ul>
<li>Clear and natural API</li>
<li>Reduce callback hell</li>
</ul>
</li>
<br/>
<li>
Abstraction
<ul>
<li>Future value semantics</li>
<li>Invert the flow of control</li>
</ul>
</li>
<br/>
<li>
Evolution
<ul>
<li>Basis for implementing the async/await pattern</li>
</ul>
</li>
</ul>
</section>
<section id="generators">
<h4>Generators</h4>
<pre class="es6"><code class="javascript">
//es6
function* squaresMaker() {
let i = 1;
while (true) {
let step = yield i*i;
i += step || 1;
}
};
let squares = squaresMaker();
squares.next(); // { value: 1, done: false }
squares.next(); // { value: 4, done: false }
squares.next(); // { value: 9, done: false }
squares.next(2); // { value: 16, done: false }
//step = 2
squares.next(); // { value: 36, done: false }
</code></pre>
</section>
<section id="why-generators">
<h4>How do generators help?</h4>
<ul>
<li>
Abstraction
<ul>
<li>State machine inside a function</li>
<li>Two way message passing</li>
<li>Co-routines and flow of control</li>
</ul>
</li>
<br/>
<li>
Evolution
<ul>
<li>Basis for implementing the async/await pattern</li>
</ul>
</li>
</ul>
</section>
<section id="async-await">
<h4>Async/Await</h4>
<pre class="es7"><code class="javascript">
//es7
async function combinedQuery() {
let firstAnswer = await firstQuery(),
secondAnswer = await secondQuery(firstAnswer);
return secondAnswer;
}
//use inside another async function
async function useQueryAsync() {
let answer = await combinedQuery();
doSomethingClever(answer);
}
//or use the result as a promise
function useQuery() {
combinedQuery().then(doSomethingClever);
}
</code></pre>
</section>
<section id="annotations">
<h4>Annotations</h4>
<pre class="es7"><code class="javascript">
//es7
let myObject = {
//built-in annotation
@readonly
myConstant: 42
};
myObject.constant = 7; //throws an error
//custom annotation
@authorized
function getData() { ... }
//annotation definitions are just functions
function authorized() { ... }
</code></pre>
</section>
<section id="http2">
<h4>HTTP/2</h4>
<ul>
<li>faster and smarter</li>
<li>two way communication</li>
<li>breaks many hacks around http 1.1</li>
</ul>
</section>
<section id="webassembly">
<h4>WebAssembly</h4>
<ul>
<li>A byte-code for the web</li>
<li>Decouples the language from the runtime</li>
<li>Compiled from many languages</li>
</ul>
</section>
<section id="what-we-skipped">
<h4>Future Topics</h4>
<ul>
<li>Using ES6 today through the Babel transpiler</li>
<li>Testing and monitoring tools</li>
<li>Web APIs: canvas, webGL, audio and workers</li>
<li>Modern JavaScript libraries, frameworks and design patterns</li>
</ul>
</section>
<section id="links">
<h4>Links!</h4>
<ul>
<li><a href="https://github.com/debel/Web-Engineering-With-ES6-Presentation">The code of this presentation</a></li>
<li><a href="http://mmikoff.net/slides/aubg_js/">Intro to server side JavaScript (previous presentation)</a></li>
<li><a href="https://github.com/getify/You-Dont-Know-JS">You don't know JavaScript book series</a></li>
<li><a href="http://exploringjs.com/">Exploring ES6</a></li>
<li><a href="https://addyosmani.com/resources/essentialjsdesignpatterns/book/">JavaScript Design Patterns</a></li>
<li><a href="https://github.com/tc39">JavaScript language development committee</a></li>
<li><a href="https://babeljs.io/">Babel transpiler</a></li>
</ul>
</section>
<section id="thanks">
<h4>Thank you!</h4>
<h3>Questions? Comments?</h3>
</section>
</div>
</div>
<script src="bower_components/reveal-js/lib/js/head.min.js"></script>
<script src="bower_components/reveal-js/js/reveal.min.js"></script>
<script>
Reveal.initialize({
transition: 'fade',
transitionSpeed: 'fast',
controls: false,
history: true,
dependencies: [
{
src: 'bower_components/reveal-js/plugin/highlight/highlight.js',
callback: function () { hljs.initHighlightingOnLoad(); }
},
{
src: 'bower_components/reveal-js/plugin/notes/notes.js'
}
]
});
</script>
</body>
</html>