-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
645 lines (508 loc) · 16 KB
/
index.html
File metadata and controls
645 lines (508 loc) · 16 KB
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>ES2015+</title>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/solarized.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="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 ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Intro to Javascript ES2015+</h1>
</section>
<section>
<h2>Goals</h2>
<p>💥 Introduce you to a useful subset of features</p>
<p>💥 Ease you into some new syntax</p>
</section>
<section>
<h2>FM Javascript Quick Reference ES2015+</h2>
<small>https://github.com/fostermadeco/standards/</small>
</section>
<section>
<h2>JS Versions</h2>
<p>ECMAScript is the scripting language that forms the basis of JavaScript.</p>
<p>When you see ES6, it means that that version of JavaScript is following the specifications in the sixth edition of ECMAScript</p>
</section>
<section>
<h2>A version is a set of features</h2>
<p>💥 ES6 === ES2015</p>
<p>💥 Going forward, ECMAScript will be updated annually</p>
<p>💥 ES2015, ES2017, ES2018, ESNext</p>
</section>
<section>
<h2>Compiling: Babel</h2>
<p>Converts ES2015+ code into backwards compatibility js that can be read by current and older browsers</p>
<p>💥 Syntax</p>
<p>💥 Polyfills for built-ins</p>
</section>
<section data-markdown><textarea data-template>
## Syntax vs. Built-ins
Syntax: `let` & `const`, arrow functions, `class`
```javascript
const cat = 'Sylvie';
// Babel compiles to
var cat = 'Sylvie';
```
Built-ins (methods and objects):
```javascript
const cats = ['Sylvie', 'Blixa', 'Max', 'Ramona'];
// String.prototype.startsWith - ES2015
cats[0].startsWith('Sy'); // true
// Array.prototype.find - ES2015
cats.find(cat => cat === 'Sylvie'); // Sylvie
// Array.prototype.includes - ES2016
cats.includes('Sylvie'); // true
```
</textarea></section>
<section>
<h2>Mix</h2>
<small>Wrapper around Webpack (uses Babel)</small>
<p>With our Mix setup you can safely use ES2015 and ES2017</p>
<small>💥 Mix uses Babel to compile ES2015+ syntax to vanilla JS.</small>
<small>💥 Our Mix setup also adds polyfills to support <em>used</em> ES2015+ built-ins for older browsers like IE11.</small>
</section>
<section data-markdown><textarea data-template>
<h2>Eslint & Prettier</h2>
<a href="https://github.com/fostermadeco/eslint-config-fostermade">
<img src="http://kellypackercom.s3.amazonaws.com/pages/eslint-prettier.png" />
</a>
</textarea></section>
<section data-markdown><textarea data-template>
## Misc notes
Strict mode included
```javascript
"use strict";
```
Unnecessary if file is included before closing body tag
```javascript
$(document).ready({});
```
</textarea></section>
<section data-markdown><textarea data-template>
<h2>ES2015+ Features</h2>
</textarea></section>
<section data-markdown><textarea data-template>
## Variable Declarations
<small>ES2015 introduced two new ways to declare</small>
<p>var</p>
<p>let</p>
<p>const</p>
</textarea></section>
<section data-markdown><textarea data-template>
## `var`
not block scoped
```javascript
var x = 1;
if (x === 1) {
var x = 2;
console.log(x);
// expected output: 2
}
console.log(x);
// expected output: 2
```
**When to use:**
<small>You don't need to change old code, but I wouldn't suggest using `var` on new code.</small>
</textarea></section>
<section data-markdown><textarea data-template>
## `let`
1) block scope variable 2) value can change
```javascript
let x = 1;
x = 2;
if (x === 1) {
let x = 3;
console.log(x);
// expected output: 3
}
console.log(x);
// expected output:2
```
**When to use**
<small>When the variable will be reassigned a new value.</small>
</textarea></section>
<section data-markdown><textarea data-template>
## `const`
block scope variable that can't change
```javascript
const number = 42;
try {
number = 99;
} catch(err) {
console.log(err);
// expected output: TypeError: invalid assignment to const `number'
// Note - error messages will vary depending on browser
}
console.log(number);
// expected output: 42
```
</textarea></section>
<section data-markdown><textarea data-template>
## Template Literals
```javascript
const first = 'Kelly';
const last = 'Packer';
// old
const intro = 'My name is ' + first + ' ' + last + '!';
// template literal
const intro2 = `My name is ${first} ${last}!`;
// multi-line template literal
const headerTemplate = `
<div>
<h1>Hi, my name is ${first} ${last}!</h1>
</div>
`;
```
</textarea></section>
<section data-markdown><textarea data-template>
## Iterables/Loops
`for`
Old, difficult syntax 🛑:
```javascript
const names = ['Sylvie', 'Max', 'Blixa', 'Ramona'];
for (let i = 0; i < names.length; ++i) {
const name = names[i];
}
```
</textarea></section>
<section data-markdown><textarea data-template>
`forEach()`
<small>💥 For arrays. 😢 Can't break out of it.</small>
```javascript
const names = ['Sylvie', 'Max', 'Blixa', 'Ramona'];
names.forEach(function(name, index) {
console.log(name, index);
});
```
</textarea></section>
<section data-markdown><textarea data-template>
`for/in`
<small>For objects, but can be used with arrays (as long as order isn't important).</small>
```javascript
const cat = {name: 'Sylvie', type: 'calico', gender: 'female'};
for (const key in cat) {
const value = cat[key];
// name Sylvie
console.log(key, value);
}
```
</textarea></section>
<section data-markdown><textarea data-template>
`for/of`
<p>💥 Very versatile.</p>
<p>💥 Can be used on anything iterable: array, string, object (ES2017), dom collection (node list), arguments, string, map or set.</p>
</textarea></section>
<section data-markdown><textarea data-template>
`for/of`
<small>With an array:</small>
```javascript
const names = ['Sylvie', 'Max', 'Blixa', 'Ramona'];
for (const [index, item] of names.entries()) {
// 0 Sylvie
console.log(index, item);
}
```
<small>💥 `array.entries` returns a new Array iterator object</small>
<small>💥 Uses destructuring</small>
</textarea></section>
<section data-markdown><textarea data-template>
`for/of`
<small>With an object:</small>
```javascript
const cat = { name: 'Sylvie', type: 'calico', gender: 'female' };
for (const [key, value] of Object.entries(cat)) {
// name Sylvie
console.log(key, value);
}
```
<small>💥 `Object.entries(obj)` returns array of [key, value]</small>
<small>💥 Uses destructuring</small>
</textarea></section>
<section data-markdown>
<textarea data-template>
## Object and Array Destructuring
Say you have an object that you want to use some properties from.
```javascript
const person = {
firstName: 'Adam',
lastName: 'Tsai',
jobTitle: 'Web developer',
};
```
You might write something like this.
```javascript
const message = `Hello, ${person.firstName} ${person.lastName}.`;
```
Or you might write it like this.
```javascript
const firstName = person.firstName;
const lastName = person.lastName;
const message = `Hello, ${firstName} ${lastName}.`;
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
#### Object and Array Destructuring
With destructuring
```javascript
const { firstName, lastName } = person;
const message = `Hello, ${firstName} ${lastName}.`;
```
Works with arrays too
```javascript
const people = [
'Jon Snow',
'Daenerys Targaryen',
'Tyrion Lannister'
];
const [ jon, dany, tyrion ] = people;
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
#### Object and Array Destructuring
You can also alias your variables
```javascript
const { firstName: first, lastName: last } = person;
const message = `Hello, ${first} ${last}.`;
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Arrow Functions
(AKA "fat arrow" syntax)
A more concise syntax for writing function expressions.
Pre-ES2015 Function Expression
```javascript
var addTwoNumbers = function (a, b) {
return a + b;
}
```
ES2015
```javascript
const addTwoNumbers = (a, b) => {
return a + b;
}
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
#### Arrow Functions
If your function is just a single expression, then this can also be writen with an implicit return
```javascript
const addTwoNumbers = (a, b) => (
a + b
);
```
or just
```javascript
const addTwoNumbers = (a, b) => a + b;
```
If your function has just a single parameter, you can omit the parentheses:
```javascript
const multiplyBy5 = a => a * 5;
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
#### Arrow Functions
Not meant to 100% replace `function`. The fat arrow changes the way `this` is bound.
```javascript
$('input[name="first_name"]').on('change', function () {
var inputValue = $(this).val();
alert(inputValue);
});
$('input[name="first_name"]').on('change', () => {
var inputValue = $(this).val(); // This will result in an error
alert(inputValue);
});
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Default Function Parameters
Old
```javascript
function addTwoNumbers(a, b) {
if (typeof b === 'undefined') {
b = 0;
}
return a + b;
}
addTwoNumbers(5);
```
New
```javascript
function addTwoNumbers(a, b = 0) {
return a + b;
}
addTwoNumbers(5);
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Object Literal property value shorthand
<small>If you are assigning to an object property from a variable of the same name, you can use this shorthand.</small>
```javascript
const goat = 1;
const pig = 2;
const bear = 3;
// OLD
const combined = { goat: goat, pig: pig, bear: bear };
// NEW
const combined = { goat, pig, bear };
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
<h2>Fin</h2>
<h4>Questions?</h4>
<small><a href="https://kangax.github.io/compat-table/es2016plus/">ECMAScript compatibility table</a></s>
</textarea>
</section>
<section data-markdown><textarea data-template>
## Extra Credit
New String & Array methods
```javascript
const cats = ['Sylvie', 'Blixa', 'Max', 'Ramona'];
// String.prototype.startsWith - ES2015
cats[0].startsWith('Sy'); // true
// .endsWith, .includes
// Array.prototype.find - ES2015
cats.find(cat => cat === 'Sylvie'); // Sylvie
// Array.prototype.includes - ES2016
cats.includes('Sylvie'); // true
Array.prototype.filter
Array.prototype.map
Array.prototype.some
Array.prototype.every
```
</textarea></section>
<!--
<section data-markdown>
<textarea data-template>
## Spread Operator
Official definition on MDN docs:
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
???
</textarea>
</section>
<section data-markdown>
<textarea data-template>
#### Spread Operator - Arrays
Concatenating (Old)
```
const cats = ['Mittens', 'Bootsy, 'Miss Cinderella'];
const dogs = ['Rover', 'Bud', 'Gromit'];
const chaos = cats.concat(dogs);
```
Concatenating (New)
```
const cats = ['Mittens', 'Bootsy, 'Miss Cinderella'];
const dogs = ['Rover', 'Bud', 'Gromit'];
const chaos = [...cats, ...dogs];
```
Cloning:
```
const cats = ['Mittens', 'Bootsy, 'Miss Cinderella'];
const copyOfCats = [...cats];
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
#### Spread Operator - Objects
Merging (Old)
```
const girlfriendsCat = {
name: 'Zy',
fat: true,
lazy: true,
limbs: 'natural'
};
const bionicLegs = { limbs: 'bionic' };
const upgradedCat = Object.assign({}, girlfriendsCat, bionicLegs);
console.log(upgradedCat) // { name: 'Zy' fat: true, lazy: true, limbs: 'bionic' }
```
Merging (New)
```
const girlfriendsCat = {
name: 'Zy',
fat: true,
lazy: true,
limbs: 'natural'
};
const bionicLegs = { limbs: 'bionic' };
const upgradedCat = { ...girlfriendsCat, ...bionicLegs };
console.log(upgradedCat) // { name: 'Zy' fat: true, lazy: true, limbs: 'bionic' }
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
#### Spread Operator - Function calls
```
const someNumbers = [9, 4, 7, 1];
const min = Math.min(...someNumbers);
console.log(min); // 1
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
## Rest Operator
The rest parameter syntax allows us to represent an indefinite number of arguments as an array.
```
function sumAll(...numbers) { // args is the name for the array
let sum = 0;
numbers.forEach(number => (sum += number));
return sum;
}
console.log(sumAll(1)); // 1
console.log(sumAll(1, 2)); // 3
console.log(sumAll(1, 2, 3)); // 6
```
</textarea>
</section>
-->
</div>
</div>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true }
]
});
</script>
</body>
</html>