-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1340 lines (1160 loc) · 61.6 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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Moving to ES6 Workshop</title>
<meta name="description" content="Materials and slides for a Women Who Code ES6 JavaScript workshop.">
<meta name="author" content="Liz Shaw">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="dist/css/reveal.css">
<link rel="stylesheet" href="dist/css/wwc.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor --><link rel="stylesheet" href="dist/css/light.css">
<!-- dark editor <link rel="stylesheet" href="dist/css/dark.css">-->
<!-- <link rel="stylesheet" href="dist/css/zenburn.css"> -->
<link rel="stylesheet" href="plugin/accessibility-helper/css/accessibility-helper.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'dist/css/print.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="dist/css/print.css" type="text/css" media="print">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<!--[if lt IE 9]>
<script src="dist/js/html5shiv.js"></script>
<![endif]-->
</head>
<body spellcheck="false">
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Moving to ES6</h1>
<img style="margin:1em 0;" src="dist/img/women-who-code-logo.jpeg" alt="Women Who Code Logo" />
<h2>Welcome!</h2>
</section>
<section class="hide-pdf">
<h2>Welcome!</h2>
<p class="green">Some "rules"</p>
<ul>
<li>We are here for you!</li>
<li>Every question is important</li>
<li>Help each other</li>
<li>Have fun</li>
</ul>
</section>
<section class="activity small" data-background="#007a7c">
<h2>Our Workshop Activity</h2>
<h3>A 100% JavaScript Shopping Cart (featuring emoji 😉)</h3>
<div style="min-height:602px;position:relative;overflow:hidden;">
<p data-height="602" data-theme-id="0" data-slug-hash="xdGNZg" data-default-tab="result" data-user="anythingcodes" data-embed-version="2" data-pen-title="WWC ES6 Activity - Solution" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/xdGNZg/">WWC ES6 Activity - Solution</a> by Liz Shaw (<a href="https://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="https://codepen.io">CodePen</a>.</p>
</div>
</section>
<section data-background="#029393">
<h1 style="color: #fafafa;">Background</h1>
</section>
<section>
<h2>What is ES6?</h2>
<div class="small clear">
<div class="halfblock">
<p class="blue underlined">📄 Standard:</p>
<ul>
<li><span class="green">ECMAScript</span>, abbreviated <span class="green">ES</span></li>
<li class="fragment">Describes features, syntax, and behavior of all <span class="blue">implementations</span></li>
<li class="fragment">Think of it as a ruleset or blueprint</li>
</ul>
<p class="blue underlined"><br />✍ Implementation:</p>
<ul>
<li><span class="green">JavaScript</span>, the implementation of the <span class="green">ECMAScript</span> standard</li>
<li class="fragment">Implementations <span class="blue">track</span> standards</li>
</ul>
</div>
<div class="halfblock">
<img src="dist/img/flowchart-es-js.svg" alt="ES6's relationship to JavaScript" />
<div class="fragment-progression">
<div class="fragment">
<img src="dist/img/house1.png" alt="The standard, ECMAScript, is the ruleset, or blueprint" />
</div>
<div class="fragment">
<img src="dist/img/house2.png" alt="The implementation, JavaScript, is the language that uses that ruleset's plan, or built house/product" />
</div>
</div>
<p class="fragment" style="margin-top:15px;">
📝 We'll implement the new features from version <span class="orange">6 of ECMAScript</span> using <span class="orange">JavaScript</span>
</p>
</div>
</div>
<aside class="notes">
<ul>
<li>ECMAScript and ES terms are interchangeable</li>
<li>There are plenty of other implementations of ECMAScript, including ActionScript</li>
</ul>
</aside>
</section>
<section>
<h2>What ES6 can do for you</h2>
<ol>
<li class="fragment">Fix scoping problems
</li>
<li class="fragment">Write cleaner, easier-to-understand code</li>
<li class="fragment">New functionality for working with arrays and objects</li>
<li class="fragment">A built-in helper for making AJAX requests</li>
<li class="fragment">Encourage the ✨powers✨ of object-oriented programming</li>
</ol>
</section>
<section data-background="#029393">
<h1 style="color: #fafafa;"><span class="ordinal">( 1 )</span>Fix scoping problems</h1>
<aside class="notes">Can someone tell me what scope means in the context of programming in general?</aside>
</section>
<section>
<h2>Scopes 🔬</h2>
<div class="clear">
<div class="halfblock halfblock-reverse">
<ol class="small fragment">
<li>
<strong class="">Function Scopes: YES</strong>
<p>Variables defined inside of a function are <span class="blue">not accessible</span> from the outside scope</p>
<pre class="fragment"><code contenteditable class="javascript"> function myFunctionScope() {
var a = 'alpha';
return a;
}</code></pre>
<pre class="fragment"><code contenteditable class="javascript"> console.log(a); // undefined
</code></pre>
</li>
<li class="fragment">
<strong class="">Block Scopes (a.k.a. lexical scopes): NO</strong>
<p>Any variables declared within <code>{ }</code> are <span class="blue">accessible only within the <code>{ }</code></span></p>
<pre><code contenteditable class="javascript"> var myBlockScope = true;
if (myBlockScope) {
var z = 'zeta';
}</code></pre>
<pre class="fragment"><code contenteditable class="javascript"> console.log(z); // 'zeta'
</code></pre>
</li>
</ol>
</div>
<div class="halfblock halfblock-reverse">
<div class="fragment" style="text-align: center;">
<p>
<img class="" src="dist/img/wait-what.gif" alt="Wait what?" />
</p>
<p>What is going on here?!</p>
</div>
<p class="fragment" style="margin-top:1em;"><code>var</code> <span class="blue">ignores block scope</span> and only respects function scope</p>
</div>
</div>
<div class="fragment small">
<p><a href="https://codepen.io/anythingcodes/pen/ooxdPG?editors=0010" target="_blank">CodePen Example »</a></p>
<p class="info"><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch3.md" target="_blank">Article: Function vs. Block Scope »</a></p>
</div>
<aside class="notes">
Most languages use block scopes. This made programming in JavaScript unintuitive and inconsistent for many who were used to block scoping.
<ul>
<li>What would the first console.log output? firstVariable is not defined</li>
<li>What would the second console.log output? zeta</li>
<li>Ask for block scope examples, e.g. <code>{ }</code>, <code>if () { }</code>, <code>for() {}</code>, <code>while() { }</code>, etc.</li><li></li>
<li>A <strong>function</strong> cordons off an area. Plain old <code>{}</code> does not. Any variables defined in a function remain scoped there, and aren't accessible outside.</li>
<li>When you see the word <strong>function</strong>, it should trigger the fact that a new scope/context has been created. Any variables defined there are scoped to that area.</li>
</ul>
</aside>
</section>
<section>
<h2>Why is this a problem?</h2>
<p class="fragment">
Unpredictable and hard to control
</p>
<p class="fragment"><a href="https://codepen.io/anythingcodes/pen/LyZwRr?editors=1010" target="_blank">CodePen Example »</a></p>
<aside class="notes">
A common interview question. Essentially happening because there's only one <code>i</code> in the outer scope that was closed over, instead of a new <code>i</code> for each iteration's function to close over.
</aside>
</section>
<section>
<h2>How does ES6 fix block scoping?</h2>
<p class="fragment">Use <code>let</code> and <code>const</code> instead of <code>var</code></p>
<ul>
<li class="fragment"><code>const</code> is for constants</li>
<li class="fragment"><code>let</code> is for variables whose value will change (<a href="https://codepen.io/anythingcodes/pen/WjxBLy/" target="_blank" class="small">Example »</a>)</li>
<li class="fragment">Use <code>const</code> by default, but change to <code>let</code> when you need to change the value</li>
</ul>
<p class="fragment"><a href="https://codepen.io/anythingcodes/pen/LyZwRr?editors=1010" target="_blank">CodePen Example »</a></p>
</section>
<section>
<h2>Do you still need <code>var</code>?</h2>
<p class="fragment">No 😊</p>
<p class="fragment"><span class="orange">Only one potential case:</span> If you have code that should be available from the global object (<code>window</code> in browsers), e.g. if you need to access code across frames or windows</p>
<p class="fragment">👆 This is rare and should generally be avoided</p>
<aside class="notes">
Reality is that ES6 has brought much-needed functionality to the table. Block-scoping is a game-changer. Huge benefit to const/let is that they do not pollute your global scope (<code>window</code> in browsers). Much safer.
</aside>
</section>
<section>
<h2>How does ES6 help with function scope issues?</h2>
<ul>
<li class="fragment">With <span class="blue">arrow functions</span>: <code>=></code></li>
<li class="fragment">Usually used to fix issues with the <code>this</code> keyword</p>
</li>
</ul>
<div class="clear fragment" style="margin-top: 0.5em;">
<div class="halfblock">
<h3 class="green">ES5</h3>
<pre><code contenteditable class ="javascript">var getMessage = function(name) {
return 'Hello ' + name;
}</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6</h3>
<pre><code contenteditable class ="javascript">const getMessage = name => 'Hello ' + name;</code></pre>
</div>
</div>
<div class="clear small">
<div class="halfblock">
<h3 class="fragment">Making an arrow function:</h3>
<ol>
<li class="fragment">Remove the word <code>function</code></li>
<li class="fragment">Place a fat arrow (<code>=></code>) after parameters</li>
</ol>
</div>
<div class="halfblock">
<h3 class="blue fragment">Trimming it down:</h3>
<ul>
<li class="fragment">If there's only <span class="green">one parameter</span>, remove the surrounding parentheses <code>()</code></li>
<li class="fragment">If there's only <span class="green">one expression</span> in the function body, remove <code>{ }</code> and <code>return</code> (<span class="orange">implicit return</span>)</li>
</ul>
</div>
</div>
<aside class="notes">
<ul>
<li>Refactor ES5 version in front of students. Ask about <code>const</code> and template strings</li>
<li>Skinny arrow is <code>-></code> like in Ruby and Coffeescript</li>
<li>If there are zero or more than one args, you need to include parentheses, e.g. <code>const sum = () => retVal;</code></li>
</ul>
</aside>
</section>
<section>
<h2>The <code>this</code> keyword</h2>
<p>The <code>this</code> keyword determines context</p>
<pre class="small"><code class="javascript" contenteditable>const obj = {
name: 'Zeta',
getName: function() {
console.log(this.name);
}
};
obj.getName(); // Zeta</code></pre>
<aside class="notes">
Perhaps mention concise object methods, if it makes sense.
</aside>
</section>
<section>
<h2>There's <code>this</code> One Problem</h2>
<p class="fragment">Binding and changing <code>this</code> in functions is one of the most common JavaScript errors</p>
<div class="fragment clear" style="margin-top:.5em;">
<p><img src="dist/img/computer-frustration.gif" alt="Computer frustration" class="right" />It's possible to mistakenly affect one object when you meant to affect another</p>
<pre class="fragment" style="display: inline-block;"><code class="xml">var self = this;</code></pre>
<pre class="fragment" style="display: inline-block;"><code class="xml">var _this = this;</code></pre>
<pre class="fragment" style="display: inline-block;"><code class="xml">var that = this;</code></pre>
<p class="fragment">🤦</p>
</div>
<!--<p class="fragment"><a href="https://codepen.io/anythingcodes/pen/BRvYQp/?editors=0010" target="_blank">Let's take a look »</a></p>-->
<aside class="notes">This is straightforward with a small codebase but it quickly spirals out of control with nested functions.</aside>
</section>
<section>
<h2>What is <code>this</code> problem?</h2>
<h3>Let's take a look</h3>
<div style="min-height:300px;" class="fragment">
<p data-height="300" data-theme-id="0" data-slug-hash="dZMqQZ" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Arrow functions and `this`" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/dZMqQZ/">Arrow functions and `this`</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<!-- <a class="solution solution__lesson-2" href="https://codepen.io/anythingcodes/pen/QvqReo?editors=0010#0" target="_blank">View Solution</a> -->
</div>
<aside class="notes">
<ul>
<li>Ask students: What would you expect to happen?</li>
<li>Ask students: What are some ways around the problem that you've seen?</li>
<li>Refer to it as 'the this keyword' and then 'the keyword' going forward</li>
<li>The call to this.logEvent() is broken because <code>this</code> is a reference to the object that was the target of the event (<code>document</code>)</li>
<li>Ask students to transform existing code to arrow function per earlier steps
<ul>
<li>Can fix by binding <code>this</code> to logEvent(), but this creates a new function whose <code>this</code> is bound to the current <code>this</code>.</li>
<li>Could also declare <code>var self = this;</code></li>
</ul>
</li>
</ul>
</aside>
</section>
<section>
<h2>Can arrow functions help?</h2>
<ul>
<li class="fragment">Arrow functions <span class="green">do not have a <code>this</code> 🌟</li>
<li class="fragment">The value of <code>this</code> is <span class="orange">lexically fetched</span> from the scope the arrow function sits inside</li>
</ul>
<div style="min-height:200px;" class="fragment">
<p data-height="200" data-theme-id="0" data-slug-hash="qmPGJN" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Arrow functions and `this`" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/qmPGJN/">Arrow functions and `this`</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<!-- <a class="solution solution__lesson-2" href="https://codepen.io/anythingcodes/pen/QvqReo?editors=0010#0" target="_blank">View Solution</a> -->
</div>
<aside class="notes">
<ul>
<li>Arrow functions are a better way to fix the issue! ES6's purpose is to solve this problem and stop worrying about context.</li>
</ul>
</aside>
</section>
<section data-background="#029393">
<h1 style="color: #fafafa;"><span class="ordinal">( 2 )</span>Write cleaner, easier-to-understand code</h1>
</section>
<section>
<h2>Template Strings</h2>
<p>Allow multi-line strings</p>
<p>Great for use with HTML <span class="orange">templates</span></p>
<div class="clear">
<div class="halfblock fragment">
<h3 class="green underlined">ES5:</h3>
<pre><code class="xml">var html = '<div>' +
'<h1>Hello ' + str + '!</h1>' +
'</div>';</code></pre>
<p class="right small fragment">becomes</p>
</div>
<div class="halfblock fragment">
<h3 class="green underlined">ES6:</h3>
<pre><code class="xml">const html = `<div>
<h1>Hello ${str}!</h1>
</div>`;</code></pre>
</div>
</div>
<aside class="notes">Ask who's used the ES5 version and gotten confused about plus signs, quotation marks, etc. when adding variables</aside>
</section>
<section>
<h2>Using Template Strings</h2>
<pre><code class="javascript">const message = `User ${name} scored ${score} on the exam`;</code></pre>
<p> </p>
<ul>
<li>Use backticks (<code>` `</code>) around the string</li>
<li class="fragment">Use <code>${ }</code> around any <span class="orange">variable</span> or <span class="orange">expression</span></li>
<li class="fragment">Work well with <span class="green">ternary operators</span>, for example<br /><code class="javascript">${ userIsYoungerThan21 ? serveGrapeJuice() : serveWine() }</code></li>
</ul>
<p> </p>
<pre class="fragment small"><code class="javascript">const message = `User ${name} ${score >= 60 ? 'passed' : 'failed'} the exam`;</code></pre>
<aside class="notes">
Can't use <code>if</code> because that's a statement, not an expression. Ternary operators are expressions.
</aside>
</section>
<!--<section>
<h2>Concise object methods</h2>
<p class="fragment">Object methods no longer need <code>: function</code></p>
<div class="clear">
<div class="halfblock fragment">
<h3 class="green underlined">ES5:</h3>
<pre><code class="javascript" contenteditable>var dog = {
name: 'Boo',
bark: function() {
console.log('yip!');
}
};
dog.bark(); // yip!</code></pre>
<p class="right small fragment">becomes</p>
</div>
<div class="halfblock fragment">
<h3 class="green underlined">ES6:</h3>
<pre><code class="javascript">const dog = {
name: 'Boo',
bark() {
console.log('yip!');
}
};
dog.bark(); // yip!</code></pre>
</div>
</div>
<aside class="notes">Reformat ES5 version in front of students</aside>
</section>-->
<section>
<h2>Default Parameter Values</h2>
<p class="fragment">Using ES5, we could establish default parameter values, but there were some flaws<br /><a href="https://codepen.io/anythingcodes/pen/LyKZRX?editors=0010" target="_blank">View CodePen »</a></p>
<p></p>
<div class="clear fragment" style="margin-top:.25em">
<div class="halfblock">
<h3 class="green underlined">ES5:</h3>
<pre style="font-size:76%;"><code class="javascript" contenteditable>function makeRequest(url, timeout, callback) {
timeout = timeout || 2000;
// what if timeout is 0?
callback = callback || function() {};
callback();
}</code></pre>
<p class="fragment">becomes</p>
<pre style="font-size:76%;" class="fragment"><code class="javascript" contenteditable>function makeRequest(url, timeout, callback) {
timeout = (typeof timeout !== 'undefined') ? timeout : 2000;
callback = (typeof callback !== 'undefined') ? callback || function() {};
callback();
}</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green underlined">ES6:</h3>
<pre style="font-size:76%;"><code class="javascript" contenteditable>function makeRequest(url, timeout = 2000, callback = () => {}) {
callback();
}</code></pre>
<div class="small fragment" style="margin-top:.5em;">
<img src="dist/img/its-beautiful.gif" alt="It's beautiful" />
<p><em>It's beautiful</em></p>
</div>
</div>
</div>
<aside class="notes">
<ul>
<li>The logical OR operator (<code>||</code>) always returns the second operand when the first is falsy.</li>
<li>Ask Class: What happens if timeout is 0? </li>
<li>The problem with the first ES5 makeRequest() is that 0 — a valid timeout — would be considered falsy and would then change to 2000 instead of 0. Same for the function — if no callback is passed in, it's undefined. So we have to account for undefined specifically and then assign values.</li>
<li>Too much code! ES6 can handle this error-checking for us.</li>
<li>makeRequest() expects only the <code>url</code>, the first parameter, to always be passed, which is why there's no default</li>
<li>Mention that equal signs are commonly used throughout ES6 to denote default values --> FOR ACTIVITY</li>
</ul>
</aside>
</section>
<section>
<h2>Calling functions with default parameters</h2>
<p>Given the following function:</p>
<pre class="small"><code class="javascript" contenteditable>function makeRequest(url, timeout = 2000, callback = () => {}) {
callback();
}</code></pre>
<div class="fragment">
<p>Only <code>url</code> is required. The following calls all work:</p>
<pre class="small"><code class="javascript">makeRequest('/api');
makeRequest('/api', 500);
makeRequest('/api', 0, requestData => updateDatabase(requestData));
makeRequest('/api', undefined, requestData => updateDatabase(requestData));</code></pre>
</div>
<aside class="notes">
Ask students: What would the timeout value be? Point out that you can pass in <code>undefined</code>. However, <code>null</code> is a valid value so the function will use <code>null</code> instead of the default parameter.
</aside>
</section>
<!--<section>
<h2>Property Initializer Shorthand</h2>
<p class="fragment">When an object property name is the same as the local variable name, you can include the name without a colon and value</p>
<div class="clear small">
<div class="halfblock fragment">
<h3 class="green underlined">ES5:</h3>
<pre><code class="javascript">function saveData(url, data){
$.ajax({ method: 'POST', url: url, data: data });
}</code></pre>
<p class="right small fragment">becomes</p>
</div>
<div class="halfblock fragment">
<h3 class="green underlined">ES6:</h3>
<pre><code class="javascript">function saveData(url, data){
$.ajax({ url, data, method: 'POST' });
}</code></pre>
</div>
</div>
<p class="fragment">Sometimes referred to as <span class="blue">enhanced literal notation</span></p>
<aside class="notes">Convention is to place shorthanded properties at beginning for readability. This example uses jQuery, but you don't need to know it; it's just an example.</aside>
</section>-->
<section data-background="#029393">
<h1 style="color: #fafafa;"><span class="ordinal">( 3 )</span>New functionality for working with arrays and objects</h1>
</section>
<section>
<h2>Destructuring Objects</h2>
<p>Unpack properties from objects into distinct variables</p>
<div class="clear small">
<div class="halfblock fragment">
<h3 class="green underlined">ES5:</h3>
<pre><code class="javascript">var person = {
name: 'Whitney',
age: 38
};
var name = person.name;
var age = person.age;
console.log(name, age); // Whitney 38</code></pre>
<p class="right small fragment">becomes</p>
</div>
<div class="halfblock fragment">
<h3 class="green underlined">ES6:</h3>
<pre><code class="javascript">const person = {
name: 'Whitney',
age: 38
};
const { name, age } = person;
console.log(name, age); // Whitney 38</code></pre>
</div>
</div>
<ul class="">
<li class="fragment"><span class="green">Left-hand side of the equal sign (<code>=</code>)</span><br />A comma-separated list of the object's keys you'd like to unpack, wrapped in <code>{ }</code></li>
<li class="fragment"><span class="green">Right-hand side of the equal sign (<code>=</code>)</span><br />The object you're destructuring</li>
</ul>
</section>
<!--<section>
<h2>Assigning to Different Local Variable Names</h2>
<p class="fragment">How to use a different name for your destructured variables</p>
<div class="fragment-progression">
<pre class="fragment"><code class="javascript">const { name, age } = person;</code></pre>
<pre class="fragment"><code class="javascript">const { name, age } = person;
const localName = name;
// localName is the variable name I want to use going forward</code></pre>
</div>
<p> </p>
<ol class="clear small">
<li class="fragment">Use a <code>:</code> in the comma-separated list of keys</li>
<li class="fragment">The <span class="green">variable name after <code>:</code></span> can now be referenced as a local variable</li>
</ol>
<p> </p>
<pre class="fragment"><code class="javascript">const { name : localName, age } = person;
// now I can reference localName going forward!</code></pre>
</section>-->
<section>
<h2>Destructuring Arrays</h2>
<p>Array destructuring operates on <span class="green">positions within an array</span> rather than any named properties</p>
<div class="clear small">
<div class="halfblock fragment">
<h3 class="green underlined">ES5:</h3>
<pre><code class="javascript">var colors = ['red', 'green', 'blue'];
var first = colors[0];
var second = colors[1];
var third = colors[2];
console.log(first, second, third);
// red green blue</code></pre>
<p class="right small fragment">becomes</p>
</div>
<div class="halfblock fragment">
<h3 class="green underlined">ES6:</h3>
<pre><code class="javascript">const colors = ['red', 'green', 'blue'];
const [ first, second, third ] = colors;
console.log(first, second, third);
// red green blue</code></pre>
</div>
</div>
<ul class="">
<li class="fragment"><span class="green">Left-hand side of the equal sign (<code>=</code>)</span><br />A comma-separated list of the values you'd like to unpack, wrapped in <code>[]</code></li>
<li class="fragment"><span class="green">Right-hand side of the equal sign (<code>=</code>)</span><br />The array you're destructuring</li>
</ul>
</section>
<!--<section>
<h2>Default Destructuring Values</h2>
<p class="fragment">Set a default value with an equal sign (<code>=</code>). This works when destructuring objects and arrays.</p>
<h3 class="small fragment blue" style="margin-top: 1em;">An example with objects:</h3>
<div class="clear small">
<div class="halfblock fragment">
<h3 class="green underlined">ES5:</h3>
<pre><code class="javascript">var person = {
name: 'Whitney'
};
var name = person.name;
var active = person.active; // undefined
console.log(name, active); // Whitney undefined</code></pre>
<p class="right small fragment">becomes</p>
</div>
<div class="halfblock fragment">
<h3 class="green underlined">ES6:</h3>
<pre><code class="javascript">const person = {
name: 'Whitney'
};
const { name, active = false } = person;
console.log(name, active); // Whitney false</code></pre>
</div>
</div>
<aside class="notes">Ask: What do you think you'd use to set a default value? Similar patterns in ES6 — you can combine tons of different patterns/syntax, so try to give everything a shot.</aside>
</section>-->
<!--<section>
<h2>Rest Parameters</h2>
<p class="fragment">Placing <code>...</code> <span class="green">before a function parameter</span> transforms that parameter and the <span class="blue">rest</span> (i.e. all following parameters) into an array</p>
<pre class="fragment small"><code class="javascript">function giveItARest(param1, ...allTheRest) {
console.log(param1); // 1
console.log(allTheRest); // [2, 3, 4, 5, 6]
}
giveItARest(1, 2, 3, 4, 5, 6);
</code></pre>
<p class="fragment">Useful when you don't know how many arguments will be passed to a function</p>
<aside class="notes">
Compress the <strong>rest</strong> into an array. To change a method name after deploying, when tons of existing apps are using a method by that name, e.g. changing calculateProduct() to multiply(). You can pass the call of one method to another and use the spread operator.
</aside>
</section>
<section>
<h2>Spread operator</h2>
<p>Placing <code>...</code> <span class="green">before an array</span> <span class="blue">spreads</span> the array into its individual elements</p>
<div class="fragment-progression">
<pre class="fragment small"><code class="javascript" contenteditable>const numbers = [2, 3, 4];
</code></pre>
<pre class="fragment small"><code class="javascript" contenteditable>const numbers = [2, 3, 4];
console.log([1, numbers]);</code></pre>
<pre class="fragment small"><code class="javascript" contenteditable>const numbers = [2, 3, 4];
console.log([1, numbers]); // nested array: [1, [2, 3, 4]]</code></pre>
<pre class="fragment small"><code class="javascript" contenteditable>const numbers = [2, 3, 4];
console.log([1, numbers]); // nested array: [1, [2, 3, 4]]
console.log([1, ...numbers]);</code></pre>
<pre class="fragment small"><code class="javascript" contenteditable>const numbers = [2, 3, 4];
console.log([1, numbers]); // nested array: [1, [2, 3, 4]]
console.log([1, ...numbers]); // normal array: [1, 2, 3, 4]</code></pre>
</div>
<aside class="notes">Ask what would the output be?</aside>
</section>
<section>
<h2>Rest vs. Spread</h2>
<p><code>...</code></p>
<p class="fragment"><span class="blue">Rest parameters</span>, primarily used in function parameters, to transform individual elements <span class="green">into an array</span></p>
<pre class="fragment"><code class="javascript">function sum(first, ...others) {
// others will be an array here
}</code></pre>
<p class="fragment"><span class="blue">Spread operator</span> used on an array to transform the array <span class="green">into individual elements</span></p>
<pre class="fragment"><code class="javascript">const arr1 = ['a', 'b', 'c'];
const arr2 = ['x', 'y', 'z'];
const combo = [...arr1, ...arr2];
console.log(combo); // ['a', 'b', 'c', 'x', 'y', 'z']</code></pre>
<aside class="notes">Ask: When you think "parameters", what do you think of? (function)</aside>
</section>-->
<section>
<h2>Array Helpers</h2>
<p>Tons of new methods that can be applied to arrays</p>
<table class="data compact fragment">
<tr>
<th></th>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td>✔️</td>
<td><code class="javascript">arr.forEach((element, index, array) => { });</code></td>
<td>loop through elements in an array</td>
<td><code>undefined</code></td>
</tr>
<tr>
<td>️</td>
<td><code class="javascript">arr.every((element, index, array) => { });</code></td>
<td>check if <span class="blue">every</span> element passes a test</td>
<td><code>true||false</code></td>
</tr>
<tr>
<td></td>
<td><code class="javascript">arr.some((element, index, array) => { });</code></td>
<td>check if <span class="blue">some</span> (at least 1) <code>arr</code> elements pass a test</td>
<td><code>true||false</code></td>
</tr>
<tr>
<td></td>
<td><code class="javascript">arr.filter((element, index, array) => { });</code></td>
<td>create a new array by <span class="blue">filtering</span> the original array elements</td>
<td><code>Array</code></td>
</tr>
<tr>
<td>✔️</td>
<td><code class="javascript">arr.map((element, index, array) => { });</code></td>
<td>create a new array by <span class="blue">modifying</span> the original array elements</td>
<td><code>Array</code></td>
</tr>
<tr>
<td>✔️</td>
<td><code class="javascript">arr.find((element, index, array) => { });</code></td>
<td>find the <span class="blue">first element that passes a test</span></td>
<td>1 element</td>
</tr>
<tr>
<td></td>
<td><code class="javascript">arr.findIndex((element, index, array) => { });</code></td>
<td>find the <span class="blue">index of the first element that passes a test</span></td>
<td>1 element</td>
</tr>
<tr>
<td>✔️</td>
<td><code class="javascript">arr.reduce((calculatedValue, element, index, array) => { }, initialValue);</code></td>
<td>pass in an <code>initialValue</code> and modify it according to the current <code>element</code> value</td>
<td>1 reduced value</td>
</tr>
</table>
<aside class="notes">
Convenient methods applied to arrays. We'll go over 4 of these, with checkmarks. Point out that reduce() is the odd one out. It takes different parameters. Note that you don't need to use ALL the parameters, just the ones you need.
</aside>
</section>
<section>
<h2>Array Helpers</h2>
<h3>Moving away from for loops</h3>
<ul>
<li class="fragment">Easier for other devs to immediately read and understand</li>
<li class="fragment">Encourage functional programming</li>
<li class="fragment">More compact — why write more code?</li>
<li class="fragment">More maintainable and scalable</li>
</ul>
<aside class="notes">Nothing wrong with for loops, but you probably don't need them anymore. Array helpers describe your intent more easily to your teammates, so it's a best practice.</aside>
</section>
<section>
<h2>forEach</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.forEach((element, index, array) => { });</code></td>
<td>loop through elements in an array</td>
<td><code>undefined</code></td>
</tr>
</table>
<div class="small clear fragment">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var names = ['Morgan', 'Taylor', 'Lesley'];
for (var i = 0; i < names.length; i++) {
console.log(names[i]);
}</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6: forEach</h3>
<pre><code contenteditable class ="javascript">const names = ['Morgan', 'Taylor', 'Lesley'];
names.forEach(function(name, index, array) {
console.log(name);
});</code></pre>
</div>
<p><a href="http://codepen.io/anythingcodes/pen/KagmxL?editors=0012" target="_blank">View CodePen »</a></p>
</div>
<aside class="notes">
Activity on this slide: Ask class to arrow functionitize the right-hand ES6 block. Transform it in front of students!
</aside>
</section>
<section>
<h2>forEach</h2>
<code>arr.forEach(iteratorFunction)</code>
<pre class="small javascript"><code contenteditable>const numbers = [123, 500, -50, 82, 8];
numbers.forEach(function(number, index, array) {
console.log(number > 50); // can you get this on one line?
});</code></pre>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:75%;float: left">
<p>The iterator function runs once for each element in the array</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-forEach.svg" alt="forEach diagram" style="max-height: 47vh;"/></p>
</div>
</div>
<aside class="notes">
Ask students what they'd expect to see in the console. Code the arrow function version in front of students. Mention that all array helpers in the class will use arrow functions going forward. Clarify how the iterator function automatically receives the current element, in this case <code>number</code>. Point out the convention of using the plural form as the array name (numbers) and the singular form (number) in your arrow function.
</aside>
</section>
<section>
<h2>map</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.map((element, index, array) => { });</code></td>
<td>create a new array by <span class="blue">modifying</span> the original array elements</td>
<td><code>Array</code></td>
</tr>
</table>
<p> </p>
<p class="fragment">You can <span class="blue">assign the resulting array</span> to a variable:<br /><code>const modifiedArr = arr.map(iteratorFunction);</code></p>
<div class="small clear fragment">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var numbers = [2, 6, 10];
var halvedNumbers = [];
for (var i = 0; i < numbers.length; i++) {
halvedNumbers.push(numbers[i] / 2);
}
console.log(halvedNumbers); // 1, 3, 5
</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6: map</h3>
<pre><code contenteditable class ="javascript" >const numbers = [2, 6, 10];
const halvedNumbers = numbers.map(number => number / 2);
console.log(halvedNumbers); // 1, 3, 5
</code></pre>
</div>
<p><a href="http://codepen.io/anythingcodes/pen/MJjoxm?editors=0012" target="_blank">View CodePen »</a></p>
</div>
<p class="small orange fragment">No need to create an empty array first! 🎉</p>
<aside class="notes">
Primary difference between forEach and map: <em>forEach</em> iterates through an array but <span class="orange">doesn't return anything</span>
</aside>
</section>
<section>
<h2>map</h2>
<code>arr.map(iteratorFunction)</code>
<pre class="small javascript"><code contenteditable>const numbers = [123, 500, -50, 82, 8];
const decrementedNumbers = numbers.map(number => number - 1);
console.log(decrementedNumbers); // [ 122, 499, -51, 81, 7 ]</code></pre>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:75%;float: left">
<p>The iterator function runs once for each element in the array</p>
<p class="fragment info blue">Be sure to return a value each iteration!</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-map.svg" alt="map diagram" style="max-height: 47vh;"/></p>
</div>
</div>
<aside class="notes">
Difference between forEach() and map(): map() returns an array at the end. Don't use map if you just need to loop through an array. forEach doesn't return anything, but map <em>modifies</em> the previous array values ti create a new array.
</aside>
</section>
<section>
<h2>find</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.find((element, index, array) => { });</code></td>
<td>find the <span class="blue">first element that passes a test</span></td>
<td>1 element</td>
</tr>
</table>
<p class="fragment"><code>const matchingObject = arr.find(iteratorFunction);</code></p>
<div class="small clear fragment">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var jobs = [
{ title: 'Electrician' },
{ title: 'Developer' },
{ title: 'Barista' }
];
var devJob;
for (var i = 0; i < jobs.length; i++) {
if (jobs[i].title === 'Developer') {
devJob = jobs[i];
break;
}
}
console.log(devJob); // { title: 'Developer' }</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6: find</h3>
<pre><code contenteditable class ="javascript">const jobs = [
{ title: 'Electrician' },
{ title: 'Developer' },
{ title: 'Barista' }
];
const devJob = jobs.find(job => job.title === 'Developer');
console.log(devJob); // { title: 'Developer' }</code></pre>
<p class="small"><a href="http://codepen.io/anythingcodes/pen/ggLbMO?editors=0012" target="_blank">CodePen Example »</a></p>
</div>
</div>
<aside class="notes">
Don't need to use an if statement, since <code>user.active</code> returns a boolean. Also <code>home.price < 600000</code> returns a boolean.
</aside>
</section>
<section>
<h2>find</h2>
<p><code>arr.find(iteratorFunction)</code></p>
<div class="small"><pre class="small"><code contenteditable class ="javascript">const jobs = [
{ title: 'Electrician' },
{ title: 'Developer' },
{ title: 'Barista' }
];
const devJob = jobs.find(job => job.title === 'Developer');
console.log(devJob); // { title: 'Developer' }</code></pre></div>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:75%;float: left">
<p>Runs until the iterator function returns <code>true</code>. When it does, the element at that index is returned.</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-find.svg" alt="find diagram" style="max-height: 40vh;"/></p>
</div>
</div>
</section>
<section>
<h2><code>find()</code> Practical Example</h2>
<h3>Single-page web apps</h3>
<p><img src="dist/img/single-page-app__find.png" alt="The find helper in a single-page web app" /></p>
</section>
<section>
<h2>reduce</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.reduce((calculatedValue, element, index, array) => { }, initialValue);</code></td>
<td>pass in an <code>initialValue</code> and modify it according to the current <code>element</code> value</td>