This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path01.advanced.js.html
executable file
·3610 lines (3215 loc) · 119 KB
/
01.advanced.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Web & Mobile Development — 01.Advanced.JS</title>
<meta name="description" content="Web & Mobile Development — 01.Advanced.JS">
<meta name="author" content="Bram(us) Van Damme - ikdoeict.be">
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css" media="screen">
<link rel="stylesheet" href="css/print.css" media="print">
<link rel="stylesheet" href="assets/01/examples.css">
<link rel="stylesheet" href="lib/zenburn.css">
</head>
<body>
<div id="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- 0 : Title -->
<section>
<section>
<h3 class="inverted">Web & Mobile Development <small>JLW288</small></h3>
<h1>01.Advanced.JS</h1>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
<script>
// Delicously hacky. Look away.
if( navigator.userAgent.match( /(iPhone|iPad|iPod|Android)/i ) )
document.write( '<p style="color: rgba(0,0,0,0.3); text-shadow: none;">('+'Tap to navigate'+')</p>' );
</script>
</section>
</section>
<!-- 1 : JavaScript? -->
<section>
<section>
<h2>JavaScript?</h2>
<p><img src="assets/01/quiz.png" alt="What is the world's most popular language?" title="What is the world's most popular language?" width="429" height="333" /></p>
<p class="fragment">Yeah, JavaScript!</p>
</section>
<section>
<h2>Java(Script)?</h2>
<div>
<figure class="fragment">
<img src="assets/01/hamster.png" alt="JavaScript is to Java as Ham is to Hamster" title="JavaScript is to Java as Ham is to Hamster" width="500" height="284" style="border-color: #FFF; border-width: 12px;" />
<figcaption>Dixit <a href="http://adactio.com/">Jeremy Keith</a>, Cartoon by <a href="http://www.flickr.com/photos/manssandstrom/4132454413/in/photostream/">Brad Colbow</a></figcaption>
</figure>
</div>
</section>
<section>
<h2>JavaScript: History</h2>
<ul>
<li class="fragment">
Scripting language originally developed for Netscape 2.0
<ul>
<li>First known as Mocha or as LiveScript, later on renamed to JavaScript</li>
</ul>
</li>
<li class="fragment">
Internet Explorer 3 included its own implementation, named JScript
<ul>
<li>Implemented due to gaining popularity of JavaScript</li>
<li>Named differently to avoid trademark issues</li>
</ul>
</li>
<li class="fragment">
Eventually standardized in the ECMA-266 specificiation, known as ECMAScript
<ul>
<li>Several versions do exist</li>
<li>Current version is <a href="http://es5.github.com/">ECMAScript 5.1</a></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>ECMAScript Dialects</h2>
<ul>
<li class="fragment">
Several languages/implementations are based on (some version of) ECMAScript
<ul>
<li class="fragment">Adobe's ActionScript 3 (Flash, Flex)</li>
<li class="fragment">JavaScript as we know it today</li>
<li class="fragment">...</li>
</ul>
</li>
<li class="fragment">
Officially, JavaScript <a href="https://developer.mozilla.org/en-US/docs/JavaScript">is maintained by Mozilla</a> nowadays.
<ul>
<li class="fragment">Current version JavaScript 1.8.5</li>
<li class="fragment">Non-Mozilla implementations claim “JavaScript Compliance” but actually target the ECMAScript standard.</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>JavaScript Engines</h2>
<ul>
<li class="fragment">
Browsers have a built-in engine to interpret JavaScript
<ul>
<li class="fragment"><a href="https://developer.mozilla.org/en/SpiderMonkey">SpiderMonkey</a> in Firefox</li>
<li class="fragment"><a href="http://code.google.com/p/v8/">V8</a> in Chrome</li>
<li class="fragment"><a href="http://www.webkit.org/projects/javascript/index.html">JavaScriptCore</a> (SquirrelFish/Nitro) in Webkit/Safari</li>
<li class="fragment"><a href="http://my.opera.com/ODIN/blog/carakan-faq">Carakan</a> in Opera</li>
<li class="fragment"><a href="http://en.wikipedia.org/wiki/Chakra_%28JScript_engine%29">Chakra</a> in IE</li>
</ul>
</li>
<li class="fragment">
If you have access to them, these engines can be implemented in other projects
<uL>
<li class="fragment"><a href="http://nodejs.org/">Node.js</a> is powered by Google's V8</li>
<li class="fragment"><a href="https://developer.mozilla.org/en/Rhino">Rhino</a> is an open source JavaScript engine developed by Mozilla and written in Java</li>
<li class="fragment"><a href="http://www.adobe.com/devnet/photoshop/scripting.html">Photoshop (!) also comes with a JavaScript engine</a></li>
</ul>
</li>
</ul>
</section>
</section>
<!-- 2: JavaScript 101 -->
<section>
<section>
<h2>JavaScript 101 with Forrest Gump</h2>
<p><img src="assets/01/forrestgump.jpg" alt="Forrest Gump" title="Forrest Gump" width="467" height="350" /></p>
<p>Because Forrest Gump is one of those movies ...</p>
</section>
<section>
<h2>JavaScript 101</h2>
<div>
<figure>
<img src="assets/01/robertnyman.jpg" alt="Robert Nyman" title="Robert Nyman" width="467" height="350" />
<figcaption>Photo by <a href="http://www.flickr.com/photos/manssandstrom/4132454413/in/photostream/">Måns Sandström</a></figcaption>
</figure>
</div>
<p style="font-size: 80%">... and because <a href="http://www.robertnyman.com/">Robert Nyman</a> made <a href="http://www.slideshare.net/robnyman/javascript-like-a-box-of-chocolates">an awesome presentation</a> about it, which formed the starting point for these slides.</p>
</section>
<section>
<h2>Whilst I'm at it</h2>
<div>
<figure>
<img src="assets/01/mathias.jpg" alt="Mathias Bynens" title="Mathias Bynens" width="480" height="360" />
<figcaption>Photo by <a href="http://www.addyosmani.com/">Addy Osmani</a></figcaption>
</figure>
</div>
<p style="font-size: 80%">Another thank you goes out to <a href="http://www.mathiasbynens.be/">Mathias Bynens</a> <em>(2nd one from the left)</em> who gave some handy remarks.</p>
</section>
</section>
<!-- 3: JavaScript Variables -->
<section>
<section>
<h2>JavaScript 101: Variables</h2>
<p><img src="assets/01/likeaboxofchocolates.jpg" alt="Life is like a box of chocolates" title="Life is like a box of chocolates" width="397" height="264" /></p>
<q>Life is like a box of chocolates,<br />you never know what you're gonna get</q>
</section>
<section>
<h2>Before we dive in</h2>
<ul>
<li class="fragment">
Variables have a <em>name</em> and a <em>value</em>
</li>
<li class="fragment">
JavaScript is case sensitive
<ul>
<li><code>firstName</code> and <code>firstname</code> are not the same!</li>
</ul>
</li>
<li class="fragment">JavaScript is dynamically typed
<ul>
<li>When declaring a variable, you cannot specify the type. JavaScript will <em>guess</em> the data type when assigning a value</li>
<li>viz. a variable given the value <code>3</code> will be a number</li>
</ul>
</li>
<li class="fragment">JavaScript is weakly typed
<ul>
<li>JavaScript will adjust the type of a var when a new value is assigned
<ul>
<li>This phenomenon is known as <em>implicit type conversion</em> or <em>coercion</em></li>
</ul>
</li>
<li>viz. if that same variable is changed to <code>"Hello"</code> it'll become a string</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Declaring variables (1)</h2>
<ul>
<li class="fragment">
Use the <code>var</code> keyword and assign a <em>value</em> to a <em>name</em>
<pre class="bigger"><code contenteditable class="language-javascript">var firstName = 'Forrest';
alert(firstName);</code></pre>
</li>
<li class="fragment">
Remember: JavaScript is dynamically typed and weakly typed
<pre class="bigger"><code contenteditable class="language-javascript">// Dynamic typing
var firstName = 'Forrest';
alert(typeof firstName); // string
// Weak typing
firstName = 3;
alert(typeof firstName); // JavaScript has changed the type to number</code></pre>
</li>
</ul>
</section>
<section>
<h2>Declaring variables (2)</h2>
<ul>
<li class="fragment">
It's possible to declare multiple vars at once.
</li>
<li class="fragment">
Just separate them by a comma
<pre class="bigger"><code contenteditable class="language-javascript">var firstName = 'Forrest',
lastName = 'Gump';
alert(firstName);
alert(lastName);</code></pre>
</li>
</ul>
</section>
<section>
<h2>JavaScript Data Types (1)</h2>
<ul>
<li class="fragment">JavaScript supports these Primitive Data Types
<ul>
<li><code>string</code></li>
<li><code>number</code>
<ul>
<li>No real distinction for float/double/int!</li>
</ul>
</li>
<li><code>boolean</code></li>
<li><code>null</code>
<ul>
<li>empty value</li>
</ul>
</li>
<li><code>undefined</code>
<ul>
<li>variable does not exist</li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>JavaScript Data Types (2)</h2>
<ul>
<li class="fragment">What about <code>Array</code>? And <code>Date</code>? And ...?
<ul class="fragment"><li>Well, those are <code>Object</code>s really</li></ul>
</li>
<li class="fragment">And oh, next to <code>string</code> you also have something like <code>String</code>
<ul>
<li class="fragment">The former is the data type</li>
<li class="fragment">And the latter is ... an <code>Object</code></li>
</ul>
</li>
<li class="fragment">
And then you have functions
<ul>
<li class="fragment">Those are functions</li>
<li class="fragment">
... but JavaScript treats them as <em>First Class Objects</em>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Not Sure If ...</h2>
<p><img src="assets/01/notsureif.png" alt="Not Sure If ..." title="Not Sure If ..." width="480" height="360" /></p>
<p class="fragment"><em>Let me explain on the next few slides ;)</em></p>
</section>
<section>
<h2>Global Objects and Literals (1)</h2>
<ul>
<li class="fragment">
In JavaScript nearly everything is built upon <code>Object</code>
<ul>
<li><code>Object</code> is the core of the language</li>
<li>With it, you can declare anything you want if you extend upon it.</li>
</ul>
</li>
<li class="fragment">
JavaScript provides some global objects such as <code>String</code>, <code>Number</code>, <code>Boolean</code>, <code>Array</code>, etc.
<ul>
<li class="fragment">Derived from <code>Object</code></li>
<li class="fragment">Define a structure to store <em>something</em> in</li>
<li class="fragment">
Instances have some properties
<ul>
<li>eg. <code>myString.length;</code> (Also noted as <code>String#length</code>)</li>
</ul>
</li>
<li class="fragment">
Instances have some methods (to manipulate/access the stored value)
<ul>
<li>eg. <code>myString.toUpperCase();</code> (Also noted as <code>String#toUpperCase</code>)</li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Global Objects and Literals (2)</h2>
<ul>
<li class="fragment">
Normally, one would do something like this
<pre class="bigger"><code contenteditable class="language-javascript">var firstName = new String('Forrest');</code></pre>
<ul>
<li class="fragment">What you get back is an instance of <code>String</code></li>
<li class="fragment">The type of the instance itself is <code>object</code></li>
<li class="fragment">The type of the value inside that instance is <code>string</code></li>
</ul>
</li>
<li class="fragment">Let's pull out some code to test this
<pre class="bigger"><code contenteditable class="language-javascript">var firstName = new String('Forrest');
alert(firstName instanceof String); // true
alert(typeof firstName); // object
alert(typeof firstName.toString()); // string</code></pre>
</li>
</ul>
</section>
<section>
<h2>Global Objects and Literals (3)</h2>
<ul>
<li class="fragment">
But next to that syntax, JavaScript also provides something named the <em>literal notation</em>
</li>
<li class="fragment">Literals allow you to immediately assign a value to a variable
<pre class="bigger"><code contenteditable class="language-javascript">var firstName = 'Forrest';</code></pre>
<ul>
<li class="fragment">What you get back is the value</li>
<li class="fragment">The type of the value is <code>string</code> (dynamic typing in action)</li>
</ul>
</li>
<li class="fragment">
Again, some code
<pre class="bigger"><code contenteditable class="language-javascript">var firstName = 'Forrest';
alert(firstName instanceof String); // false
alert(typeof firstName); // string</code></pre>
</li>
</ul>
</section>
<section>
<h2>Still Not Sure?</h2>
<p><img src="assets/01/notsureif.png" alt="Not Sure If ..." title="Not Sure If ..." width="480" height="360" /></p>
<p class="fragment"><em>It's not that hard, really!</em></p>
</section>
<section>
<h2>Global Objects and Literals (4)</h2>
<ul>
<li class="fragment">
You know, this literal thing isn't new, you've been doing it all along; in Java for example:
<pre class="bigger"><code class="language-java">Integer num = new Integer(3); // Not Literal
Integer num = 3; // Literal</code></pre>
</li>
<li class="fragment">JavaScript provides literals for:
<ul>
<li><code>String</code></li>
<li><code>Number</code></li>
<li><code>Boolean</code></li>
<li><code>RegExp</code></li>
<li><code>Function</code> <em>(*)</em></li>
<li><code>Array</code></li>
<li><code>Object</code></li>
</ul>
</li>
</ul>
<footer class="fragment"><em>(*) Function is the odd one here (see further)</em></footer>
</section>
<section>
<h2>Global Objects and Literals (5)</h2>
<ul>
<li class="fragment">
Example (1)
<pre class="bigger"><code contenteditable class="language-javascript">var firstName = new String('Forrest'); // String Constructor
var firstName = 'Forrest'; // String Literal
var birthYear = new Number('1945'); // Number Constructor
var birthYear = 1945; // Number Literal
var isRunning = new Boolean('false'); // Boolean Constructor
var isRunning = false; // Boolean Literal
var hasR = new RegExp('r', 'i'); // RegExp Constructor
var hasR = /r/i; // RegExp Literal
var says = new Function('sentence', 'alert(sentence)'); // Function Constructor
var says = function(sentence) { alert(sentence); }; // Function Literal
function says(sentence) { alert(sentence); }; // The function statement</code></pre>
</li>
</ul>
</section>
<section>
<h2>Global Objects and Literals (6)</h2>
<ul>
<li class="fragment">
Example (2)
<pre class="bigger"><code contenteditable class="language-javascript">var forrestFriends = new Array('Bubba', 'Lieutenant Dan');
var forrestFriends = ['Bubba', 'Lieutenant Dan']; // Array Literal
var forrest = new Object();
forrest.firstName = 'Forrest';
forrest.lastName = 'Gump';
var forrest = { // Object Literal
firstName : 'Forrest', // semi-colons to assign
lastName : 'Gump',
says : function() { // works with functions too!
return 'Stupid is as stupid does';
} // no trailing comma (!)
};</code></pre>
</li>
</ul>
</section>
<section>
<h2>Global Objects and Literals (7)</h2>
<ul>
<li class="fragment">
<span>Remember that if it exists as a primitive data type (viz. <code>string</code>, <code>number</code>, <code>boolean</code>), you'll get the primitive returned when using the literal notation!</span>
<pre class="bigger"><code contenteditable class="language-javascript">// String Instance
var lastName = new String('Gump');
alert(typeof lastName); // object
alert(lastName instanceof String); // true
// String Literal
var firstName = 'Forrest';
alert(typeof firstName); // string
alert(firstName instanceof String); // false</code></pre>
</li>
</ul>
</section>
<section>
<h2>Global Objects and Literals (8)</h2>
<ul>
<li class="fragment">
If no primitive variant exists (<code>Object</code>, <code>Array</code>, <code>RegExp</code>, <code>Function</code>), the literal <em>will</em> return an instance when using the literal notation.
<pre class="bigger"><code contenteditable class="language-javascript">// Array Instance
var forrestFriends = new Array('Bubba', 'Lieutenant Dan');
alert(typeof forrestFriends); // object
alert(forrestFriends instanceof Array); // true
// Array Literal
var forrestFriends = ['Bubba', 'Lieutenant Dan'];
alert(typeof forrestFriends); // object
alert(forrestFriends instanceof Array); // true</code></pre>
</li>
</ul>
</section>
<section>
<h2>Oh, I See ...</h2>
<p><img src="assets/01/ohisee.png" alt="Oh, I see ..." title="Oh, I see ..." width="480" height="360" /></p>
<p class="fragment"><em>Or at least I hope you do</em></p>
</section>
<section>
<h2>Global Objects and Literals (9)</h2>
<ul>
<li class="fragment">
Remember when I told you all Global Objects (such as <code>String</code>) derive from <code>Object</code>?
</li>
<li class="fragment">
Here's some code proof:
<pre class="bigger"><code contenteditable class="language-javascript">// Number Instance
var birthYear = new Number('1945');
alert(typeof birthYear);
alert(birthYear instanceof Number);
alert(birthYear instanceof Object);
// Array Instance
var forrestFriends = ['Bubba', 'Lieutenant Dan'];
alert(typeof forrestFriends);
alert(forrestFriends instanceof Array);
alert(forrestFriends instanceof Object);
// Function literal
var says = function() { alert('Stupid is as stupid does'); };
alert(typeof says); // <-- the odd one out
alert(says instanceof Function);
alert(says instanceof Object);</code></pre>
</li>
</ul>
</section>
<section>
<h2>Global Objects and Literals (10)</h2>
<ul>
<li class="fragment">
What you should remember from all this
<ul>
<li>Literal Notations are shorthands</li>
<li>Literal Notations return a direct value instead of an object if possible</li>
</ul>
</li>
<li class="fragment">
What you must know about Literals
<ul>
<li>Literal Notations are the preferred way to write JavaScript</li>
<li>Literal Notations are all around, even some of the current techniques (think JSON) are built upon this principle</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Still seeing it?</h2>
<p><img src="assets/01/stillseeingit.png" alt="Still seeing it?" title="Still seeing it?" width="480" height="360" /></p>
<p class="fragment"><em>Now, back to variables</em></p>
</section>
<section>
<h2>Accessing Object Properties</h2>
<ul>
<li class="fragment">Via square brackets, or via dot notation
<pre class="bigger"><code contenteditable class="language-javascript">// Object Literal
var forrest = {
firstName : 'Forrest',
lastName : 'Gump',
sentence : 'Stupid is as stupid does',
says : function() {
return this.sentence;
}
};
// test
console.log(forrest);
alert(forrest.firstName);
alert(forrest['firstName']);
alert(forrest.says());</code></pre>
<ul>
<li class="fragment">The <em>keys</em> of the Object Literal may be placed between quotes. When using spaces/special chars in the keys <em>(not recommended though)</em>, <a href="http://mathiasbynens.be/notes/javascript-properties">quotes must be used</a></li>
<li class="fragment">The dot notation to accessing properties is the preferred way. Square brackets mandatory when properties have spaces in their name</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Implicit Type Conversion (1)</h2>
<ul>
<li class="fragment">As mentioned: JavaScript will change variable types on the fly <em>(coercion)</em></li>
<li class="fragment">
This not only happens when assigning values, but also when using variables
<pre class="bigger"><code contenteditable class="language-javascript">// Various 'false' values
var nullVal = null;
var undefinedVal = undefined;
var zeroVal = 0;
var falseVal = false;
var emptyString = '';
if (emptyString) {
alert('truthy');
} else {
alert('falsy'); // false-ish
}</code></pre>
<pre class="bigger"><code contenteditable class="language-javascript">console.log('5' + 6 + 7);</code></pre>
</li>
</ul>
</section>
<section>
<h2>Implicit Type Conversion (2)</h2>
<ul>
<li class="fragment">You can prevent coercion by using the <em>identity operator</em>
<pre class="bigger"><code contenteditable class="language-javascript">// Equality
alert((7 == '7') ? 'equal' : 'not equal');
// Identity
alert((7 === '7') ? 'equal (identity)' : 'not equal (identity)');</code></pre>
</li>
<li class="fragment">Or by explicitly casting a variable to being a certain type
<pre class="bigger"><code contenteditable class="language-javascript">// Type Coercion (default)
alert('5' + 6 + 7);
// Prevent Type Coercion
alert(parseInt('5', 10) + 6 + 7);</code></pre>
</li>
</ul>
</section>
<section>
<h2>Scope (1)</h2>
<ul>
<li class="fragment">Global or Local
<pre class="bigger"><code contenteditable class="language-javascript">// Global
var quote = 'I had run for 3 years, 2 months, 14 days, and 16 hours.';
var foo = function() {
var saying = 'My name is Forrest, Forrest Gump'; // Local
alert(saying);
alert(quote); // Access to Global
question = 'Why don\'t you love me, Jenny?'; // Local ?
alert(question);
};
foo();
alert(quote);
// alert(saying); // disabled, will fail
alert(question); // Nope, Chuck Testa! Erm, I mean global!</code></pre>
<ul>
<li class="fragment">
The scope is also known as the <em>environment</em> in which a variable lives
</li>
<li class="fragment">
Note: In <a href="https://developer.mozilla.org/en/JavaScript/Strict_mode">Strict Mode</a>, the <code>var</code> keyword is mandatory.
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Scope (2)</h2>
<ul>
<li class="fragment">
Best practice: <strong>Always</strong> use the <code>var</code> keyword inside functions, loops, etc.
<ul>
<li>Prevents pollution of the global scope</li>
<li>Prevents overwriting (by others)</li>
<li>Works in Strict Mode</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Variable Properties and Methods (1)</h2>
<ul>
<li class="fragment">
Each instance of a global object provides some properties and/or methods
<ul>
<li>eg. <code>String#length</code></li>
<li>eg. <code>String#toUpperCase</code></li>
</ul>
</li>
<li class="fragment">
Next to instance methods, they also <em>can</em> provide static methods
<ul>
<li>eg. <code>Array#isArray</code></li>
</ul>
</li>
<li class="fragment">
Full list of properties/methods to be found on <a href="https://developer.mozilla.org/en/JavaScript/Reference#Global_Objects">Mozilla Developer Network</a> or <a href="http://es5.github.com/">the ECMAScript Spec</a>
</li>
</ul>
</section>
<section>
<h2>Variable Properties and Methods (2)</h2>
<ul>
<li class="fragment">
Properties and methods are also there if a simple type is returned when using literals.
<ul>
<li class="fragment">JavaScript does some magic in the background to make that work</li>
</ul>
</li>
<li class="fragment">
Example
<pre class="bigger"><code contenteditable class="language-javascript">// String Instance
var firstName = new String('Forrest'); // an instance of String
alert(firstName.toUpperCase());
// String Literal
var firstName = 'Forrest'; // a variable of the type string
alert(firstName.toUpperCase());
// Look ma, no var
alert('Forrest'.toUpperCase());
alert(['Bubba', 'Lieutenant Dan'].length);</code></pre>
</li>
</ul>
</section>
<section>
<h2>Variables 101: Summary</h2>
<ul>
<li class="fragment">
JavaScript is weakly and dynamically typed
<ul>
<li class="fragment">Coercion!</li>
</ul>
</li>
<li class="fragment">
In JavaScript, about everything is an <code>Object</code>
<ul>
<li class="fragment">JavaScript provides properties and methods which you can access/call</li>
</ul>
</li>
<li class="fragment">
JavaScript provides a shorthand (<strong>Literal Notation</strong>)
<ul>
<li class="fragment">
Primitive returned when possible (<code>String</code>, <code>Number</code>, <code>Boolean</code>)
</li>
</ul>
<li class="fragment">
Variables have a scope in which they are accessible.
<ul>
<li>It's possible to access variables that live in an outer scope</li>
</ul>
</li>
</ul>
</section>
</section>
<!-- 4: JavaScript Control Structures -->
<section>
<section>
<h2>JavaScript 101: Control Structures</h2>
<p><img src="assets/01/stoppedrunning.jpg" alt="" title="Feather" width="467" height="298" /></p>
<q>I had run for 3 years, 2 months, 14 days, and 16 hours.</q>
</section>
<section>
<h2>If-Else & Switch-Case(-Default)</h2>
<div class="fragment">
<pre class="bigger"><code contenteditable class="language-javascript">// If statement
var badGrades = true;
if (badGrades) {
alert('Mom sleeps with teacher');
} else {
alert('Mom does not sleep with teacher');
}
// Switch statement
var age = 10,
lifeState;
switch (age) {
case 10:
lifeState = 'Young';
break;
case 60:
lifeState = 'Old';
break;
default:
lifeState = 'unknown';
break;
}
alert(lifeState);</code></pre>
</div>
</section>
<section>
<h2>For</h2>
<div class="fragment">
<pre class="bigger"><code contenteditable class="language-javascript">// Var we'll be using
var forrestFriends = ['Bubba', 'Lieutenant Dan'];
// For
for (var i = 0; i < forrestFriends.length; i++) {
alert(forrestFriends[i]);
}</code></pre>
</div>
</section>
<section>
<h2>For (optimized)</h2>
<p class="fragment">Performance can be improved by caching variables. That way JavaScript doesn't have to look up everything again.</p>
<div class="fragment">
<pre class="bigger"><code contenteditable class="language-javascript">// Var we'll be using
var forrestFriends = ['Bubba', 'Lieutenant Dan'];
// For
for (var i = 0, len = forrestFriends.length; i < len; i++) {
alert(forrestFriends[i]);
}</code></pre>
</div>
</section>
<section>
<h2>For-In</h2>
<div class="fragment">
<pre class="bigger"><code contenteditable class="language-javascript">// Vars we'll be using
var forrest = {
firstName : 'Forrest',
lastName : 'Gump' // no trailing comma (!)
};
var forrestFriends = ['Bubba', 'Lieutenant Dan'];
// For - In (Array)
for (var friend in forrestFriends) {
alert(forrestFriends[friend]);
}
// For - In (Object)
for (var prop in forrest) {
alert(prop + ' = ' + forrest[prop]);
}</code></pre>
</div>
<p class="fragment"><small>Note: When using arrays, the order is not guaranteed! Better to use <code>Array#forEach</code></small></p>
</section>
<section>
<h2>While & Do-While</h2>
<div class="fragment">
<pre class="bigger"><code contenteditable class="language-javascript">// While
var count = 5;
while (count > 0) {
console.log(count);
count--;
}
// Do - While
var count = 5;
do {
console.log(count);
count--;
} while (count > 0);
// Tip: retry this example with count = 0 as starting value
</code></pre>
</div>
</section>
<section>
<h2>With</h2>
<div class="fragment">
<pre class="bigger"><code contenteditable class="language-javascript">// Vars we'll be using
var forrest = {
firstName : 'Forrest',
lastName : 'Gump' // no trailing comma (!)
};
// With
with (forrest) {
alert(firstName);
alert(lastName);
}
</code></pre>
</div>
<p class="fragment"><small>Note: With is <a href="http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/">considered harmful</a> and performs badly. Not suggested.</small></p>
</section>
<section>
<h2>Try-Catch(-Finally)</h2>
<div class="fragment">
<pre class="bigger"><code contenteditable class="language-javascript">// Vars we'll be using
var forrest = {
firstName : 'Forrest',
lastName : 'Gump',
says : function() {
console.log('Stupid is as stupid does');
}
};
// Try-Catch
try {
forrest.functionDoesNotExist();
} catch (error) {
console.error('ERROR! ' + error.name + ' : ' + error.message);
}
// Try-Catch-Finally
try {
forrest.functionDoesNotExist();
} catch (error) {
console.error('ERROR! ' + error.name + ' : ' + error.message);
} finally {
forrest.says();
}
</code></pre>
</div>
<p class="fragment"><small>Not a real control structure, but found it fitting here</small></p>
</section>
<section>
<h2>Break (1)</h2>
<ul>
<li class="fragment">Jumps to the end of the statement & aborts current task</li>
<li class="fragment">
Valid inside <code>switch</code>, <code>for</code>, <code>for-in</code>, <code>while</code>, and <code>do-while</code>
</li>
<li class="fragment">
Example
<pre class="bigger"><code contenteditable class="language-javascript">for (var x = 1; x <= 5; x++) {
var y = 1;
while (y <= 7) {
if (y == 5) { break; }
console.log(x + '-' + y);
y++;
}
}</code></pre>
</li>
</ul>
</section>
<section>
<h2>Break (2)</h2>
<ul>
<li class="fragment">
To break the outer loop, one can make use of a <em>label</em>
<pre class="bigger"><code contenteditable class="language-javascript">myForLoop:
for (var x = 1; x <= 5; x++) {
var y = 1;
while (y <= 7) {
if (y == 5) { break myForLoop; }
console.log(x + '-' + y);
y++;
}
}</code></pre>
</li>
</ul>
</section>
<section>
<h2>Continue (1)</h2>
<ul>
<li class="fragment">Jumps back to start of statement and continues with next item</li>
<li class="fragment">Valid inside <code>for</code>, <code>for-in</code>, <code>while</code>, and <code>do-while</code>
<li class="fragment">
Example
<pre class="bigger"><code contenteditable class="language-javascript">for (var x = 1; x <= 5; x++) {
for (var y = 1; y <= 7; y++) {
if (y == 5) continue;
console.log(x + '-' + y);
}
}</code></pre>
</li>
</ul>
</section>
<section>
<h2>Continue (2)</h2>
<ul>
<li class="fragment">
Using a <em>label</em> is also possible
<pre class="bigger"><code contenteditable class="language-javascript">myForLoop:
for (var x = 1; x <= 5; x++) {
for (var y = 1; y <= 7; y++) {
if (y == 5) { continue myForLoop; }
console.log(x + '-' + y);
}
}</code></pre>
</li>
</ul>
<p class="fragment"><small>In this example you get the same result as the first <code>break</code> example</small></p>
</section>
</section>