-
Notifications
You must be signed in to change notification settings - Fork 42
/
fundamental.html
executable file
·1277 lines (1162 loc) · 45.1 KB
/
fundamental.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
<!-- TODO
- Motivate with d3 examples
- SVG could have shown the diagram in this page http://www.recursion.org/d3-for-mere-mortals/
- check tags depedencies
- CSS Explanation about rules
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Technology Fundamentals for D3</title>
<meta name="description" content="D3 Tutorial by Interactive Data Lab, University of Washington">
<meta name="author" content="Kanit Wongsuphasawat, Dominik Moritz">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="reveal.js/css/theme/black.css" id="theme">
<link rel="stylesheet" href="style.css">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="highlight.js/src/styles/obsidian.css" id="highlight-theme">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write('<link rel="stylesheet" href="reveal.js/css/print/' + (window.location.search.match(/print-pdf/gi) ? 'pdf' : 'paper') + '.css" type="text/css" media="print">');
</script>
<script src="js/jquery.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-55386037-1', 'auto');
ga('send', 'pageview');
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="center">
<h1>
Technology fundamentals for D3.js - HTML, CSS, SVG, Javascript
</h1>
<br/>
<p>
<small>
A tutorial by
<a href="http://kanitw.yellowpigz.com/">Kanit "Ham" Wongsuphasawat</a> (<a href="http://twitter.com/kanitw">@kanitw</a>),
<a href="https://homes.cs.washington.edu/~jhoffs/#CurriculumVitae">Jane Hoffswell</a>,
and
<a href="http://homes.cs.washington.edu/~domoritz/">Dominik Moritz</a> (<a href="http://twitter.com/domoritz">@domoritz</a>)
</small>
</p>
<p>
<small>
<a href="http://idl.cs.washington.edu">Interactive Data Lab</a>,
<a href="http://uw.edu">University of Washington</a>
</small>
</p>
</section>
<section>
<h3>Notes</h3>
<ul>
<li>HTML, CSS and SVG examples are all codepens, which you can click edit to play with.</li>
<li>You can also use inspector (<code>⌘⎇I</code> in Firefox or Chrome) to inspect examples.</li>
<li>Also use Javascript console to try Javascript code examples.</li>
</ul>
</section>
<section>
<section>
<h2>HTML</h2>
<p>
Hypertext Markup Language
</p>
<aside class="notes">
First thing first.
</aside>
</section>
<section>
<h3>Plain Text</h3>
<iframe src="example/plain.html" class="white" width="100%" height="450"></iframe>
</section>
<section>
<h3>Use HTML to markup</h3>
<p><code>html, body, h1, p, strong, em, a, img</code></p>
<div class="leftright">
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="eNOeYG" data-user="domoritz" data-default-tab="html" class='codepen'></p>
</div>
<div class="right">
<p data-height="500" data-theme-id="0" data-slug-hash="eNOeYG" data-default-tab="result" data-user="domoritz" class='codepen'></p>
</div>
<aside class="notes">
Tags must be closed
</aside>
</div>
</section>
<section>
<h3>HTML: div, span + ID, Class</h3>
<p>
<code><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div"><div></a>
</code>,
<code><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span"><span></a>
</code>
</p>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="jAHsK" data-user="kanitw" data-default-tab="html" class='codepen'> </p>
</div>
<div class="right">
<p data-height="450" data-theme-id="0" data-slug-hash="jAHsK" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>More Tags</h3>
<li>list (<code>ul, ol, li</code>),
<code>hr, br, form, input, select, iframe</code>
</li>
<!-- <p><table>, -->
<li class="see-also">
See w3schools's <a href="http://www.w3schools.com/html/html_quick.asp">quick list</a> of the most commonly used HTML tags
</li>
<li>Or learn more at Mozilla Develop Network's <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML">HTML tutorial</a>.</li>
<aside class="notes">
In interests of saving time, I would skip other commonly used tags.
</aside>
</section>
</section>
<section>
<section>
<h3>DOM</h3>
<ul>
<li>A convention for representing and interacting with objects in HTML, XHTML and XML documents.</li>
<li>Nodes of every document are organized in a <strong>tree structure</strong>, called the DOM tree
<ul>
<li>For example, use inspector to see this file's source.</li>
</ul>
</li>
<li>Provides API to modify its content using language such as Javascript</li>
</ul>
</section>
</section>
<section>
<section data-markdown>
### CSS
#### Cascading Style Sheets
</section>
<section>
<h3>CSS Selectors</h3>
<p>Query the DOM Tree</p>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="xDIbe" data-user="kanitw" data-default-tab="css" class='codepen'> </p>
</div>
<div class="right">
<p data-height="450" data-theme-id="0" data-slug-hash="xDIbe" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
<p class="see-also"><small>See also: <a href="http://www.webmonkey.com/2010/02/css-properties/">CSS Properties</a> – Webmonkey and <a href="http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/">CSS Selectors on NetTuts</a></small></p>
<aside class="notes">
<ul>
<li>ID:
<code id="">tag#id</code>
</li>
<li>Classes:
<code>tag.class</code>
</li>
<li>
Pseudo-classes:
<code>a:hover</code>,
<code>div:first-child</code>
</li>
<li>
Descendant:
<code>A D</code>
</li>
<li>
Child:
<code>A > C</code>
</li>
<li>
Next Sibling:
<code>A + C</code>
</li>
</ul>
</aside>
</section>
<section>
<h3>CSS: Box Model, Position</h3>
<p>The default position is <code>position: static;</code></p>
<div class="left">
<img src="img/box-model.gif" width="100%" />
</div>
<div class="right">
<p data-height="300" data-theme-id="0" data-slug-hash="eLlKj" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
<p>
</p>
</section>
<section>
<h3>CSS: Position</h3>
<p>
<small>
<code>relative</code>
adjust the element's position, without changing layout
</small>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="Fnulb" data-user="kanitw" data-default-tab="css" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="300" data-theme-id="0" data-slug-hash="Fnulb" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</p>
</section>
<section>
<h3>CSS: Position</h3>
<p>
<small>
<code>absolute</code>
taken out of the flow and thus takes up no space when placing other elements. The absolutely positioned element is positioned relative to nearest positioned ancestor. If a positioned ancestor doesn't exist, the initial container is used.
</small>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="HDfgn" data-user="kanitw" data-default-tab="css" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="300" data-theme-id="0" data-slug-hash="HDfgn" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</p>
</section>
<section>
<h3>CSS: Position</h3>
<p>
<small>
<code>absolute</code> under <code>relative</code>
</small>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="EyaHr" data-user="kanitw" data-default-tab="css" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="300" data-theme-id="0" data-slug-hash="EyaHr" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</p>
</section>
<section>
<h3>CSS: Short Form</h3>
<p>
<pre>
<code class="css" data-trim>
background-color: #000; background-image: url(images/bg.gif); background-repeat: no-repeat; background-position: top right;
</code>
</pre>
</p>
<p>Can be shortened to:</p>
<p>
<pre>
<code class="css" data-trim>
background: #000 url(images/bg.gif) no-repeat top right;
</code>
</pre>
</p>
</section>
<section>
<h3>CSS: Include</h3>
<p>
<small>Inline:</small>
</p>
<pre>
<code class="html">
<style>
/* put css here */
</style></code>
</pre>
<pre>
<code class="html" data-trim>
<div style="/* put css in style attribute of any tag */">
...
</div>
</code>
</pre>
<p>
<small>Include:</small>
</p>
<p>
<pre>
<code class="html" data-trim>
<link rel="stylesheet" href="style.css"></code>
</pre>
</p>
</section>
<section>
<h3><a href="http://sass-lang.com">Sass</a>
</h3>
<p>
<b>Variable</b>, <b>Nesting</b>, Partials, Import, <b>Mixins</b>, Inheritance, <b>Operators</b>
<div style="height: 350px;" class="overflow-y">
<pre><code class="scss" data-trim>
/* variable, operator */
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 0.8*100% $font-stack;
color: $primary-color;
}
/* nesting */
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
/* mixin */
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
-ms-border-radius: $radius;
-o-border-radius: $radius;
border-radius: $radius;
}
.box { @include border-radius(10px); }
</code>
</pre></div>
</p>
<p class="see-also"><small><a href="http://compass-style.org/">Compass</a> is a collection of reusable mixins for Sass.</small></p>
<p class="see-also"><small><a href="http://lesscss.org/">Less</a> is a another option.</small></p>
</section>
<section>
<h3>Advanced CSS</h3>
<ul>
<li>
CSS <a href="https://css-tricks.com/snippets/css/a-guide-to-flexbox/">flexbox</a> and <a href="https://css-tricks.com/snippets/css/complete-guide-grid/">grid</a> can be very useful for creating page layout.
</li>
<li> To make advanced CSS work with older browser, you can easily add <a href="https://github.com/postcss/autoprefixer">autoprefixer</a> if you use a build system such as <a href="https://github.com/postcss/autoprefixer#webpack">Webpack</a>.
</li>
</ul>
</section>
<section>
<h3>CSS: Grids</h3>
<p>
Grid is one of the most commonly used graphic design concept.
</p>
<p>
<img src="img/grid-crop.png" height="350"/>
</p>
<p>
<small>For instance, see
<a href="http://getbootstrap.com">bootstrap</a> grid's
<a href="http://getbootstrap.com/examples/grid/">examples</a></small>
</p>
</section>
<!-- <section>
<h3><a href="http://compass-style.org/">Compass</a>
</h3>
<h4>a collection of reusable mixins for designers</h4>
<p>Example: inline-block</p>
<pre><code class="scss" data-trim>
@mixin inline-block($alignment: $inline-block-alignment) {
@if $legacy-support-for-mozilla {
display: -moz-inline-stack;
}
display: inline-block;
@if $alignment and $alignment != none {
vertical-align: $alignment;
}
@if $legacy-support-for-ie {
*vertical-align: auto;
zoom: 1;
*display: inline;
}
}
</code></pre>
</section> -->
<section>
<h3>CSS: Learn more!</h3>
<p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/learn/css">Learn CSS</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/CSS">CSS Portal</a> – Mozilla Developer Network</li>
<li><a href="http://www.csszengarden.com/">CSS Zen Garden</a>– huge collection of examples</li>
<li>Use inspector to learn from others.</li>
</ul>
</p>
</section>
</section>
<section>
<section>
<h3>SVG</h3>
<h4>Scalable Vector Graphics</h4>
</section>
<!--TODO add figure from http://www.recursion.org/d3-for-mere-mortals/ -->
<section>
<h3>SVG shapes</h3>
<small>Also check out <a href="http://bost.ocks.org/mike/d3/workshop/#109">http://bost.ocks.org/mike/d3/workshop/#109</a></small>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="bdbKaX" data-user="domoritz" data-default-tab="html" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="450" data-theme-id="0" data-slug-hash="bdbKaX" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>SVG + CSS</h3>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="upxsq" data-user="kanitw" data-default-tab="css" class='codepen'> </p>
</div>
<div class="right">
<p data-height="450" data-theme-id="0" data-slug-hash="upxsq" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>SVG: Polygon, Polyline</h3>
<p>Use coordinates to specify path</p>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="GfxFD" data-user="kanitw" data-default-tab="html" class='codepen'> </p>
</div>
<div class="right">
<p data-height="450" data-theme-id="0" data-slug-hash="GfxFD" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>SVG: Path</h3>
<ul>
<li>
<code>M x y</code> – Move to (x,y)
</li>
<li>
<code>m dx dy</code> – Move by (dx,dy)
</li>
<li><code>L x y</code> – Line to (x,y) (or <code>l dx dy</code>)</li>
<li><code>H x</code>, <code>V y</code> – draw horizontal and vertical lines</li>
<li><code>Z</code>, <code>z</code> close path</li>
<li class="see-also">See also: <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths?redirectlocale=en-US&redirectslug=SVG%2FTutorial%2FPaths#Curve_commands">Curves Commands</a></li>
</ul>
</section>
<section>
<h3>SVG: Path Example</h3>
<div class="left">
<p data-height="400" data-theme-id="0" data-slug-hash="Dbrau" data-user="kanitw" data-default-tab="html" class='codepen'> </p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="Dbrau" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>SVG: Group, Title & Transform</h3>
<p>
<ul>
<li>Transform example: <code>translate</code></li>
<li>Use <code>title</code> for tooltip</li>
</ul>
</p>
<div class="left">
<p data-height="400" data-theme-id="0" data-slug-hash="Dbuhd" data-user="kanitw" data-default-tab="html" class='codepen'> </p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="Dbuhd" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>SVG: More Transforms</h3>
<p><small>rotate, scale, skewX, skewY</small></p>
<div class="left">
<p data-height="450" data-theme-id="0" data-slug-hash="cprgG" data-user="kanitw" data-default-tab="html" class='codepen'> </p>
</div>
<div class="right">
<p data-height="450" data-theme-id="0" data-slug-hash="cprgG" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
<p class="see-also"><small>See more <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Basic_Transformations">Transformation</a></small></p>
</section>
<section>
<h3>SVG: No-Layer</h3>
<p>Painter's Algorithm: any pixel-paint applied later obscure any earlier paint and therefore appear to be "in front".</p>
<div class="left">
<p data-height="350" data-theme-id="0" data-slug-hash="linFm" data-user="kanitw" data-default-tab="html" class='codepen'> </p>
</div>
<div class="right">
<p data-height="350" data-theme-id="0" data-slug-hash="linFm" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>SVG: Transparency</h3>
<p><code>rgba</code> or <code>opacity</code></p>
<div class="left">
<p data-height="400" data-theme-id="0" data-slug-hash="AFzvx" data-user="kanitw" data-default-tab="html" class='codepen'> </p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="AFzvx" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>SVG: Learn more!</h3>
<ul>
<li>
<code><a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Texts?redirectlocale=en-US&redirectslug=SVG%2FTutorial%2FTexts">Text</a></code>,
<code><a href="https://developer.mozilla.org/en-US/SVG/Tutorial/Gradients">Gradient</a></code>,
<code><a href="https://developer.mozilla.org/en-US/SVG/Tutorial/Patterns">Patterns</a></code>,
<code><a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Clipping_and_masking">Clip Mask</a></code>,
<code><a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Other_content_in_SVG">Image</a></code>
</li>
<li>
<a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial">SVG Tutorial</a> – Mozilla Developer Network
</li>
</ul>
</section>
</section>
<section>
<section>
<h2>Javascript</h2>
</section>
<section data-markdown>
### Javascript
* Created in 10 days in May 1995 by Brendan Eich
* Originally developed as a prototype language for web browser (Client-side).
* Now used in server-side (Node.js) as well.
* Not related to Java, just named similarly for marketing purpose.
* C style syntax but got inspiration from Functional programming
* `for`, `while`, `continue`, `break`, `if`/`else`, `switch` are similar to C
* operators (`+`,`-`,`*`,`/`,`%`) are also similar (except `==`,`!=`,`||`)
* include function operations such as `map`, `reduce`, `forEach`.
</section>
<section data-markdown>
### Note
* Javascript world is [moving extremely fast](https://medium.com/@ericclemmons/javascript-fatigue-48d4011b6fc4) with new standards and tools introduced all the time.
* Javascript Next include versions of new syntax release annually (ES2015, ES2016, and ES2017). To use these new syntax, take a look at [Babel](https://babeljs.io/).
* [Typescript](https://www.typescriptlang.org/) is a version of Javascript with static typing, built by Microsoft.
* For this tutorial, we will only cover classic Javascript (ES5).
* See our [wiki](https://github.com/uwdata/d3-tutorials/wiki) for more resources.
</section>
<section>
<h3>Client-side Javascript: Inline/Include</h3>
<p>Inline</p>
<pre><code class="html" data-trim>
<script>
//Put Javascript code here
</script>
</code></pre>
<p>Include</p>
<pre><code class="html" data-trim>
<script src="main.js"></script>
</code></pre>
</section>
<section>
<h3>Data Types</h3>
<p>
<ul>
<li><i>Numbers</i> - 42, 3.14159</li>
<li><i>Logical</i> - true, false</li>
<li><i>Strings</i> - "Hello", 'Hello'</li>
<li>
<code>null</code>
</li>
<li>
<code>undefined</code>* - Yes. <code>undefined</code> is not <code>null</code>!
</li>
<li>
<code>Objects</code>
</li>
<li>
<code>functions</code>
</li>
</ul>
</p>
<p class="see-also"><small>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals">Values, Variable, Literals</a>–MDN</small></p>
</section>
<section>
<h3>Variables</h3>
<pre><code class="javascript" data-trim>
var a;
console.log("The value of a is " + a); // logs "The value of a is undefined"
console.log("The value of b is " + b); // throws ReferenceError exception
b = 5 // declare global variable ... equivalent to window.b = 5
console.log(b, window.b) // 5, 5
</code></pre>
<br>
Javascript uses dynamic typing.
<pre>
<code id="js-dynamic" class="javascript" data-trim>
x = "The answer is " + 42; // "The answer is 42"
"37" - 7; // 30
"37" + 7; // "377"
"1.1" + "1.1"; //= "1.11.1"
(+"1.1") + (+"1.1"); // 2.2
+"1.1" // 1.1 "+" operator converts string to number
</code>
</pre>
<small>Tips: operator can return Object too.</small>
<pre><code class="javascript" data-trim>
y = null || "String"
y // "String"
</code></pre>
</section>
<section>
<h3>Control Flow</h3>
<p>C-Style `for`, `while`, `continue`, `break`, `if`/`else`, `switch/case`</p>
<pre><code class="javascript" data-trim>
for (i=0; i<10; i++) {
if (condition) {
statement_1_runs_if_condition_is_true();
break;
} else {
statement_2_runs_if_condition_is_false();
continue;
}
}
</code></pre>
<p><small>(I'm not showing <code>while</code>, <code>switch/case</code> here for brevity.)</small></p>
</section>
<section data-markdown>
### Object
* An object in javascript is an associative array of property names (Strings) and values (Objects).
* Everything except null and undefined) can be treated as objects.
* Object can be used as <a href="http://en.wikipedia.org/wiki/Hashmap">hashmaps</a>.
* Object can be created using (a) literals (b) new
</section>
<section>
<h3>Objects: Literals & new</h3>
<pre><code class="javascript" data-trim>
//create object using an object literal
var apple = {
// quotes not required for a property name
state: "CA",
// unless the name contains space(s) or precedes with number or the name is reserved word
"famous founder": "Steve Jobs",
// property value can be any type including function
getCity: function(){return "Cupertino";},
// nested object
boards: { "CEO": "Tim Cook"}
};
apple.state; // "CA"
apple["state"]; // "CA"
apple.getCity(); // "Cupertino"
apple['getCity'](); // "Cupertino"
apple.boards.CEO // "Tim Cook"
</code></pre>
<pre><code class="javascript" data-trim>
//create object using new instead
var microsoft = new Object();
microsoft.state = "WA";
microsoft['famous founder'] = "Bill Gates";
microsoft.getCity = function(){ return "Redmond";};
</code></pre>
</section>
<section>
<h3><code>for .. in</code></h3>
<p>Careful: use <code>hasOwnProperty</code> to check</p>
<pre><code class="javascript" data-trim>
var obj = {a:1, b:2, c:3}, key;
Object.prototype.crazy = 4;
for (key in obj) {
if(obj.hasOwnProperty(key)){ //avoid the inherited obj.crazy=4
console.log(key, obj[key]); // "a, 1" "b, 2", "c, 3"
}
}
</code></pre>
</section>
<section>
<h3>Arrays</h3>
<pre><code class="javascript" data-trim>
var numbers = [ 5, 10, 15, 20, 25];
numbers[1] // 10
numbers[2] // 15
numbers.length = 5
2 in numbers // true
5 in numbers // false
numbers.a = 5 // Array is an object
numbers.a // 5
'a' in numbers // true
</code></pre>
<pre><code class="javascript" data-trim>
var numbers = [ 5, 10, , 20, 25, , ];
numbers[1] // 10
numbers[2] // undefined
numbers.length //6 last empty comma is automatically ignored
</code></pre>
<pre>
<code data-trim>
// array with multiple types of values
var mashup = [ 1,3, 4.5, 5.6, "string", null, undefined, true ];
</code>
</pre>
</section>
<section>
<h3>Function</h3>
<p>First class object...</p>
<pre><code class="javascript" data-trim>
function foo(a1, a2){
// code
console.log(a1+a2);
}
</code></pre>
<small>same as</small>
<pre><code class="javascript" data-trim>
var foo = function(a1, a2){
// code
console.log(a1+a2);
};
</code></pre>
<pre><code class="javascript" data-trim>
foo(a1,a2); // call
</code></pre>
<pre><code class="javascript" data-trim>
function g(f){ f(1,2); }
g(foo) // 3
</code></pre>
<p class="see-also"><small>
See also:
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Using_the_arguments_object">Arguments Objects</a>
</small></p>
</section>
<section>
<h3>Scope</h3>
<ul>
<li>Javascript has function level scope, not block level scope*.</li>
<li>The inner function full access to all the variables and functions defined inside the outer function. But the converse is not true.</li>
</ul>
<pre><code class="javascript" data-trim>
var name = "Andy"
var pet = function(name) { // The outer function defines a variable called "name"
var getName = function() {
return name; // The inner function has access to the "name" variable of the outer function, which is "Vivie" in this case.
}
return getName; // Return the inner function, thereby exposing it to outer scopes
};
var myPet = pet("Vivie");
myPet(); // Returns "Vivie"
console.log(name); // "Andy"
</code></pre>
<p class="see-also"><small>*ES2016 and TypeScript include block-level scoping.</small></p>
</section>
<section>
<h3>Scope</h3>
<p>Javascript has function level scope, <b>not block level scope*</b>.</p>
<pre><code class="javascript" data-trim>
var x = 1;
{
var x = 2;
}
alert(x); // outputs 2
</code></pre>
<p class="see-also"><small>*ES2016 and TypeScript include block-level scoping.</small></p>
</section>
<section>
<h3>Scope: Hoisting</h3>
<pre><code class="javascript" data-trim >
console.log(x === undefined); // logs "true"
var x = 3;
</code></pre>
<p>is equivalent to</p>
<pre><code class="javascript" data-trim>
var x;
console.log(x === undefined); // logs "true"
x = 3;
</code></pre>
</section>
<section>
<h3>IIFE</h3>
<h4>Immediately Invoked Function Expression</h4>
<div>
<div class="left">
<p>
<pre><code class="javascript" data-trim>
var elems = document.getElementsByTagName( 'a' );
for ( var i = 0; i < elems.length; i++ ) {
elems[ i ].addEventListener( 'click', function(e){
e.preventDefault();
alert( 'I am link #' + i );
}, 'false' );
}
</code></pre>
</p>
<p>
<small class="fragment">The value of `i` never
gets locked in. Thus, every link, when clicked (well after the loop
has finished executing), alerts the total number of elements.</small>
</p>
</div>
<div class="right fragment">
<p>
<pre><code class="javascript" data-trim>
var elems = document.getElementsByTagName( 'a' );
for ( var i = 0; i < elems.length; i++ ) {
(function( lockedInIndex ){
elems[ i ].addEventListener( 'click', function(e){
e.preventDefault();
alert( 'I am link #' + lockedInIndex );
}, 'false' );
})( i );
}
</code></pre>
</p>
<p>
<small class="fragment">This works, because inside the IIFE, the value of `i` is locked in as
`lockedInIndex`.</small>
</p>
</div>
</div>
<div class="see-also"><small>
See also: <a href="http://benalman.com/news/2010/11/immediately-invoked-function-expression/">Ben Alman's Post</a>
</small></div>
</section>
<section>
<h3>IIFE: Anonymous Namespaces</h3>
<p>Use IIFE to avoid namespace clashing!</p>
<pre><code class="javascript" data-trim>
(function() {
// a self contained "namespace"
// PUT YOUR NONE PUBLIC SCRIPT HERE
// ...
window.foo = function() {
// an exposed closure
};
})(); // execute the function immediately
</code></pre>
<!-- <p><small>For public namespace, use nested objects. See <a href="https://github.com/mbostock/d3/blob/master/d3.js">d3.js</a>, for example.</small></p> -->
</section>
<section>
<h3><code>this</code></h3>
<pre><code class="javascript" data-trim>
var test = {};
this; // this = global object aka window
foo(); // this = global object again
test.foo(); // this = test
new foo(); // this = newly created object
</code></pre>
<p>Explicity Setting of <code>this</code></p>
<pre><code class="javascript" data-trim>
function foo(a, b, c) {
// this = bar here when foo is called from apply and call below
}
var bar = {};
foo.apply(bar, [1, 2, 3]); // array will expand to the below
foo.call(bar, 1, 2, 3); // results in a = 1, b = 2, c = 3
</code></pre>
<p><small>Source: <a href="http://bonsaiden.github.io/JavaScript-Garden/#function.this">Javascript Garden</a></small></p>
</section>
<section>
<h3>"<code>that</code>"</h3>
<pre><code class="javascript" data-trim>
Foo = {a:1};
Foo.method = function() {
var that = this;
function test() {
// Use that instead of this here
// because here this is set to the global object
console.log(this, that); // Window, {a:1, method: function}
}
test();
}
</code></pre>
</section>
<section>
<h3>OOP in Javascript: using Constructor</h3>
<pre><code class="javascript" data-trim>
function Person(gender) {
this.gender = gender;
}
Person.prototype.sayHello = function(){
alert ('hello');
};
var person1 = new Person('Male'), person2 = new Person('Female');
// call the Person sayHello method.
person1.sayHello(); // hello
</code></pre>
<p><small>Source: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript">Mozilla</a></small></p>
</section>
<section>
<h3>OOP in Javascript: Inheritance</h3>
<p>Extend Person</p>
<pre><code class="javascript" data-trim>
function Person(gender) {
this.gender = gender;
}
Person.prototype.sayHello = function(){
alert ('hello');
};
</code></pre>
<pre><code class="javascript" data-trim>
// inherit Person
Student.prototype = new Person();
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
// replace the sayHello method
Student.prototype.sayHello = function(){
alert('hi, I am a student');
}
var student1 = new Student();
student1.sayHello();