-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
2877 lines (2623 loc) · 127 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" />
<link rel="icon" type="image/x-icon" href="./assets/img/JavaScript-logo.png">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Training</title>
<link rel="stylesheet" href="./assets/css/global.css">
</head>
<body>
<div class="reveal">
<div class="menubar" id="menubar">
<div class="dropdown">
<input id="checkbox_toggle" type="checkbox">
<label for="checkbox_toggle" data-i18n="chooselanguage">Language</label>
<div class="langchooser">
<label>
<input type="radio" name="menulangradios" value="en" checked><span>English</span>
</label>
<label>
<input type="radio" name="menulangradios" value="fr"><span>Français</span>
</label>
</div>
</div>
</div>
<div class="slides">
<section id="welcome">
<a href="./"><img src="./assets/img/JavaScript-logo.png" alt="logo" class="logo"></a>
<h1 data-i18n="title">JavaScript Training</h1>
<p>
<a href="https://github.com/worldline/JS-Training" target="_blank">
https://github.com/worldline/JS-Training
</a>
</p>
<div class="langchooser flags">
<label>
<input type="radio" name="langradios" value="en" checked><img src="./assets/img/en.png">
</label>
<label>
<input type="radio" name="langradios" value="fr"><img src="./assets/img/fr.png">
</label>
</div>
</section>
<section class="small-list" id="intro">
<ul>
<li data-i18n="1">
This course is for developers who <b>already have a beginner to intermediate level in JavaScript</b> and who
wish to deepen their knowledge of this language to better understand more ambitious JS projects.
</li>
<li class="fragment fade" data-i18n="2">
It is classified as <b>advanced</b>, which means that we will focus more on some technical aspects of the language
and on the study of some advanced programming patterns.
</li>
<li class="fragment fade" data-i18n="3">
The training focuses on the language and is <b>agnostic of any framework or environment</b>, meaning that this
can be applied to both front-end (Angular, Vue.js, Ember...) and back-end (Node.js).
</li>
<li class="fragment fade" data-i18n="4">
It is the ideal opportunity to review and consolidate the basics for all those who have learned
JavaScript "on the job", or to <b>prepare or complement a training on a modern web framework</b>.
</li>
</ul>
</section>
<section class="agenda" id="agenda">
<h2>Agenda</h2>
<div class="flex-slide">
<div style="flex-basis: 100%;">
<p class="chapter-title" data-i18n="chapter-1">Introduction</p>
<ul>
<li data-i18n="list-1">ECMAScript, TC39 and versions of JavaScript</li>
</ul>
<p class="chapter-title" data-i18n="chapter-2">A reintroduction to JavaScript</p>
<ul>
<li data-i18n="list-2">Primitives and Objects</li>
<li data-i18n="list-3">Property descriptors</li>
<li data-i18n="list-4">Scopes and Closures</li>
<li data-i18n="list-5">Functions and contexts</li>
<li data-i18n="list-6">Implicit casts</li>
</ul>
</div>
<div style="flex-basis: 100%;">
<p class="chapter-title" data-i18n="chapter-3">Modern JS: ES2015+</p>
<ul>
<li data-i18n="list-7">Arrow functions</li>
<li data-i18n="list-8">Default parameters</li>
<li data-i18n="list-9">Spread / Rest</li>
<li data-i18n="list-10">Destructuring</li>
<li data-i18n="list-11">Template literals</li>
<li data-i18n="list-12">Map and Sets</li>
<li data-i18n="list-13">Modules</li>
</ul>
<p class="chapter-title" data-i18n="chapter-4">Managing asynchronicity</p>
<ul>
<li data-i18n="list-14">Asynchronicity, what is it?</li>
<li data-i18n="list-15">Callbacks and Pub/Sub</li>
<li data-i18n="list-16">Promises</li>
<li data-i18n="list-17">async / await</li>
<li data-i18n="list-18">Observables</li>
</ul>
</div>
<div style="flex-basis: 100%;">
<p class="chapter-title" data-i18n="chapter-5">Object oriented</p>
<ul>
<li data-i18n="list-19">Prototypes and delegation</li>
<li data-i18n="list-20">class / super / extends</li>
<li data-i18n="list-21">Composition of objects</li>
<li data-i18n="list-22">Symbols, iterables and generators</li>
<li data-i18n="list-23">Proxies</li>
</ul>
<p class="chapter-title" data-i18n="chapter-6">Functional programming</p>
<ul>
<li data-i18n="list-24">Recursivity</li>
<li data-i18n="list-25">Chainable APIs</li>
<li data-i18n="list-26">Function Composition</li>
<li data-i18n="list-27">Currying</li>
</ul>
<p class="chapter-title" data-i18n="chapter-7">Transpilers</p>
<ul>
<li data-i18n="list-28">Babel</li>
<li data-i18n="list-29">TypeScript, JSX</li>
</ul>
</div>
</div>
</section>
<section>
<section id="cheat-sheets">
<h2 data-i18n="title">Cheat Sheets</h2>
<p data-i18n="description">It can be useful</p>
</section>
<section id="string-methods">
<h3 data-i18n="title">String Methods</h3>
<pre style="font-size: 80%"><code class="javascript hljs" data-trim data-line-numbers>
"str".charAt(1) === "str"[1] === "t"
"a".concat("b") === "a" + "b" === "ab"
"str".startsWith("s") === true
"str".endsWith("r") === true
"str".includes("st") === true
"toto".indexOf("t") === 0
"toto".lastIndexOf("t") === 2
"str".search(/t/) === 1
"str1".match(/r(\d)/) => ["r1", "1"]
"Hi Bob".replace(/Hi (\w+)/, "Hello $1") => "Hello Bob"
"string".slice(0,3) === "str"
"1,2,3".split(",") => ["1","2","3"]
"STR".toLowerCase() === "str"
"str".toUpperCase() === "STR"
" str ".trim() == "str"
</code></pre>
</section>
<section id="array-methods">
<h3 data-i18n="title">Array Methods</h3>
<pre style="font-size: 50%"><code class="javascript hljs" data-trim data-line-numbers>
[1,2].concat([3,4], 5, 6) // [1,2,3,4,5,6]
[1,2,3].fill(4) // [4,4,4]
[1,2,3].includes(2) // true
[1,2,3].indexOf(2) // 1
[1,2,3].join("-") // "1-2-3"
[1,2,3,4].slice(1,3) // [2,3]
[1,2,3,4].every((value, index, arr) => n > 2) // false
[1,2,3,4].some((value, index, arr) => n > 2) // true
[1,2,3,4].find((value, index, arr) => n > 2) // 3
[1,2,3,4].findIndex((value, index, arr) => n > 2) // 2
[1,2,3,4].filter((value, index, arr) => n > 2) // [3,4]
[1,2,3,4].forEach((value, index, arr) => { ... })
[1,2,3,4].map((val, index, arr) => val * 2) // [2,4,6,8]
[1,2,3,4].reduce((accu, n) => accu + n, "test") // "test1234"
[1,2,3,4].reduceRight((accu, n) => accu + n, "test") // "test4321"
// /!\ Mutative methods, original array is modified!
let arr = [3,2,1]
arr.push(4,5) // returns new array.length (5), a = [3,2,1,4,5]
arr.pop() // returns last item (5), a = [3,2,1,4]
arr.unshift(6,5) // returns new array.length (6) ; a = [6,5,3,2,1,4]
arr.shift() // returns first item (6) ; a = [5,3,2,1,4]
arr.splice(1,2,7,8) // at index 1, remove 2 items, insert (7,8) ; [5,7,8,1,4]
arr.sort((a,b) => a-b) // [1,4,5,7,8]
arr.reverse() // [8,7,5,4,1]
</code></pre>
</section>
<section>
<h3>Map</h3>
<pre style="font-size: 80%"><code class="javascript hljs" data-trim data-line-numbers>
let map = new Map([ ['key1', 'val1'], ['key2', 'val2'] ])
let key = {}, value = {} // any type
map.set(key, value)
map.get(key) === value
map.has(key) === true
map.delete(key);
map.size === 2
let keys = [...map.keys()]
let values = [...map.values()]
let fusionMap = new Map([...map1, ...map2])
for(let [key, val] of map){ }
map.forEach((val, key, map) => { })
</code></pre>
</section>
<section>
<h3>Set</h3>
<pre><code class="javascript hljs" data-trim data-line-numbers>
let mySet = new Set([ 1,2,3,4 ])
▸ Set(4) {1, 2, 3, 4}
mySet.add("any type")
map.has(3) === true
map.delete(3);
map.size === 4;
map.clear(); // remove all
for(let item of mySet){ }
mySet.forEach(item => { })
let items = [...mySet];
</code></pre>
</section>
</section>
<section>
<section id="chapter-1">
<h2 data-i18n="title">Introduction</h2>
<p data-i18n="description">
Who manages JavaScript? How is the language evolving? <br />
ECMAScript, TC39 and JavaScript versions
</p>
</section>
<section id="ecmascript">
<h3 class="flex-center">ECMAScript <img height="50px" src="assets/img/ecma.jpg"></h3>
<ul>
<li data-i18n="list-1">
<b>ECMA International</b> is an organisation that creates standards for technologies
</li>
<li data-i18n="list-2">
<b>ECMAScript</b> is the name of the specification referenced ECMA-262 and describing a general purpose scripting language
</li>
<li data-i18n="list-3">
<b>TC39</b> is the name of the technical committee that decides on developments in the ECMAScript specification.
It is composed of delegates from <b>major companies</b> (including all browser providers) and <b>invited experts.</b>
</li>
<li data-i18n="list-4">
<b>JavaScript</b> is a versatile scripting language and the most popular implementation of ECMAScript<br>
<em>(another popular implementation: ActionScript)</em>
</li>
</ul>
</section>
<section id="javascript-evolutions" style="text-align: left;">
<h3 data-i18n="title">JavaScript Evolutions</h3>
<p data-i18n="text-1">
TC39 is considering proposals for ECMAScript developments. Each proposal goes through 5 stages:
</p>
<ul>
<li data-i18n="list-1">0:
<b>Strawman -</b> suggestion of a need, call for ideas
</li>
<li data-i18n="list-2">1:
<b>Proposal -</b> draft solution, first polyfills/demos
</li>
<li data-i18n="list-3">2:
<b>Draft -</b> precisely describes the syntax/API
</li>
<li data-i18n="list-4">3:
<b>Candidate -</b> signed by the whole committee, call for implementations
</li>
<li data-i18n="list-5">4:
<b>Finished -</b> ready to be included in the next version of ES
</li>
</ul>
<div style="text-align: center;">
<p data-i18n="text-2">TC39 proposals and meetings are public: <a href="https://github.com/tc39" target="_blank">github.com/tc39</a></p>
<aside class="note warning" data-i18n="note">
Any proposal before stage 4 can be dropped completely. <br />
However, from stage 3 onwards, the level of confidence is quite high.
</aside>
</div>
</section>
<section id="ecmascript-versions">
<h3 data-i18n="title">Versions of ECMAScript</h3>
<img src="./assets/img/ecmascript-versions.svg" height="150px">
<p data-i18n="text-1">
A long delay between initial draft and release implies many browser-specific implementations.
</p>
<p data-i18n="text-2">
TC39 has decided to move to an annual release schedule from ES6, renamed <b>ES2015</b>
</p>
<p data-i18n="text-3">
Since 2015, a new version of the ECMAScript specification is released every year: ES2016, ES2017...
</p>
</section>
<section id="ecmascript-navigator">
<h3 data-i18n="title">ECMAScript browser implementation</h3>
<p data-i18n="text-1">
As a result of the new annual rhythm:<br />
<small>an excellent, comprehensive and consistent support across all modern browsers</small>
</p>
<div class="flex-center">
<p data-i18n="text-2">So-called "<span class="green">evergreen</span>" browsers</p>
<img height="450px" src="./assets/img/evergreen.png">
</div>
</section>
</section>
<section>
<section id="chapter-2">
<h2 data-i18n="title">A reintroduction to JS</h2>
<p data-i18n="description">Exercises 1 to 6</p>
</section>
<section id="exercices-training" style="text-align: left;">
<h3 data-i18n="title">Exercises</h3>
<p data-i18n="text-1">
Slides and exercises available on Github:
<a href="https://github.com/worldline/JS-Training" target="_blank">https://github.com/worldline/JS-Training</a>
</p>
<p data-i18n="text-2">
Do the exercises from your browser at this address:
<a href="https://codesandbox.io/s/github/worldline/JS-Training" target="_blank">https://codesandbox.io/s/github/worldline/JS-Training</a>
</p>
<img src="./assets/img/Image1.png">
<p data-i18n="text-3">
Fork to create your personal version of the working repo.
You can publish it on your personal Github account if you have one.
</p>
<div class="flex-center">
<img height="142px" src="./assets/img/Image2.png">
<img height="142px" src="./assets/img/Image3.png">
<img height="142px" src="./assets/img/Image4.png">
</div>
<p data-i18n="text-4">For each exercise, correct the source to validate the associated test</p>
</section>
<section id="types-primitives">
<h3 data-i18n="title">Types and Primitives</h3>
<span class="ref" data-i18n="exo">Exercise 1</span>
<p data-i18n="text-1">JavaScript defines 7 data types:</p>
<ul class="narrow has-brace-right">
<li><code>null</code></li>
<li><code>undefined</code></li>
<li><code>boolean</code></li>
<li><code>number</code></li>
<li><code>string</code></li>
<li><code>symbol</code> (new in ES6)</li>
<li><code>object</code></li>
<span class="brace" style="height: 84%; top: 42%;"></span>
<span class="brace-description" style="top: 42%">Primitives</span>
</ul>
<p data-i18n="text-2">The <b class="monospace">typeof</b> operator returns the type of an object:</p>
<pre><code class="javascript hljs" data-trim>
typeof true === "boolean"
typeof 12 === "number"
</code></pre>
<p data-i18n="text-3">but be careful of these exceptions:</p>
<pre><code class="javascript hljs" data-trim>
typeof null === "object" // bug in the 1st version of ES
typeof function(){} === "function"
</code></pre>
</section>
<section id="null-or-undefined">
<h3 data-i18n="title">null or undefined</h3>
<ul>
<li data-i18n="text-1"><b class="monospace">undefined</b> : the variable is declared but not assigned</li>
<li data-i18n="text-2"><b class="monospace">null</b> : the variable has been assigned to the null reference</li>
</ul>
<p data-i18n="text-3">
<b class="monospace">null</b> represents the intentional absence of value and often comes from an assignment by the developer
</p>
<aside class="note warning">
<span data-i18n="note">Don't assign variables to undefined. Use the delete operator instead:</span><br>
<img height="70px" src="./assets/img/Image6.png">
</aside>
</section>
<section id="objects" style="text-align: left;">
<h3 data-i18n="title">Objects</h3>
<p data-i18n="text">Functions, arrays and other data structures are all of type <b class="monospace">object</b>.</p>
<p>Unlike primitives, objects:</p>
<ul>
<li data-i18n="list-1">are compared by reference</li>
</ul>
<pre><code class="javascript hljs" data-trim>
"test" === "test"
▸ true
{ item: "test" } === { item: "test" }
▸ false
</code></pre>
<ul>
<li data-i18n="list-2">have a list of properties that are key-value pairs: key (String, Symbol) ⇔ value (any)</li>
</ul>
<pre><code class="javascript hljs" data-trim>
"item" in { item: "test" }
▸ true
Symbol.iterator in [1,2,3]
▸ true
</code></pre>
</section>
<section id="properties-listing">
<h3 class="flex-center" data-i18n="title">Objects: properties listing</h3>
<span class="ref" data-i18n="exo">Exercise 2</span>
<pre style="font-size: 90%"><code class="javascript hljs" data-trim>
let obj = { a: 1, b: 2 }
"a" in obj
▸ true
for(let key in obj) console.log(key, obj[key])
▸ a 1
▸ b 2
Object.keys(obj)
▸ ["a", "b"]
Object.values(obj)
▸ [1, 2]
Object.entries(obj)
▸ [ ["a", 1], ["b", 2] ]
</code></pre>
</section>
<section id="properties-assignment">
<h3 data-i18n="title">Objects: properties assignment</h3>
<span class="ref" data-i18n="exo">Exercise 3</span>
<pre><code class="javascript hljs" data-trim>
let obj = { key: "value" }
obj.a = 1;
obj["b"] = 2;
Object.assign(obj, { c: 3, d: 4}, {d: 5});
▸ {key: "value", a: 1, b: 2, c: 3, d: 5}
Object.fromEntries([ ['a', 1], ['b', 2]]);
▸ {a: 1, b: 2}
</code></pre>
<aside class="note info" data-i18n="note">
<b class="monospace">Object.assign</b> is very useful for assigning default values
or assigning a set of properties in a single statement
</aside>
</section>
<section id="property-descriptors" style="text-align: left;">
<h3 data-i18n="title">Property descriptors</h3>
<p data-i18n="text-1">Each property contains in addition to its value, a list of intrinsic parameters called <b>descriptors</b>:</p>
<ul>
<li><b class="monospace">value:</b> <em data-i18n="list-1">the value of the property</em></li>
<li><b class="monospace">enumerable: true</b> <em data-i18n="list-2">if the property is listed with Object.keys or for..in</em></li>
<li><b class="monospace">writable: true</b> <em data-i18n="list-3">if the value of the property can be modified</em></li>
<li><b class="monospace">configurable: true</b>: <em data-i18n="list-4">if the descriptor can be modified</em></li>
<li><b class="monospace">get</b>: <em data-i18n="list-5">optional function to use as getter</em></li>
<li><b class="monospace">set</b>: <em data-i18n="list-6">optional function to use as setter</em></li>
</ul>
<p data-i18n="text-2">A default assignment is enumerable, modifiable and configurable, without specific accessors or mutators.</p>
</section>
<section id="define-the-descriptors">
<h3 data-i18n="title">Define the descriptors</h3>
<span class="ref" data-i18n="exo">Exercise 4</span>
<p data-i18n="text"><b class="monospace">Object.defineProperty()</b> allows you to assign a property by specifying its descriptors</p>
<div class="flex-center">
<pre style="font-size: 80%"><code class="javascript hljs" data-trim>
const obj = {}
Object.defineProperty(obj, 'a', {
value: 1,
enumerable: true,
writable: false
});
Object.defineProperties(obj, {
b: { enumerable: true, value: 2 },
c: { get: function(){ return 3 } },
});
▸ {a: 1, b: 2}
</code></pre>
<img height="325px" src="./assets/img/Image7.png">
</div>
</section>
<section id="getters-setters">
<h3>Getters / Setters</h3>
<p data-i18n="text">Literal declaration for get / set descriptors:</p>
<pre><code class="javascript hljs" data-trim data-line-numbers>
const user = {
name: "Smith",
first: "John",
get fullName(){
return this.first + " " + this.name.toUpperCase()
},
set fullName(value){
this.first = value.split(" ")[0];
this.name = value.split(" ")[1];
}
}
</code></pre>
</section>
<!--<section id="delegated-properties">
<h3 data-i18n="title">Own and delegated properties</h3>
<p data-i18n="text-1">
The properties assigned to an object are called the <b>own properties</b> of the object.
But there are other types of properties, the <b>delegated properties</b>.
</p>
<img height="150" src="./assets/img/Image8.png">
<p data-i18n="text-2">These delegated properties are in fact the properties of the object's prototype (see Object Oriented section)</p>
<pre><code class="javascript hljs" data-trim>
obj.toString === Object.prototype.toString
▸ true
</code></pre>
</section>-->
<section id="scopes">
<h3 data-i18n="title">Scopes</h3>
<p data-i18n="text-1"><b>Scope</b> = perimeter from which variables can be accessed.</p>
<p data-i18n="text-2">At the highest level, we are in the global scope, accessible everywhere.</p>
<img height="300" src="./assets/img/Image9.png">
<p>
<span data-i18n="text-3">Accessible from global scope</span> <span class="green">(1)</span> : <span class="monospace">foo</span><br />
<span data-i18n="text-4">Accessible from function foo scope</span> <span class="red">(2)</span> : <span class="monospace">foo, a, b, bar</span><br />
<span data-i18n="text-5">Accessible from function bar scope</span> <span class="blue">(3)</span> : <span class="monospace">foo, a, b, bar, c</span><br />
</p>
</section>
<section id="blocs-scopes">
<h3 data-i18n="title">Block scopes</h3>
<p data-i18n="text-1">In addition to function scopes, some instructions create so-called <b>block scopes</b></p>
<p data-i18n="text-2">These are known as <b>block instructions:</b></p>
<div class="flex-center">
<pre><code class="javascript hljs" data-trim>
if(){ ... }
for(){ ... }
while(){ ... }
</code></pre>
<img height="150" src="./assets/img/Image10.png">
</div>
<p data-i18n="text-3">
A block scope can also be declared with <span class="monospace">{ ... }</span> <br />
<em>(not to be confused with a literal object declaration)</em>
</p>
</section>
<section id="var-let-const">
<h3 data-i18n="title">var, let or const?</h3>
<ul>
<li data-i18n="list-1"><b class="monospace">let</b> and <b class="monospace">const</b> appeared with ES2015</li>
<li data-i18n="list-2">they bind variables to <b>block scope</b> and not function scope like <code>var</code>, which is much more intuitive</li>
<li data-i18n="list-3"><b class="monospace">var</b> is no longer used since</li>
</ul>
<div class="flex-slide">
<img height="250" src="./assets/img/Image11.png">
<table>
<thead><tr><th></th><th>Scope</th><th>Re-assignable</th></tr></thead>
<tbody>
<tr><td class="monospace">var</td><td>function</td><td>yes</td></tr>
<tr><td class="monospace">let</td><td>block</td><td>yes</td></tr>
<tr><td class="monospace">const</td><td>block</td><td>no</td></tr>
</tbody>
</table>
</div>
<aside class="note info" data-i18n="note"><u>Recommended:</u> Use <code>const</code>, or <code>let</code> if you need to reassign</aside>
</section>
<section id="closures">
<h3 data-i18n="title">Closures</h3>
<p data-i18n="text-1">Scope of a function = local scope + parent scope = local variables + non-local variables</p>
<p data-i18n="text-2">
A function using <b>non-local variables</b> (defined in a parent scope) creates what is called a <b>closure</b>.
A closure must remember its parent scope in order to execute correctly.
</p>
<div class="cols-2">
<div class="note info" style="font-size: 100%; font-style: italic">
<p data-i18n="text-3"><b class="monospace">add5</b> retains its parent scope with <code>x=5</code></p>
<p data-i18n="text-4">This link prevents the garbage collector from dereferencing the local variable x at the end of the <b class="monospace">add</b> execution</p>
<p data-i18n="text-5"><b class="monospace">add5</b> creates a <b>closure</b></p>
</div>
<pre><code class="javascript hljs" data-trim>
function add(x) {
return function(y) {
return x + y;
}
}
const add5 = add(5);
add5(8)
▸ 13
</code></pre>
</div>
<aside class="note warning" data-i18n="note">
Unintentional closures are the most common source of <b>memory leaks</b> problems in web applications.
</aside>
</section>
<section id="using-closures">
<h3 data-i18n="title">Using closures to make "private" variables</h3>
<pre><code lass="javascript hljs" data-trim>
function getCounter() {
let value = 0;
return {
getValue(){ return value },
increment() { value += 1 }
}
}
</code></pre>
<p data-i18n="text-1">Outside the scope of the <b class="monospace">getCounter</b> function, the value variable will never be accessible.</p>
<p data-i18n="text-2">Only the <b class="monospace">getValue</b> and <b class="monospace">increment</b> closures allow access</p>
</section>
<section id="using-closures-2">
<h3 data-i18n="title">Using closures</h3>
<span class="ref" data-i18n="exo">Exercise 5</span>
<pre><code class="javascript hljs" data-trim>
for (var i = 1; i<=3; i++) {
setTimeout( function timer(){
console.log( i );
}, i*1000 );
}
▸ 4
▸ 4
▸ 4
</code></pre>
<aside class="note" data-i18n="note">
Note that closures retain a reference to the parent scope,<br>
and therefore access non-local variables by <b>reference</b>, not by value. <br>
<br>
The value of the non-local variables can therefore vary depending on when the function is called.
</aside>
</section>
<section class="left" id="functions-execution-context">
<h3 data-i18n="title">Functions and execution context</h3>
<p data-i18n="text-1">
Functions have some kind of hidden argument called <b>context</b> and assigned to the keyword <b class="monospace">this</b> inside the function body.
</p>
<p data-i18n="text-2">
When a function is called as a property of an object, we call it a <b>method</b> and that parent object is assigned as execution context of the method:
</p>
<pre><code class="javascript hljs" data-trim>
const obj = {
whoisthis(){ return this }
}
obj.whoisthis() === obj
▸ true
</code></pre>
<p data-i18n="text-3">
When a function is called by direct reference, it has no context, which means the <code>this</code> variable will be looked up in parent scope.
</p>
<p data-i18n="text-4">At the <b>root scope</b>, the context is defined as the <b>global context</b> (<code>Window</code> on browser)</p>
<img height="80" src="./assets/img/Image12.png">
</section>
<section id="functions-execution-context-2">
<h3 data-i18n="title">Functions and execution context</h3>
<p data-i18n="text-1">
The execution context can be changed with the methods
<span class="monospace">call</span>,
<span class="monospace">apply</span> and <span class="monospace">bind</span>
</p>
<pre style="font-size: 80%"><code class="javascript hljs" data-trim>
function calculateScore(score1, score2) {
return this.name + " score is " + (score1 + score2)
}
let alice = { name: "Alice" },
bob = { name: "Bob" };
calculateScore.call(alice, 7, 5)
▸ "Alice score is 12"
calculateScore.apply(bob, [4, 6])
▸ "Bob score is 10"
let getBobScore = calculateScore.bind(bob, 3, 5)
getBobScore()
▸ "Bob score is 8"
</code></pre>
<p data-i18n="text-2">These methods are available on all functions.</p>
</section>
<section id="primitives-wrappers">
<h3 data-i18n="title">Primitives wrappers</h3>
<p data-i18n="text-1">
<span class="monospace">String()</span>, <span class="monospace">Number()</span>,
<span class="monospace">Boolean()</span> are the object equivalent constructors of the corresponding
primitives, also called <b>primitive wrappers</b>
</p>
<p data-i18n="text-2">The switch between object and primitive is automatic:</p>
<div class="flex-center">
<div style="flex-basis: 100%;">
<p class="center">Primitive → Object</p>
<pre style="font-size: 80%"><code class="javascript hljs" data-trim>
let str = "abc";
str.length; // 3
str.toUpperCase(); // "ABC"
</code></pre>
</div>
<div style="flex-basis: 100%;">
<p class="center">Object → Primitive</p>
<pre style="font-size: 65%"><code class="javascript hljs" data-trim>
let Str = new String( "abc" ),
str = Str.valueOf() // ou "" + Str
let N = new Number( 42 ),
n = N.valueOf() // +N
console.log(Str, typeof Str) // "abc", "object"
console.log(str, typeof str) // "abc", "string"
console.log(N, typeof N) // 42, "object"
console.log(n, typeof n) // 42, "number"
</code></pre>
</div>
</div>
<aside class="note" data-i18n="note">Generally, primitive wrappers constructors are not called explicitly</aside>
</section>
<section id="type-conversions">
<h3 data-i18n="title">Type conversions (cast)</h3>
<p data-i18n="text">There are 3 types of automatic type conversions in JS:</p>
<table class="slide-array" style="width: 80%">
<thead><tr><th></th><th>Explicit cast</th><th>Implicit cast</th><th>Cast operators</th></tr></thead>
<tbody>
<tr>
<td>ToString</td>
<td><pre><code class="javascript hljs" data-trim>
String(42)
▸ "42"
</code></pre></td>
<td><pre><code class="javascript hljs" data-trim>
12 + "34"
▸ "1234"
</code></pre></td>
<td><pre><code> + </code></pre></td>
</tr>
<tr>
<td>ToNumber</td>
<td><pre><code class="javascript hljs" data-trim>
Number("42")
▸ 42
</code></pre></td>
<td><pre><code class="javascript hljs" data-trim>
+"42"
▸ 42
</code></pre></td>
<td>
<pre><code class="javascript hljs" data-trim>- + * / %</code></pre>
<pre><code class="javascript hljs" data-trim>> < <= >=</code></pre>
<pre><code class="javascript hljs" data-trim>| & ^ ~</code></pre>
</td>
</tr>
<tr>
<td>ToBoolean</td>
<td><pre><code class="javascript hljs" data-trim>
Boolean(1)
▸ true
</code></pre></td>
<td><pre><code class="javascript hljs" data-trim>
!!1
▸ true
</code></pre></td>
<td>
<pre><code class="javascript hljs" data-trim>|| && !</code></pre>
</td>
</tr>
</tbody>
</table>
<aside class="note info" data-i18n="note"><span style="font-weight: bold; text-decoration: underline">Recommended</span>: cast explicitly and as soon as possible</aside>
</section>
<section id="equals-operator">
<h3 data-i18n="title">== or === ?</h3>
<p data-i18n="text">
== weak equality (with conversion) <br />
=== strict equality (without conversion)
</p>
<img height="250" src="./assets/img/Image13.png">
<div class="note info" data-i18n="note">
The rules for conversion with weak equality are <a href="https://eqeq.js.org/" target="_blank">very complex</a>. <br />
It's recommended to use strict equality, which is more predictable.
</div>
</section>
<section class="quizz" id="implicit-casts">
<h3 data-i18n="title">Implicit casts</h3>
<span class="ref">Quiz</span>
<div class="flex-slide">
<div style="flex-basis: 100%;">
<p><pre class="fragment fade"><code>"0"-0</code></pre><pre class="fragment fade"><code>▸ 0</code></pre></p>
<p><pre class="fragment fade"><code>"0"+0</code></pre><pre class="fragment fade"><code>▸ "00"</code></pre></p>
<p><pre class="fragment fade"><code>+"1"+2</code></pre><pre class="fragment fade"><code>▸ 3</code></pre></p>
<p><pre class="fragment fade"><code>+1+"2"</code></pre><pre class="fragment fade"><code>▸ "12"</code></pre></p>
</div>
<div style="flex-basis: 100%;">
<p><pre class="fragment fade"><code>!"0"</code></pre><pre class="fragment fade"><code>▸ false</code></pre></p>
<p><pre class="fragment fade"><code>!+"0"</code></pre><pre class="fragment fade"><code>▸ true</code></pre></p>
<p><pre class="fragment fade"><code>+!"0"</code></pre><pre class="fragment fade"><code>▸ 0</code></pre></p>
<p><pre class="fragment fade"><code>+"test"</code></pre><pre class="fragment fade"><code>▸ NaN</code></pre></p>
</div>
<div style="flex-basis: 100%;">
<p><pre class="fragment fade"><code>+[1]</code></pre><pre class="fragment fade"><code>▸ 1</code></pre></p>
<p><pre class="fragment fade"><code>+[1,2]</code></pre><pre class="fragment fade"><code>▸ NaN</code></pre></p>
<p><pre class="fragment fade"><code>![]</code></pre><pre class="fragment fade"><code>▸ false</code></pre></p>
<p><pre class="fragment fade"><code>!+[]</code></pre><pre class="fragment fade"><code>▸ true</code></pre></p>
</div>
</div>
</section>
<section id="logical-operators">
<h3 data-i18n="title">Logical operators</h3>
<p data-i18n="text-1">Operands are casted to booleans to evaluate the operator,<br>
but it is the original values that are returned.</p>
<pre><code class="javascript hljs" data-trim>
let a = 42,
b = "abc",
c = null;
a || b; // 42
a && b; // "abc"
c || b; // "abc"
c && b; // null
</code></pre>
<aside class="monospace note info">
a || b ⇔ a ? a : b <br>
a && b ⇔ a ? b : a
</aside>
<p data-i18n="text-2">
If a value is implicitely casted as Boolean as <code>true</code>,
it is said to be <em>truthy</em>, otherwise <em>falsey</em>
</p>
</section>
<section id="logical-operators-2">
<h3 data-i18n="title">Logical operators</h3>
<span class="ref" data-i18n="exo">Exercise 6</span>
<p data-i18n="text-1">
The operators <span class="monospace">||</span> and <span class="monospace">&&</span>,
the ternary conditions <span class="monospace">?:</span> and <br>
<span class="monospace">if else</span> statements execute only one operand here:
</p>
<div class="flex-center">
<pre><code class="javascript hljs" data-trim>if(0) log('A'); else log('B');</code></pre>
<p class="fragment fade">B</p>
</div>
<div class="flex-center">
<pre><code class="javascript hljs" data-trim>undefined ? log('C') : log('D');</code></pre>
<p class="fragment fade">D</p>
</div>
<div class="flex-center">
<pre><code class="javascript hljs" data-trim>true && log('E'); false && log('F');</code></pre>
<p class="fragment fade">E</p>
</div>
<div class="flex-center">
<pre><code class="javascript hljs" data-trim>true || log('G'); false || log('H');</code></pre>
<p class="fragment fade">H</p>
</div>
<p data-i18n="text-2">
The <span class="monospace">&&</span> operator has priority over <span class="monospace">||</span>
which has priority over <span class="monospace">?:</span>
</p>
</section>
</section>
<section>
<section id="chapter-3">
<h1 data-i18n="title">Upgrade to modern JS: ES2015+</h1>
<p data-i18n="description">Exercises 7 to 11</p>
</section>
<section id="syntactic-key-value">
<h3 data-i18n="title">Syntactic sugar: key and value</h3>
<pre><code class="javascript hljs" data-trim>
let foo = "foo", bar = "bar"
const obj = { foo, bar } // equivalent to { foo: foo, bar: bar }
obj
▸ {foo: "foo", bar: "bar"}
</code></pre>
</section>
<section id="syntactic-calculated-keys">
<h3 data-i18n="title">Syntactic sugar: calculated keys</h3>
<pre><code class="javascript hljs" data-trim>
let foo = "foo", bar = "bar"
const obj = {
[foo]: [bar],
[foo+bar]: foo+bar
}
/* equivalent to
obj[foo] = [bar]
obj[foo+bar] = foo + bar
*/
</code></pre>
<pre><code class="javascript hljs" data-trim>
obj
▾ {foo: Array(1), foobar: "foobar"}
▸ foo: ["bar"]
foobar: "foobar"
</code></pre>
</section>
<section class="left" id="arrow-functions">
<h3 data-i18n="title">Arrow functions</h3>
<span class="ref" data-i18n="exo">Exercise 7</span>
<pre><code class="javascript hljs" data-trim>
const f1 = () => {},
f2 = x => x*2,
f3 = (a,b,c) => { return a+b+c }
</code></pre>
<p data-i18n="text-1">Functions declared with this shortened syntax have specific features:</p>
<ul>
<li data-i18n="list-1">Has no execution context <br>
<span class="grey">(the value of <span class="monospace">this</span> is looked up in the parent scope)</span></li>
<li data-i18n="list-2">Cannot be called as a constructor (with operator <code style="color: orange">new</code>)</li>
</ul>
<p data-i18n="text-2">Short and effective, they encourage a functional, no-frills style <em>(no closure)</em>.</p>
<div class="note info" data-i18n="note">Arrow functions are recommended for all non-method functions</div>
</section>
<section id="default-parameters">
<h3 data-i18n="title">Default parameters</h3>
<p data-i18n="text-1">Assigns a default value if argument is <span class="monospace">undefined</span></p>
<pre><code class="javascript hljs" data-trim>
function f(a, b=1, c=2) { return a + b + c }
f(1, 2, 3)
▸ 6
f(1)
▸ 4
f(1, undefined, undefined)
▸ 4
f(1, null, null)
▸ 1
</code></pre>
<p data-i18n="text-2">Also works with the arrow functions:</p>
<pre><code class="javascript hljs" data-trim>const doSomething = (params = {}) => {}</code></pre>
</section>
<section class="left" id="default-parameters-2">
<h3 data-i18n="title">Use cases of default parameters values</h3>
<ul>
<li data-i18n="list-1">Can be expressions using other arguments or variables in the scope.</li>
<li data-i18n="list-2">If the default value is an expression, it will only be executed if necessary</li>
</ul>
<pre style="font-size: 80%"><code class="javascript hljs" data-trim>
function doSomething(data, config = retrieveDefaultConfig(data)) {
// retrieveDefaultConfig will be called only if config is undefined
}
const required = name => throw new Error("Missing required parameter: "+ name)
const findUsersByRole = (role = required('role')) => { }
</code></pre>
</section>
<section id="spread-rest">
<h3 data-i18n="title">Operators Spread / Rest</h3>
<p data-i18n="text">Spread: converts an iterable object into an argument list</p>
<pre><code class="javascript hljs" data-trim>
const sum = (a,b,c) => a+b+c;
sum(...[1,2,3])
▸ 6
sum(1, ...[2,3])
▸ 6
[1, ...[2,3], 4]
▸ [1,2,3,4]
</code></pre>
<pre><code class="javascript hljs" data-trim>const cloneArray = arr => [...arr]</code></pre>
</section>
<section id="spread-rest-2">
<h3 data-i18n="title">Operators Spread / Rest</h3>
<p data-i18n="text">Rest : converts a list of arguments into an Array</p>
<pre><code class="javascript hljs" data-trim>
const sum = (...args) => args.reduce((a,b) => a+b, 0);
sum(1,2,3,4)
▸ 10
function addToList(list, ...args) { // rest
list.push(...args) // spread
}
</code></pre>
</section>
<section class="left" id="spread-rest-3">
<h3 data-i18n="title">Operators Spread / Rest</h3>
<span class="ref" data-i18n="exo">Exercise 8</span>
<p>Object properties (ES2018)</p>
<p data-i18n="text">Only works with enumerable properties</p>
<ul>
<li>Rest properties:</li>
</ul>
<pre><code class="javascript hljs" data-trim>
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
x; // 1
y; // 2
z; // { a: 3, b: 4 }
</code></pre>
<ul>
<li>Spread properties:</li>
</ul>
<pre><code class="javascript hljs" data-trim>
let n = { x, y, ...z };
n; // { x: 1, y: 2, a: 3, b: 4}
</code></pre>
</section>
<section id="destructuring">
<h3 data-i18n="title">Destructuring</h3>