forked from anythingcodes/gdi-es6-javascript
/
class1.html
673 lines (594 loc) · 38.3 KB
/
class1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Getting Started with ES6 and ES7 JavaScript</title>
<meta name="description" content="Materials and slides for a Girl Develop It ES6 JavaScript course. Includes ES7's minor additions.">
<meta name="author" content="Girl Develop It">
<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/gdidarkblue.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/pdf.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/pdf.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">
<!-- Opening -->
<section class="small">
<h1>Getting Started with ES6 and ES7 JavaScript</h1>
<img src="dist/img/es6-gdi.jpg" alt="ES6 and ES7 Intro Image" class="noborder"/>
<h2 class="green">Class 1</h2>
</section>
<!-- Compatibility-->
<!-- Welcome! -->
<section class="hide-pdf">
<h2>Welcome!</h2>
<p>Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.</p>
<p> </p>
<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>
<!-- First Things First -->
<section class="hide-pdf">
<h2>First Things First</h2>
<p class="green">Tell us about yourself.</p>
<ul>
<li>Who are you?</li>
<li>What do you hope to get out of the class?</li>
<li>If you woke up tomorrow as an animal, what animal would you choose to be and why?</li>
</ul>
</section>
<section>
<h2>First Things First</h2>
<h3>Course Format</h3>
<ul class="small">
<li>In-Class Activities</li>
<li>At-Home Challenges</li>
<li>Course Website: <a href="http://anything.codes" target="_blank">http://anything.codes</a></li>
<li>Slides: <a href="http://tiny.cc/gdies6" target="_blank">http://tiny.cc/gdies6</a></li>
</ul>
<p> </p>
<p><span class="orange">Need help?</span><br />Note the resources on the whiteboard</p>
<p><strong>Thank you to our wonderful TAs!</strong></p>
<aside class="notes">Mention that there will be HW. Will review the solution before each class, and talk about potential other solutions.</aside>
</section>
<!-- Get Started -->
<section>
<h2>Get Started: Tools</h2>
<p>We'll be using the following tools in class:</p>
<p> </p>
<ul>
<li>
<div class="orange"><strong>Environment</strong>
</div>
<ul>
<li>An optional <a href="http://codepen.io" target="_blank">CodePen</a> account</li>
<li><a href="https://nodejs.org/">Node.js</a> (version 6 or higher)</li>
</ul>
</li>
<li>
<div class="orange"><strong>Command Line Interface (your preference)</strong></div>
<ul>
<li><em>Terminal</em>, <em>cmd</em>, <em>X11</em>, <em>Git Bash</em>, etc.</li>
</ul>
</li>
<li>
<div class="orange"><strong>Text Editor (your preference)</strong>
</div>
<ul>
<li><a href="https://atom.io/" target="_blank">Atom</a>, <a href="http://www.sublimetext.com/2" target="_blank"><span data-title="SublimeText">SublimeText</span></a>, <a href="https://www.jetbrains.com/webstorm/" target="_blank"><span data-title="WebStorm">WebStorm</span></a>, etc.
</li>
</ul>
</li>
</ul>
<aside class="notes">Ask if everyone was able to download everything. If not, no big deal. download now. CodePen recommended if they want to fork the practice exercises and save for later (will review on next slide). WebStorm is the only paid program on this list, but it's super useful if you plan on doing a ton of large JavaScript app development in future.</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Optional Activity</h2>
<h3><svg id="embed-codepen-logo" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="270.438px" height="50.257px" viewBox="0 0 411.438 74.257" enable-background="new 0 0 411.438 74.257" xml:space="preserve">
<g>
<path class="cp-embed-logo" d="M25.857 18.232c4.618 0 8.85 1.67 12.14 4.429l4.472-5.33c-4.496-3.779-10.29-6.061-16.61-6.061 C11.6 11.27 0 22.87 0 37.128c0 14.26 11.6 25.86 25.86 25.856c6.32 0 12.114-2.282 16.61-6.061l-4.472-5.33 c-3.286 2.761-7.521 4.429-12.139 4.429c-10.419 0-18.896-8.476-18.896-18.894C6.962 26.71 15.44 18.23 25.86 18.232z"></path>
<path class="cp-embed-logo" d="M168.993 12.763H155.07c-1.922 0-3.48 1.559-3.48 3.48v41.769c0 1.92 1.56 3.48 3.48 3.48h13.923 c13.436 0 24.365-10.93 24.365-24.365C193.358 23.69 182.43 12.76 168.99 12.763z M168.993 54.532h-10.442V19.725h10.442 c9.597 0 17.4 7.81 17.4 17.404C186.397 46.73 178.59 54.53 168.99 54.532z"></path>
<path class="cp-embed-logo" d="M212.206 16.244v41.769c0 1.92 1.56 3.48 3.48 3.48h29.007v-6.961h-25.525V40.608h16.243v-6.961h-16.243 V19.725h25.525v-6.961h-29.007C213.765 12.76 212.21 14.32 212.21 16.244z"></path>
<path class="cp-embed-logo" d="M317.569 16.244v41.769c0 1.92 1.56 3.48 3.48 3.48h29.006v-6.961H324.53V40.608h16.244v-6.961H324.53 V19.725h25.525v-6.961H321.05C319.128 12.76 317.57 14.32 317.57 16.244z"></path>
<path class="cp-embed-logo" d="M284.443 12.763h-15.084c-1.922 0-3.48 1.559-3.48 3.48v45.25h6.962V40.608h11.603 c7.677 0 13.923-6.246 13.923-13.922C298.366 19.01 292.12 12.76 284.44 12.763z M284.443 33.647h-11.603V19.725h11.603 c3.839 0 6.96 3.12 6.96 6.961S288.282 33.65 284.44 33.647z"></path>
<path class="cp-embed-logo" d="M404.476 12.763v35.636l-28.652-34.384c-0.938-1.127-2.481-1.545-3.859-1.045 c-1.378 0.5-2.296 1.808-2.296 3.273v45.25h6.962V25.858l28.652 34.383c0.675 0.81 1.66 1.25 2.67 1.25 c0.396 0 0.797-0.066 1.185-0.207c1.378-0.5 2.296-1.808 2.296-3.273v-45.25H404.476z"></path>
<path class="cp-embed-logo" d="M131.815 25.261c-0.017-0.09-0.032-0.18-0.056-0.268c-0.015-0.053-0.033-0.103-0.05-0.155 c-0.025-0.078-0.051-0.156-0.082-0.232c-0.022-0.053-0.047-0.104-0.071-0.155c-0.034-0.072-0.069-0.143-0.109-0.212 c-0.028-0.051-0.06-0.099-0.091-0.148c-0.043-0.066-0.087-0.131-0.135-0.194c-0.035-0.047-0.071-0.092-0.109-0.137 c-0.05-0.06-0.104-0.117-0.158-0.173c-0.042-0.042-0.084-0.084-0.128-0.125c-0.058-0.053-0.118-0.103-0.181-0.151 c-0.048-0.037-0.095-0.075-0.145-0.109c-0.018-0.013-0.034-0.028-0.053-0.04L96.511 0.536c-1.071-0.715-2.468-0.715-3.539 0 L59.034 23.161c-0.019 0.012-0.034 0.027-0.053 0.04c-0.05 0.035-0.097 0.072-0.145 0.109c-0.062 0.049-0.123 0.099-0.181 0.15 c-0.044 0.04-0.086 0.082-0.127 0.125c-0.056 0.056-0.108 0.113-0.159 0.173c-0.038 0.045-0.074 0.09-0.109 0.14 c-0.048 0.063-0.092 0.127-0.135 0.194c-0.031 0.049-0.062 0.097-0.091 0.148c-0.04 0.069-0.075 0.14-0.108 0.21 c-0.024 0.051-0.05 0.103-0.071 0.155c-0.031 0.076-0.058 0.154-0.083 0.232c-0.017 0.052-0.035 0.103-0.049 0.15 c-0.023 0.088-0.04 0.177-0.056 0.268c-0.009 0.046-0.02 0.091-0.026 0.138c-0.018 0.138-0.028 0.276-0.028 0.417V48.44 c0 0.14 0.01 0.28 0.03 0.417c0.007 0.05 0.02 0.09 0.03 0.138c0.016 0.09 0.03 0.18 0.06 0.27 c0.014 0.05 0.03 0.1 0.05 0.155c0.025 0.08 0.05 0.16 0.08 0.233c0.021 0.05 0.05 0.1 0.07 0.15 c0.033 0.07 0.07 0.14 0.11 0.213c0.028 0.05 0.06 0.1 0.09 0.146c0.043 0.07 0.09 0.13 0.14 0.19 c0.035 0.05 0.07 0.09 0.11 0.138c0.051 0.06 0.1 0.12 0.16 0.173c0.041 0.04 0.08 0.09 0.13 0.12 c0.058 0.05 0.12 0.1 0.18 0.152c0.048 0.04 0.1 0.07 0.14 0.109c0.019 0.01 0.03 0.03 0.05 0.039L92.972 73.72 c0.536 0.36 1.15 0.54 1.77 0.537s1.234-0.18 1.77-0.537l33.938-22.625c0.019-0.012 0.035-0.026 0.053-0.039 c0.05-0.035 0.097-0.072 0.145-0.109c0.062-0.049 0.123-0.1 0.181-0.152c0.044-0.039 0.086-0.082 0.128-0.124 c0.055-0.056 0.108-0.113 0.158-0.173c0.038-0.045 0.074-0.09 0.109-0.138c0.048-0.063 0.092-0.128 0.135-0.194 c0.031-0.048 0.062-0.097 0.091-0.146c0.04-0.07 0.075-0.141 0.109-0.213c0.024-0.051 0.049-0.102 0.071-0.154 c0.031-0.077 0.057-0.155 0.082-0.233c0.017-0.052 0.035-0.103 0.05-0.155c0.023-0.088 0.039-0.178 0.056-0.268 c0.008-0.046 0.02-0.091 0.025-0.138c0.018-0.138 0.028-0.276 0.028-0.417V25.816c0-0.141-0.011-0.279-0.028-0.417 C131.835 25.35 131.82 25.31 131.81 25.261z M94.741 44.677l-11.285-7.548l11.285-7.549l11.286 7.549L94.741 44.677z M91.551 24.037L77.717 33.29L66.55 25.82L91.551 9.153V24.037z M71.978 37.128l-7.982 5.339V31.789L71.978 37.128z M77.717 40.97 l13.834 9.252v14.884L66.55 48.437L77.717 40.968z M97.932 50.22l13.834-9.252l11.168 7.469L97.932 65.104V50.22z M117.505 37.13 l7.983-5.34v10.679L117.505 37.128z M111.766 33.29l-13.834-9.252V9.153l25.002 16.667L111.766 33.29z"></path>
</g>
</svg></h3>
<ol>
<li>Login to <a href="http://codepen.io" target="_blank">http://codepen.io</a> (it's free!)</li>
<li>For each CodePen exercise, click <code>Edit on CodePen</code></li>
<li>Click the <code>Fork</code> button at the top</li>
<li>Edit away! Your changes will save to a new pen in your CodePen account</li>
</ol>
<p data-height="150" data-theme-id="dark" data-slug-hash="GmqavN" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Block-scoping: ES5 (Before)" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/GmqavN/">Block-scoping: ES5 (Before)</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<aside class="notes">Show students how to fork a CodePen and save their work for each exercise.</aside>
</section>
<!--<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activity</h2>
<h3>Node.js Check</h3>
<ol>
<li>Open your favorite command line application (such as <em>Terminal</em> or <em>cmd</em>)</li>
<li>Type in <code>node -v</code> and press <em>Enter</em></li>
<li>If your version is <em>6</em>, you're good to go!</li>
</ol>
<p><img src="dist/img/nodejs.png" alt="NodeJS logo" style="max-height: 150px;background-color:#fff;" /></p>
<p class="small"><em>No luck? No problem — we won't be using Node.js until a later session. See a TA or instructor after class and we'll help you out!</em></p>
</section>-->
<section data-background="#f05b62">
<h1 style="color: #fafafa;font-size:3.7em;">Background</h1>
</section>
<!--<section>
<h2>History</h2>
<p>JavaScript's standard referred to as <span class="green">ECMAScript</span>, abbreviated <span class="green">ES</span>. Was eventually versioned by ordinal number (e.g. ES<span class="blue">3</span>).</p>
<p class="fragment"><img src="dist/img/history-es6.png" alt="The history of ECMAScript and JavaScript" /></p>
<ul>
<li class="fragment"><span class="orange">2015:</span> <span class="blue">ES6</span> / ECMAScript 2015 / ES2015</li>
<li class="fragment"><span class="orange">2016:</span> <span class="blue">ES7</span> / ECMAScript 2016 / ES2016</li>
<li class="fragment"><span class="orange">ES.Next</span> — The next upcoming release of ECMAScript</li>
</ul>
<aside class="notes">
<ul>
<li>ES4 never released due to political reasons we won't get into :)</li>
<li>ES6 is sometimes called ECMAScript 2015 because it was released in 2015. ES6 and ECMAScript are the same thing. This is true for ES7, a.k.a. ECMAScript 2016.</li>
<li>ES6 gets the most attention because it was a huge spec overhaul. It introduced a ton of new features. ES7 and all versions going forward will be smaller, more incremental changes; ES7 only introduced two new features, so we'll discuss them as part of this class.</li>
</ul>
</aside>
</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 versions <span class="orange">6 and 7 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>Why use ES6?</h2>
<ul style="font-size:90%;">
<li>Sure, there are handy new methods, syntax, and easier-to-read code, but...</li>
<li class="fragment">Top JavaScript libraries will be written in ES6. If you aren't familiar with ES6, you'll be at a disadvantage when trying to read documentation and build new features.</li>
<li class="fragment">Backwards compatible</li>
<li class="fragment">Integrates nicely with modern build tools</li>
</ul>
<p class="fragment"><br /><span class="orange">🌟 Above all, it's the new standard.</span> This means that, in the near future, ES6 will be standard in each browser.</p>
<aside class="notes">
<ul>
<li>Bigger leap than what existed in previous standards</li>
<li>Everything relevant going forward will use ES6</li>
</ul>
</aside>
</section>
<section>
<h2>Today's Topics</h2>
<ol>
<li>Transpiling</li>
<li>Scopes & Variables</li>
<li>String Helpers</li>
</ol>
</section>
<section data-background="#f05b62">
<h1 style="color: #fafafa;font-size:3.7em;">Transpiling</h1>
</section>
<section>
<h2><span class="green">Transpiling</span> = Transformation + Compiling</h2>
<p> </p>
<img src="dist/img/transpiling.svg" alt="Transpiling" />
<div class="transpiler clear">
<div class="halfblock">
<h3 class="green">Before: ES6</h3>
<pre><code contenteditable class ="javascript transpiler__input">const getMessage = () => 'Hello World';</code></pre>
</div>
<div class="halfblock">
<h3 class="green">After: ES5</h3>
<pre><code class="javascript transpiler__output">Waiting...</code></pre>
</div>
</div>
<p> </p>
<aside class="notes">Point out how this code block is being transpiled to that via Babel. Break the ES6 code as an example. Ask who's seen a <code>src</code> and <code>dist</code> folder. Both of these are creating a function that returns 'Hello World'. You and your team can focus on writing readable, less error-prone code in fewer lines. Enforces good coding practices behind the scenes (<code>'use strict';</code>). Allows you to focus on writing readable code in less lines. Build tools (e.g. WebPack) integrate nicely with this workflow and can be used to minify the ES5 result, add linting, add prefixes to CSS automatically, etc. [[[Browsers don’t yet implement natively a lot of ES6+ features, and developers want to innovate on what JavaScript is. This means they’ve built tools like Babel that implement futuristic JavaScript features and compile down to ES5.]]]
</aside>
</section>
<section>
<h2>Benefits to Transpiling</h2>
<ul>
<li class="fragment">Browser support</li>
<li class="fragment">No need to wait for browsers/engines to catch up with the ECMAScript specification</li>
<li class="fragment">Focus on writing clean, easy-to-read code — compile it to a cross-browser-compatible format</li>
<li class="fragment">JavaScript will continue to <a href="https://dayssincelastjavascriptframework.com/" target="_blank">evolve constantly</a>; without transpilation, you'll miss out on innovations that make JavaScript more effective, efficient, and robust</li>
</ul>
</section>
<section>
<h2>Transpiling Setup</h2>
<p>You can set this up on your machine using Node.js. For now, CodePen can use Babel to transpile ES6 for us.</p>
<p><img src="dist/img/codepen-babel.gif" style="border: 1px solid black;max-height:40vh;" alt="Using the Babel transpiler in CodePen" /></p>
</section>
<section data-background="#f05b62">
<h1 style="color: #fafafa;font-size:3.7em;">Scopes & Variables</h1>
<aside class="notes">Can someone tell me what scope means in the context of programming in general?</aside>
</section>
<section>
<h2>Scopes 🔬</h2>
<ol class="small">
<li>
<strong class="green">Block Scopes (a.k.a. lexical scopes):</strong>
<pre><code contenteditable class="javascript">{
// anything within { } that isn't a function
}
if (isPhiladelphia) {
var alwaysSunny = true;
}</code></pre>
<ul class="small">
<li><span class="blue">Examples:</span> <code>{ }</code>, <code>if () { }</code>, <code>for() {}</code>, <code>while() { }</code>, etc.</li><li><span class="blue">Purpose:</span> Declare variables as close as possible, as local as possible, to where they will be used.</li>
</ul>
</li>
<li class="fragment">
<strong class="green">Function Scopes:</strong>
<pre><code contenteditable class="javascript">function func() {
// function required
}</code></pre>
<ul class="small"><li><span class="blue">Purpose:</span> Code wrapped in a function effectively "hides" any enclosed variable or function declarations from the outside scope.</li></ul>
</li>
</ol>
<p style="margin-top:.3em;" class="fragment"><code>var</code> <span class="orange">only respects function scopes</span></p>
<p class="fragment small info"><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch3.md" target="_blank">Function vs. Block Scope »</a></p>
<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>Ask what the other form of scope is BEFORE the function scope definition shows.</li>
<li><code>alwaysSunny</code> exists in the global scope</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>Function Scope Problems</h2>
<p><code>var</code> variables are <span class="green">function-scoped</span>.<br />They ignore block scoping.</p>
<div style="min-height: 200px;">
<p data-height="200" data-theme-id="dark" data-slug-hash="GmqavN" data-default-tab="js,result" data-user="anythingcodes" data-embed-version="2" data-pen-title="Block-scoping: ES5 (Before)" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/GmqavN/">Block-scoping: ES5 (Before)</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-1" href="http://codepen.io/anythingcodes/pen/zwBQEX?editors=0010" target="_blank">View Solution</a>
</div>
<p>To fake block scoping, an <span class="green">immediately-invoked function expression (IIFE)</span> is often used</p>
<aside class="notes">
<ol>
<li>DON'T open console, ask students:
<ol>
<li>What would the output be?</li>
<li>What if we want it to say Alpha, and then Beta? How can we solve this? (one potential solution: IIFE)</li>
</ol>
</li>
<li>{ ... } is perfectly valid JavaScript, but you probably don't see it often — before ES6, it was basically useless. Not anymore!</li>
<li>Ask what the output would be. DON'T open the console first.</li>
</ol>
</aside>
</section>
<section>
<h2>The <em>let</em> Declaration</h2>
<ul>
<li class="fragment"><span class="blue">Block-scoped</span> <code>{ }</code>, unlike the function-scoped <code>var</code>
</li>
<li class="fragment">Value <span class="green">can</span> be reassigned</li>
</ul>
<div class="fragment" style="min-height:200px;">
<p data-height="200" data-theme-id="dark" data-slug-hash="WjxBLy" data-default-tab="js,result" data-user="anythingcodes" data-embed-version="2" data-pen-title="Block-scoping with let (Before)" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/WjxBLy/">Block-scoping with let (Before)</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-1" href="http://codepen.io/anythingcodes/pen/WjxVQb?editors=0011" target="_blank">View Solution</a>
</div>
<p class="fragment">Way easier than using an IIFE! 🏆</p>
<aside class="notes">
Doesn't have to use a function or IIFE to create a new scope
</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activity</h2>
<p>Instructions can be found by editing the below pen on CodePen. Be sure to fork the pen first if you'd like to save your work to your account!</p>
<div class="clear">
<div class="halfblock" style="min-height: 280px;">
<h3>Activity 1</h3>
<p data-height="280" data-theme-id="dark" data-slug-hash="OmYGzQ" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Block-scoping" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/OmYGzQ/">Block-scoping</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-1" href="http://codepen.io/anythingcodes/pen/zwQXRY?editors=0012" target="_blank">View Solution</a>
</div>
<div class="halfblock" style="min-height: 280px;">
<h3>Activity 2</h3>
<p data-height="280" data-theme-id="dark" data-slug-hash="LyZwRr" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Block-scoping with let (Before)" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/LyZwRr/">Block-scoping with let (Before)</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-1" href="http://codepen.io/anythingcodes/pen/OmXKRK?editors=0011" target="_blank">View Solution</a>
</div>
</div>
<aside class="notes">
<ul>
<li>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.</li>
</ul>
</aside>
</section>
<section>
<h2>The <em>const</em> Declaration</h2>
<ul>
<li class="fragment"><span class="blue">Block-scoped</span>, like <code>let</code></li>
<li class="fragment">Value <span style="color:red;">cannot</span> be reassigned
<ul>
<li class="fragment"><code>const</code> stands for <span class="orange">constant</span></li>
</ul>
</li>
<li class="fragment">Must be initialized on declaration</li>
<li class="fragment">Communicates your variable's intent to other devs</li>
<li class="fragment">Helps catch unintended changes</li>
<li class="fragment">No more <pre style="display: inline-block"><code class="javascript">var PLZ_DONT_CHANGE = 42;</code></pre>🎉</li>
</ul>
<p> </p>
<div class="fragment">
<p data-height="120" data-theme-id="dark" data-slug-hash="bWwbKa" data-default-tab="js,result" data-user="anythingcodes" data-embed-version="2" data-pen-title="const (Before)" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/bWwbKa/">const (Before)</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-1" href="https://codepen.io/anythingcodes/pen/jmMNpK?editors=0010" target="_blank">View Solution</a>
</div>
<aside class="notes">
Change the CodePen on screen
</aside>
</section>
<section>
<h2>Modifying Complex Values</h2>
<p class="fragment">
Although you can't <span class="orange">reassign</span> a <code>const</code>'s value, <span class="green">contents of complex values</span> (objects & arrays) <strong>can</strong> be modified</p>
<div class="fragment-progression">
<p class="fragment"><img src="dist/img/const-bindings__step1.gif" alt="const bindings and reassignments" /></p>
<p class="fragment">
<img src="dist/img/const-bindings__step2.gif" alt="const bindings and reassignments" />
</p>
<p class="fragment">
<img src="dist/img/const-bindings__step3.gif" alt="const bindings and reassignments" />
</p>
<p class="fragment">
<img src="dist/img/const-bindings__step4.gif" alt="const bindings and reassignments" />
</p>
<p class="fragment">
<img src="dist/img/const-bindings__step5.gif" alt="const bindings and reassignments" />
</p>
</div>
<aside class="notes">
The variable doesn't hold a constant array/object. Rather, it holds a constant reference to the array/object. The array/object itself is freely mutable. A const binding that references an object can’t later reference a different value, but the underlying object can indeed mutate.
</aside>
</section>
<section>
<h2>Modifying Complex Values</h2>
<h3>Examples</h3>
<p data-height="280" data-theme-id="dark" data-slug-hash="WjGeaJ" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Updating the contents of const values" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/WjGeaJ/">Updating the contents of const values</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<aside class="notes">
The variable doesn't hold a constant array/object. Rather, it holds a constant <strong>reference</strong> to the array/object. The array/object itself is freely mutable.
</aside>
</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 class="activity" data-background="rgb(6, 116, 123)">
<h2>Activity</h2>
<p>Complete activities 1 and 2 in CodePen. Be sure to fork the pen first if you'd like to save your work to your account!</p>
<div class="clear">
<div class="halfblock" style="min-height:265px;">
<h3>Activity 1</h3>
<p data-height="265" data-theme-id="0" data-slug-hash="rmmjXP" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="const and let exercise" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/rmmjXP/">const and let exercise</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<a href="http://codepen.io/anythingcodes/pen/dWrgrB" target="_blank" class="solution solution__lesson-1">View Solution</a>
</div>
<div class="halfblock" style="min-height:265px;">
<h3>Activity 2</h3>
<p data-height="265" data-theme-id="0" data-slug-hash="eWaoxY" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="const and let exercise #2" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/eWaoxY/">const and let exercise #2</a> by Liz Shaw (<a href="https://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<a href="http://codepen.io/anythingcodes/pen/aWrxXN?editors=0012" target="_blank" class="solution solution__lesson-1">View Solution</a>
</div>
</div>
</section>
<section data-background="#f05b62">
<h1 style="color: #fafafa;font-size:3.7em;">String Helpers</h1>
</section>
<section>
<h2>String Helper Methods</h2>
<p>Step aside, <code>indexOf()</code> — there are now easier ways to identify substrings:</p>
<ol>
<li class="fragment"><code>includes()</code> returns <span class="green">true</span> or <span class="green">false</span> depending on if the given text is found anywhere within the string</li>
<li class="fragment"><code>startsWith()</code> returns <span class="green">true</span> or <span class="green">false</span> depending on if the given text is found at the beginning of the string</li>
<li class="fragment"><code>endsWith()</code> returns <span class="green">true</span> or <span class="green">false</span> depending on if the given text is found at the end of the string</li>
</ol>
</section>
<section>
<h2>String Helper Methods</h2>
<p class="fragment">Each method accepts two arguments:</p>
<ol>
<li class="fragment">the <span class="green">text to search for</span></li>
<li class="fragment">an <span class="green">optional index</span> from which to start the search
<ul>
<li class="small fragment"><code>includes()</code> and <code>startsWith()</code> <span class="orange">include</span> the character at that index, while <code>endsWith()</code> <span class="orange">excludes</span> the character at that index</li>
</ul>
</li>
</ol>
<div class="fragment" style="min-height: 300px;">
<p data-height="270" data-theme-id="0" data-slug-hash="aWWJPZ" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="String helper methods: includes(), startsWith(), endsWith()" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/aWWJPZ/">String helper methods: includes(), startsWith(), endsWith()</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
</div>
<aside class="notes">Case sensitivity still matters. We aren't going to do any exercises around these, but they're included for reference. Good to be aware of. The only thing to note is how the index is handled in <code>endsWith()</code></aside>
</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">'<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">`<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 class="activity" data-background="rgb(6, 116, 123)">
<h2>Activity</h2>
<p>Refactor this code to use template strings.</p>
<div style="min-height: 300px">
<p data-height="300" data-theme-id="0" data-slug-hash="MmmoWo" data-default-tab="js,result" data-user="anythingcodes" data-embed-version="2" data-pen-title="Template Strings Activity" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/MmmoWo/">Template Strings Activity</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
</div>
<a href="http://codepen.io/anythingcodes/pen/EmMdqG" target="_blank" class="solution__lesson-1 solution">View Solution</a>
<aside class="notes">Focus on the <code>html</code> variable</aside>
</section>
<section class="activity__challenge" data-background="#d2ccfc">
<h2>At-Home Challenge</h2>
<h3>Fruit Basket</h3>
<div class="small clear">
<div class="halfblock">
<pre><code contenteditable class ="javascript" >var fruit = 'apple';
(function basket(){
var fruit = 'banana';
console.log(fruit);
})();
console.log(fruit);
var fruitBasket = {
fruit: 'mango',
getFruit: function(){
console.log(fruit);
fruit = 'orange';
}
};
fruitBasket.getFruit();
console.log(fruit);</code></pre>
</div>
<div class="halfblock">
<p>What would the following output to the console and why?</p>
</div>
</div>
<aside class="notes">
Common interview question!
</aside>
</section>
<!-- Next Week -->
<section class="hide-pdf">
<h2>Next Week</h2>
<ul>
<li>Review at-home challenge from today's class</li>
<li>Arrow Functions</li>
<li>Arrow Functions and <code>this</code></li>
<li>Array Helpers</li>
<li>Altering Arrays</li>
</ul>
<p> </p>
<p class="small blue"><em>Remember to do the at-home challenge!</em></p>
</section>
<section class="hide-pdf">
<h2>Questions</h2>
<div style="font-size:1200%; height:100%;" class="blue">?
<div class="clear"></div>
</div>
<aside class="notes">Can always email me!</aside>
</section>
</div>
<footer>
<div class="copyright">
View Slides at <a href="http://tiny.cc/gdies6">tiny.cc/gdies6</a> -
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="dist/js/head.min.js"></script>
<script src="dist/js/babel.min.js"></script>
<script src="dist/js/reveal.min.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,
transition: 'convex', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
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, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
<script async src="//production-assets.codepen.io/assets/embed/ei.js"></script>
</body>
</html>