-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
1322 lines (1179 loc) · 53.4 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>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>javascript obstacles</title>
<style type="text/css">
body {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
color: #222;
font-size: 100%;
}
.slide {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
background-color: #f7f7f7;
}
.slide-content {
width: 800px;
height: 600px;
overflow: hidden;
margin: 80px auto 0 auto;
padding: 30px;
font-weight: 200;
font-size: 200%;
line-height: 1.375;
}
.controls {
position: absolute;
bottom: 20px;
left: 20px;
}
.arrow {
width: 0; height: 0;
border: 30px solid #333;
float: left;
margin-right: 30px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.prev {
border-top-color: transparent;
border-bottom-color: transparent;
border-left-color: transparent;
border-left-width: 0;
border-right-width: 50px;
}
.next {
border-top-color: transparent;
border-bottom-color: transparent;
border-right-color: transparent;
border-left-width: 50px;
border-right-width: 0;
}
.prev:hover {
border-right-color: #888;
cursor: pointer;
}
.next:hover {
border-left-color: #888;
cursor: pointer;
}
h1 {
font-size: 300%;
line-height: 1.2;
text-align: center;
margin: 170px 0 0;
}
h2 {
font-size: 100%;
line-height: 1.2;
margin: 5px 0;
text-align: center;
font-weight: 200;
}
h3 {
font-size: 140%;
line-height: 1.2;
border-bottom: 1px solid #aaa;
margin: 0;
padding-bottom: 15px;
}
ul {
padding: 20px 0 0 60px;
font-weight: 200;
line-height: 1.375;
}
.author h1 {
font-size: 170%;
font-weight: 200;
text-align: center;
margin-bottom: 30px;
}
.author h3 {
font-weight: 100;
text-align: center;
font-size: 95%;
border: none;
}
a {
text-decoration: none;
color: #44a4dd;
}
a:hover {
color: #66b5ff;
}
pre {
font-size: 60%;
line-height: 1.3;
}
.progress {
position: fixed;
top: 0; left: 0; right: 0;
height: 3px;
}
.progress-bar {
width: 0%;
height: 3px;
background-color: #b4b4b4;
-webkit-transition: width 0.05s ease-out;
-moz-transition: width 0.05s ease-out;
-o-transition: width 0.05s ease-out;
transition: width 0.05s ease-out;
}
.hidden {
display: none;
}
@media (max-width: 850px) {
body {
font-size: 70%;
}
.slide-content {
width: auto;
}
img {
width: 100%;
}
h1 {
margin-top: 120px;
}
.prev, .prev:hover {
border-right-color: rgba(135, 135, 135, 0.5);
}
.next, .next:hover {
border-left-color: rgba(135, 135, 135, 0.5);
}
}
@media (max-width: 480px) {
body {
font-size: 50%;
overflow: hidden;
}
.slide-content {
padding: 10px;
margin-top: 10px;
height: 340px;
}
h1 {
margin-top: 50px;
}
ul {
padding-left: 25px;
}
}
@media print {
* {
-webkit-print-color-adjust: exact;
}
@page {
size: letter;
}
.hidden {
display: inline;
}
html {
width: 100%;
height: 100%;
overflow: visible;
}
body {
margin: 0 auto !important;
border: 0;
padding: 0;
float: none !important;
overflow: visible;
background: none !important;
font-size: 52%;
}
.progress, .controls {
display: none;
}
.slide {
position: static;
}
.slide-content {
border: 1px solid #222;
margin-top: 0;
margin-bottom: 40px;
height: 3.5in;
overflow: visible;
}
.slide:nth-child(even) {
/* 2 slides per page */
page-break-before: always;
}
}
/*
github.com style (c) Vasily Polovnyov <[email protected]>
*/
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}
.hljs-comment,
.hljs-quote {
color: #998;
font-style: italic;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-subst {
color: #333;
font-weight: bold;
}
.hljs-number,
.hljs-literal,
.hljs-variable,
.hljs-template-variable,
.hljs-tag .hljs-attr {
color: #008080;
}
.hljs-string,
.hljs-doctag {
color: #d14;
}
.hljs-title,
.hljs-section,
.hljs-selector-id {
color: #900;
font-weight: bold;
}
.hljs-subst {
font-weight: normal;
}
.hljs-type,
.hljs-class .hljs-title {
color: #458;
font-weight: bold;
}
.hljs-tag,
.hljs-name,
.hljs-attribute {
color: #000080;
font-weight: normal;
}
.hljs-regexp,
.hljs-link {
color: #009926;
}
.hljs-symbol,
.hljs-bullet {
color: #990073;
}
.hljs-built_in,
.hljs-builtin-name {
color: #0086b3;
}
.hljs-meta {
color: #999;
font-weight: bold;
}
.hljs-deletion {
background: #fdd;
}
.hljs-addition {
background: #dfd;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
blockquote {
padding-left: 20px;
margin-left: 0;
margin-right: 0;
background: #ddd;
border-left: 5px solid #ccc;
border-radius: 0 3px 3px 0;
}
blockquote p {
padding: .5em;
color: #272822;
}
p {
display: block;
-webkit-margin-before: 0.9em;
-webkit-margin-after: 0.9em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
}
pre {
font-size: 90%;
line-height: 1.2;
}
ul {
padding: 0 0 0 40px;
font-weight: 200;
line-height: 1.6;
}
.progress {
z-index: 1000;
}
.progress-bar {
height: 5px;
}
img {
width: 100%
}
#slide-1 img {
width: auto;
}
body, .slide {
background-color: #eee;
}
.slide-content {
margin-top: 20px;
width: auto;
max-width: 900px;
height: auto;
overflow: auto;
z-index: 1;
}
.arrow {
position: fixed;
bottom: 70px;
margin: 0;
padding: 0;
border: none;
opacity: 0.3;
transform: scale(0.8);
}
@media (max-width: 850px) {
.arrow {
opacity: 0.2;
bottom: 60px;
transform: scale(0.7);
}
}
@media (max-width: 480px) {
.arrow {
opacity: 0.1;
bottom: 52px;
transform: scale(0.56);
}
}
.prev {
left: 0;
border: none;
}
.prev:before {
float: left;
content: url(img/left.png);
}
.next {
right: 0;
border: none;
}
.next:before {
float: right;
content: url(img/right.png);
}
</style>
</head>
<body>
<div class="progress">
<div class="progress-bar"></div>
</div>
<div class="slide" id="slide-1">
<section class="slide-content"><h1 id="javascript-obstacles">javascript-obstacles</h1>
<blockquote>
<p><a href="http://intesso.github.io/javascript-obstacles">http://intesso.github.io/javascript-obstacles</a></p>
</blockquote>
<p>feedback and pull requests welcome on <a href="https://github.com/intesso/javascript-obstacles">GitHub </a><a href="https://github.com/intesso/javascript-obstacles"><img src="img/github.png" alt="github"></a></p>
<p><em>navigate with left, right keys</em></p>
</section>
</div>
<div class="slide hidden" id="slide-2">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Functions are first class citizens</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test</span> (<span class="hljs-params"></span>) </span>{};
<span class="hljs-keyword">typeof</span> test === <span class="hljs-string">'function'</span>;
<span class="hljs-keyword">typeof</span> test.prototype === <span class="hljs-string">'object'</span>;
test.one = <span class="hljs-number">1</span>; <span class="hljs-comment">// functions can have properties</span>
<span class="hljs-built_in">console</span>.log(test.one); <span class="hljs-comment">// -> 1</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-3">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Object properties can be named almost everything</p>
</blockquote>
<p><em>but you can only access them with strings in square brackets</em></p>
<pre><code class="lang-js">var obj = {};
obj.a = 'A';
console.log(obj.a) // -> 'A'
// same as
obj['a'] = 'A';
console.log(obj['a']) // -> 'A'
// names that DON'T work with dot notation
obj.1 = 1; // Uncaught SyntaxError: Unexpected number
obj.a-b = 'ab'; // Uncaught ReferenceError: b is not defined
obj.@#!? = 'wat?'; // SyntaxError: Invalid or unexpected token
obj.┬──┬ ノ( ゜-゜ノ) = 'mood'; // SyntaxError: Invalid or unexpected token
// but work as strings in square brackets
obj['1'] = 1;
obj['a-b'] = 'ab';
obj['@#!?'] = 'wat?';
obj['┬──┬ ノ( ゜-゜ノ)'] = 'mood';</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-4">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Callback Functions</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-comment">// dom stuff</span>
<span class="hljs-keyword">var</span> body = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'body'</span>);
<span class="hljs-comment">// event listener</span>
body.addEventListener(<span class="hljs-string">'click'</span>, myCallback);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myCallback</span>(<span class="hljs-params">e</span>) </span>{
confirm(<span class="hljs-string">"DONT CLICK!"</span>);
}
<span class="hljs-comment">// same in green, but with nested function</span>
body.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myCallback</span>(<span class="hljs-params">e</span>) </span>{
confirm(<span class="hljs-string">"DONT CLICK!"</span>);
});</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-5">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p><a href="http://callbackhell.com/">Callback Hell</a> is when your code looks like this</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">i, callback</span>) </span>{
i = i+<span class="hljs-number">1</span>;
setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'count'</span>, i);
callback(i);
}, <span class="hljs-number">100</span>);
}
add(<span class="hljs-number">1</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">i</span>)</span>{
add(i, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">i</span>) </span>{
add(i, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">i</span>)</span>{
add(i, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">i</span>)</span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'done'</span>, i);
});
});
});
});</code></pre>
<p><strong>Ways out: name and reference functions, use libraries like <a href="https://github.com/caolan/async">async</a>, <a href="https://www.npmjs.com/package/run-series">run-series</a>, <a href="https://www.npmjs.com/package/run-parallel">run-parallel</a> and modularize your stuff</strong></p>
</section>
</div>
<div class="slide hidden" id="slide-6">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Functions are first class citizens</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test</span> (<span class="hljs-params"></span>) </span>{};
<span class="hljs-keyword">typeof</span> test === <span class="hljs-string">'function'</span>;
<span class="hljs-keyword">typeof</span> test.prototype === <span class="hljs-string">'object'</span>;
test.one = <span class="hljs-number">1</span>; <span class="hljs-comment">// functions can have properties</span>
<span class="hljs-built_in">console</span>.log(test.one); <span class="hljs-comment">// -> 1</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-7">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p><strong>Statements</strong> are actions</p>
</blockquote>
<ul>
<li>are <strong>NOT allowed</strong> in ES6 Template Strings</li>
</ul>
<p>examples: </p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span> {}
<span class="hljs-keyword">else</span> {}
<span class="hljs-keyword">for</span> () {}
<span class="hljs-keyword">do</span> {} <span class="hljs-keyword">while</span> ()
<span class="hljs-keyword">while</span> {}</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-8">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p><strong>Expressions</strong> produce a value, can be assigned to a var</p>
</blockquote>
<ul>
<li>are <strong>allowed</strong> in ES6 Template Strings</li>
<li>are allowed where ever a statement is allowed</li>
</ul>
<p>examples:</p>
<pre><code class="lang-js">a
a ? <span class="hljs-number">1</span> ? <span class="hljs-number">0</span>
a = <span class="hljs-number">2</span>
{ a : <span class="hljs-number">3</span>}
[ <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span> ]
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">a</span> (<span class="hljs-params"></span>) </span>{}
a = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{}</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-9">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Prototypal Inheritance</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-comment">// constructor function</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Hello</span> (<span class="hljs-params">announcement</span>) </span>{
<span class="hljs-keyword">if</span> (!(<span class="hljs-keyword">this</span> <span class="hljs-keyword">instanceof</span> Hello)) <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Hello(announcement);
<span class="hljs-keyword">this</span>.announcement = announcement;
}
<span class="hljs-comment">// method</span>
Hello.prototype.world = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.announcement, <span class="hljs-string">'hello world'</span>);
};
<span class="hljs-comment">// instantiating an object</span>
<span class="hljs-keyword">var</span> hello = Hello(); <span class="hljs-comment">// or new Hello();</span>
hello.world(); <span class="hljs-comment">// prints out: hello world</span>
<span class="hljs-comment">// method added (on the fly)</span>
Hello.prototype.ch = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.announcement, <span class="hljs-string">'sali duu'</span>);
};
hello.ch(); <span class="hljs-comment">// prints out: sali duu</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-10">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>problem with <code>this</code></p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-comment">// constructor function</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Hello</span> (<span class="hljs-params">announcement</span>) </span>{
<span class="hljs-keyword">if</span> (!(<span class="hljs-keyword">this</span> <span class="hljs-keyword">instanceof</span> Hello)) <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Hello(announcement);
<span class="hljs-keyword">this</span>.announcement = announcement;
}
<span class="hljs-comment">// wrong method</span>
Hello.prototype.world = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-comment">// !!! this is `undefined` in the inner function</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.announcement, <span class="hljs-string">'hello world'</span>);
}, <span class="hljs-number">1000</span>);
};
<span class="hljs-comment">// correct method</span>
Hello.prototype.world = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-comment">// save `this` to use in inner function</span>
<span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>;
setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-comment">// works as expected</span>
<span class="hljs-built_in">console</span>.log(self.announcement, <span class="hljs-string">'hello world'</span>);
}, <span class="hljs-number">1000</span>);
};
Hello(<span class="hljs-string">'I SAY:'</span>).world();</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-11">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Grasp Functional Beauty</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">repeater</span> (<span class="hljs-params">who, what</span>) </span>{
<span class="hljs-keyword">var</span> text = <span class="hljs-string">'said'</span>;
<span class="hljs-comment">// returning the inner function creates a closure at run time</span>
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">repeat</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(who, text, what);
}
}
<span class="hljs-keyword">var</span> i = repeater(<span class="hljs-string">'I'</span>,<span class="hljs-string">'hello'</span>);
<span class="hljs-keyword">var</span> you = repeater(<span class="hljs-string">'you'</span>,<span class="hljs-string">'world'</span>);
i(); <span class="hljs-comment">// I said hello</span>
you(); <span class="hljs-comment">// you said world</span>
repeater(<span class="hljs-string">'everyone'</span>, <span class="hljs-string">'yay!'</span>)(); <span class="hljs-comment">//everyone said yay!</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-12">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>problem with <code>for</code> loop</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-comment">// works as expected</span>
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'i'</span>, i);
<span class="hljs-comment">//0 1 2 ... 9</span>
} <span class="hljs-comment">// **only use for loop for synchronous actions**</span>
<span class="hljs-comment">// hmmm not really what we wanted</span>
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {
setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'i'</span>, i)}, <span class="hljs-number">100</span>);
<span class="hljs-comment">//10 10 10 ... 10</span>
} <span class="hljs-comment">// **don't use for loop for asynchronous actions**</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-13">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>solutions to <code>for</code> loop problems</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-comment">// Array.forEach function</span>
<span class="hljs-keyword">var</span> arr = [];
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {
arr.push(i);
}
arr.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">i</span>)</span>{
setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'i'</span>, i)}, <span class="hljs-number">100</span>);
<span class="hljs-comment">//0 1 2 ... 9</span>
}); <span class="hljs-comment">// **Array.forEach works well for asynchronous actions**</span>
<span class="hljs-comment">// anonymous immediately invoked function expression (IIFE)</span>
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">10</span>; i++) {
(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">i</span>)</span>{
setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'i'</span>, i)}, <span class="hljs-number">100</span>);
<span class="hljs-comment">//0 1 2 ... 9</span>
})(i);
}</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-14">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Variable Hoisting</p>
</blockquote>
<p><em>Variables can be used, before they were declared. Variable declarations (not initialization) are moved to the top of the function or global scope (hoisted). It is therefore recommended to declare the variables at the top of the function or global scope</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> myvar = <span class="hljs-string">'hello outer'</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myfunction</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(myvar); <span class="hljs-comment">// undefined</span>
<span class="hljs-keyword">var</span> myvar = <span class="hljs-string">'hello inner'</span>;
}
myfunction();</code></pre>
<p><em>myfunction above is equivalent to this:</em></p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myfunction</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">var</span> myvar; <span class="hljs-comment">// declaration</span>
<span class="hljs-built_in">console</span>.log(myvar); <span class="hljs-comment">// undefined</span>
myvar = <span class="hljs-string">'hello inner'</span>; <span class="hljs-comment">// initializations</span>
}</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-15">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Function Hoisting</p>
</blockquote>
<p><em>Function definitions are also hoisted</em> </p>
<p><em>Function declarations are NOT hoisted: <code>var myfunc = function(){}</code></em></p>
<pre><code class="lang-js">hoisted(); <span class="hljs-comment">// works</span>
notHoisted(); <span class="hljs-comment">// TypeError: notHoisted is not a function</span>
noHoist(); <span class="hljs-comment">// ReferenceError: noHoist is not defined</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hoisted</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'hoisted'</span>);
}
<span class="hljs-keyword">var</span> notHoisted = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">noHoist</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'notHoisted'</span>);
}</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-16">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>NO Block Scope</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> x = <span class="hljs-string">'hallo'</span>;
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'hallo'</span>
<span class="hljs-keyword">if</span> (<span class="hljs-literal">true</span>) { <span class="hljs-comment">// block</span>
<span class="hljs-keyword">var</span> x = <span class="hljs-string">'welt'</span>; <span class="hljs-comment">// overwrites var x outside block</span>
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'welt'</span>
}
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'welt'</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-17">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>but Function Scope</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> x = <span class="hljs-string">'hallo'</span>;
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'hallo'</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">welt</span> (<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// function scope</span>
<span class="hljs-keyword">if</span> (<span class="hljs-literal">true</span>) {
<span class="hljs-keyword">var</span> x = <span class="hljs-string">'welt'</span>; <span class="hljs-comment">// own declaration of x inside function</span>
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'welt'</span>
}
}
welt();
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'hallo'</span></code></pre>
<p><strong>JavaScript has function scope (no block scope)</strong></p>
</section>
</div>
<div class="slide hidden" id="slide-18">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Function Scope, same in green</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> x = <span class="hljs-string">'hallo'</span>;
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'hallo'</span>
(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// function scope</span>
<span class="hljs-keyword">if</span> (<span class="hljs-literal">true</span>) {
<span class="hljs-keyword">var</span> x = <span class="hljs-string">'welt'</span>; <span class="hljs-comment">// own declaration of x inside function</span>
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'welt'</span>
}
})();
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// 'hallo'</span></code></pre>
<p><em>with immediately invoked function expression (IIFE)</em></p>
</section>
</div>
<div class="slide hidden" id="slide-19">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Immediately-Invoked Function Expression</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-comment">// functions can be declared in brackets (function(){})</span>
<span class="hljs-comment">// and be Immediately-Invoked with following function call ()</span>
(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'hallo welt'</span>);
})();
<span class="hljs-comment">// this can be used to get function scope and alias names</span>
<span class="hljs-comment">// often used with jQuery</span>
(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">$</span>) </span>{
<span class="hljs-built_in">console</span>.log($(<span class="hljs-string">'body'</span>));
}(jQuery));</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-20">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>NO Dynamic Scope in JavaScript</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">repeat</span> (<span class="hljs-params">who, what</span>) </span>{
<span class="hljs-keyword">var</span> text = <span class="hljs-string">'said'</span>;
repeater(who, what);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">repeater</span>(<span class="hljs-params">who, what</span>) </span>{
<span class="hljs-built_in">console</span>.log(who, text, what); <span class="hljs-comment">// ReferenceError: text is not defined</span>
}
<span class="hljs-comment">// the repeater function wasn't defined inside the</span>
<span class="hljs-comment">// caller function that has the text variable defined.</span>
repeat(<span class="hljs-string">'I'</span>, <span class="hljs-string">', hey YOU!'</span>);</code></pre>
<p><strong>JavaScript does NOT support dynamic scope, where the variable resolution happens in the scope where the function is called at run time.</strong></p>
</section>
</div>
<div class="slide hidden" id="slide-21">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>BUT Lexical Scope</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">whoSaysWhat</span> (<span class="hljs-params">who, what</span>) </span>{
<span class="hljs-keyword">var</span> text = <span class="hljs-string">'says'</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">say</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// inner function can access the outer scope</span>
<span class="hljs-built_in">console</span>.log(who, text, what); <span class="hljs-comment">// text is declared in parent function</span>
}
say();
}
whoSaysWhat(<span class="hljs-string">'elephant'</span>, <span class="hljs-string">'trörööö'</span>); <span class="hljs-comment">// elephant says trörööö</span></code></pre>
<p><strong>The scope of a variable is statically defined by its location in the source code (not dynamically at run time). This is called lexical scope.</strong></p>
<p><em>Note: inner functions can access variables from outer functions.</em></p>
</section>
</div>
<div class="slide hidden" id="slide-22">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>Closures</p>
</blockquote>
<p><em>"Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created." <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Closures">quote from mozilla</a></em></p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">repeater</span> (<span class="hljs-params">who, what</span>) </span>{
<span class="hljs-keyword">var</span> text = <span class="hljs-string">'said'</span>;
<span class="hljs-comment">// inner function is returned and creates a creates closure</span>
<span class="hljs-comment">// where the returned function has access to the outer scope</span>
<span class="hljs-comment">// it was defined in, even after the outer function has terminated.</span>
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">repeat</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(who, text, what);
}
}
repeater(<span class="hljs-string">'everyone'</span>, <span class="hljs-string">'yay!'</span>)(); <span class="hljs-comment">//everyone said yay!</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-23">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<p>different JavaScript / ECMAScript / node.js Versions</p>
</blockquote>
<ul>
<li>ES5 :: <strong>node.js</strong> <= 0.x :: <a href="http://caniuse.com/#search=es5"><strong>browser</strong> caniuse +</a></li>
<li>ES6 (ECMAScript 2015) :: <strong>io.js, node.js</strong> > 0.x :: <a href="http://caniuse.com/#search=es6"><strong>browser</strong> caniuse ~</a></li>
</ul>
<p><em>Transpilers might be helpful in using the next version of JavaScript even it is not fully supported by the engine (browser or node.js) by transpiling it into the currently supported version, but they also come at a cost and add another complexity.</em></p>
</section>
</div>
<div class="slide hidden" id="slide-24">
<section class="slide-content"><h3 id="js-obstacles">js obstacles</h3>
<blockquote>
<ul>
<li>The JavaScript universe is changing very fast ....</li>
<li>Many tools/frameworks to pick from</li>
<li><strong>It's hard to choose the <em>right</em> Tool</strong> for the job</li>
</ul>
</blockquote>
<p><em>one can easily forget the original problem that needed to be solved by adding new fancy stuff and solving problems that were not there in the first place</em></p>
</section>
</div>
<div class="slide hidden" id="slide-25">
<section class="slide-content"><h3 id="js-obstacles-with-es6">js obstacles with ES6</h3>
<blockquote>
<p>Variable Hoisting</p>
</blockquote>
<p><em><code>let</code> and <code>const</code> variables are not hoisted</em></p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">l</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">typeof</span> foo; <span class="hljs-comment">// ReferenceError: foo is not defined</span>
<span class="hljs-keyword">let</span> foo = <span class="hljs-string">'bar'</span>;
}
l();
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">c</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">typeof</span> foo; <span class="hljs-comment">// ReferenceError: foo is not defined</span>
<span class="hljs-keyword">const</span> foo = <span class="hljs-string">'bar'</span>;
}
c();
<span class="hljs-keyword">typeof</span> foo; <span class="hljs-comment">// 'undefined'</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-26">
<section class="slide-content"><h3 id="js-obstacles-with-es6">js obstacles with ES6</h3>
<blockquote>
<p>Variable Hoisting</p>
</blockquote>
<p><em>checking with <strong>typeof</strong> is NOT SAFE anymore!</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">typeof</span> undefinedVariable <span class="hljs-comment">// safe: -> 'undefined'</span>
bar = <span class="hljs-string">'milkbar'</span>; <span class="hljs-comment">// error: not defined</span>
<span class="hljs-keyword">typeof</span> bar <span class="hljs-comment">// IMPORTANT: typeof is not safe anymore and throws an error!</span>
<span class="hljs-keyword">let</span> bar = <span class="hljs-string">''</span>; <span class="hljs-comment">// because of this line (same for `let` and `const`)</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-27">
<section class="slide-content"><h3 id="js-obstacles-with-es6">js obstacles with ES6</h3>
<blockquote>
<p>Variable Scope</p>
</blockquote>
<p><em>inner functions/blocks can access <code>var</code>, <code>let</code> and <code>const</code> variables from outer scope/block</em></p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myfunction</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(myvar); <span class="hljs-comment">// -> 'hello var'</span>
<span class="hljs-built_in">console</span>.log(mylet); <span class="hljs-comment">// -> 'hello let'</span>
<span class="hljs-built_in">console</span>.log(myconst); <span class="hljs-comment">// -> 'HELLO_CONST'</span>
}
<span class="hljs-keyword">var</span> myvar = <span class="hljs-string">'hello var'</span>
<span class="hljs-keyword">let</span> mylet = <span class="hljs-string">'hello let'</span>;
<span class="hljs-keyword">const</span> myconst = <span class="hljs-string">'HELLO_CONST'</span>;
<span class="hljs-keyword">if</span> (mylet && myconst) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'mylet:'</span>, mylet, <span class="hljs-string">'myconst:'</span>, myconst);
<span class="hljs-comment">// -> mylet: hello let myconst: HELLO_CONST</span>
}
myfunction();</code></pre>
</section>
</div>
<div class="slide hidden" id="slide-28">
<section class="slide-content"><h3 id="js-obstacles-with-es6">js obstacles with ES6</h3>
<blockquote>
<p>Variable Scope</p>
</blockquote>
<p><em><code>let</code> and <code>const</code> are not accessible outside of the block they were defined in</em></p>
<p><em>but variables declared with <code>var</code> are accessible outside of the defining block</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">3</span>; i++) {
<span class="hljs-keyword">var</span> myvar = i;
<span class="hljs-keyword">let</span> mylet = i;
<span class="hljs-keyword">if</span> (!i) {
<span class="hljs-keyword">const</span> myconst = i;
}
<span class="hljs-built_in">console</span>.log(myconst); <span class="hljs-comment">// ReferenceError: myconst is not defined</span>
}
<span class="hljs-built_in">console</span>.log(myvar); <span class="hljs-comment">// -> 2</span>
<span class="hljs-built_in">console</span>.log(mylet); <span class="hljs-comment">// ReferenceError: mylet is not defined</span>
<span class="hljs-built_in">console</span>.log(myconst); <span class="hljs-comment">// ReferenceError: myconst is not defined</span></code></pre>
</section>
</div>
<div class="slide hidden" id="slide-29">
<section class="slide-content"><h3 id="js-obstacles-with-es6">js obstacles with ES6</h3>
<blockquote>
<p>problem with <code>for</code> loop</p>
</blockquote>