forked from gdi-curriculum-review/gdi-jquery-intro
-
Notifications
You must be signed in to change notification settings - Fork 2
/
class3.html
502 lines (470 loc) · 23.6 KB
/
class3.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to JavaScript : Class 3 ~ Girl Develop It</title>
<meta name="description" content="This is an introduction to JavaScript curriculum, developed by Sylvia Richardson for the Raleigh/Durham chapter, modified by Julie Cameron and Sara Gibbons for the Ann Arbor chapter. The course is meant to be taught in 4 two-hour sections. Each of the slides and practice files are customizable according to the needs of a given class or audience.">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/gdiaa.css" id="theme">
<link rel="stylesheet" href="lib/css/light.css">
<link rel="stylesheet" href="css/print/pdf.css" media="print">
<script src="lib/js/head.min.js"></script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<!-- Opening slide -->
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to JavaScript</span></h3>
<h4><span class="blue">Class 3</span></h4>
</section>
<section>
<h3>Review</h3>
<ul class="copy--small box--small list--tall">
<li>Variable Scope</li>
<li>if / else if / else</li>
<li>Logical Operators (<code>&&</code>, <code>||</code>, <code>!</code>)</li>
<li>While Loops, For Loops, and Break</li>
<li>Array</li>
</ul>
</section>
</section>
<section>
<section>
<h3>Objects</h3>
<p class="copy--small box--small">Objects let us store a collection of properties.</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
var objectName = {
propertyName: propertyValue,
propertyName: propertyValue,
// ...
};
</code></pre>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
var aboutMe = {
hometown: 'Ann Arbor, MI',
hair: 'Auburn',
likes: ['kittens', 'code'],
birthday: {month: 10, day: 17}
};
</code></pre>
</section>
<section>
<h3>Accessing Objects</h3>
<p class="copy--small box--small">You can retrieve values using "dot notation"</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
var aboutMe = {
hometown: 'Ann Arbor, MI',
hair: 'Auburn'
};
var myHometown = aboutMe.hometown;
</code></pre>
<p class="copy--small box--small">Or using "bracket notation"</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
var myHair = aboutMe['hair'];
</code></pre>
</section>
<section>
<h3>Changing Objects</h3>
<p class="copy--small box--small">You can use dot or bracket notation to change properties</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable class="javascript">
var aboutMe = {
hometown: 'Ann Arbor, MI',
hair: 'Auburn'
};
aboutMe.hair = 'blue';
</code></pre>
<p class="copy--small box--small">Add new properties</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable class="javascript">
aboutMe.gender = 'female';
</code></pre>
<p class="copy--small box--small">Or delete them</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable class="javascript">
delete aboutMe.gender;
</code></pre>
</section>
<section>
<h3>Arrays of Objects</h3>
<p class="copy--small box--small">Since arrays can hold any data type, they can also hold objects</p>
<pre style="height: 260px; overflow: hidden;"><code contenteditable>
var myCats = [
{ name: 'Lizzie', age: 18 },
{ name: 'Daemon', age: 1 }
];
for (var i = 0; i < myCats.length; i++) {
var myCat = myCats[i];
console.log(myCat.name + ' is ' + myCat.age + ' years old.');
}
</code></pre>
</section>
<section>
<h3>Arrays of Objects</h3>
<p class="copy--small box--small">Just like other data types, objects can be passed into functions:</p>
<pre style=""><code contenteditable>
var lizzieTheCat = {
age: 18,
furColor: 'grey',
likes: ['catnip', 'milk'],
birthday: { month: 7, day: 17, year: 1996 }
}
function describeCat(cat) {
console.log('This cat is ' + cat.age + ' years old with '
+ cat.furColor + ' fur.');
}
describeCat(lizzieTheCat);
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Create an object to hold information on your favorite recipe.</p>
<p class="copy--small box--small">It should have properties for <code>recipeTitle</code> (a string), <code>servings</code> (a number), and <code>ingredients</code> (an array of strings).</p>
<p class="copy--small box--small">Try displaying some information about your recipe.</p>
<p class="copy--small box--small">Bonus: Create a loop to list all the ingredients.</p>
</section>
</section>
<section>
<section>
<h3>Object methods</h3>
<p class="copy--small box--small">Objects can also hold functions.</p>
<pre style="height: 250px; overflow: hidden;"><code contenteditable>
var lizzieTheCat = {
age: 18,
furColor: 'grey',
meow: function() {
console.log('meowww');
},
eat: function(food) {
console.log('Yum, I love ' + food);
},
};
</code></pre>
<p class="copy--small box--small">Call object methods using dot notation.</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
lizzieTheCat.meow();
lizzieTheCat.eat('brown mushy stuff');
</code></pre>
</section>
<section>
<h3>Built-in methods</h3>
<p class="copy--small box--small">JS provides several built-in objects:</p>
<ul class="copy--small box--small list--tall">
<li><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array">Array</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number">Number</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp">RegExp</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date">Date</a></li>
<li><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math">Math</a></li>
</ul>
</section>
</section>
<section>
<section>
<h3>Review: Anatomy of a website</h3>
<div class="box"><div class = "blue"> Your Content</div>
<div><span class = "blue">+ HTML: </span>Structure</div>
<div><span class = "blue">+ CSS: </span>Presentation</div>
<div class = "blue">= Your Website</div></div>
<p class="copy--small box--small">A website is a way to present your content to the world, using HTML to structure the content & CSS to make it look good.</p>
</section>
<section>
<h3>IDs vs. Classes</h3>
<div class = "left-align box copy--small">
<span class = "yellow">ID</span> -- Should only apply to one element on a webpage. For instance, a webpage only has one footer.
<p class="copy--small box--small">The <code>#</code> is how you tell CSS "this is an id."</p>
</div>
<div class = "left-align copy--small">
<span class = "yellow">Class</span> -- Lots of elements can have the same class. For instance, there can be many warnings on one webpage.
<p class="copy--small box--small">The <code>.</code> is how you tell CSS "this is a class name."</p>
</div>
</section>
<section>
<h3>The DOM Tree</h3>
<p class="copy--small box--small">We model the nested structure of an HTML page with<br/>the DOM (Document Object Model) Tree.</p>
<p class="copy--small box--small">This is the browser's "map" of all the elements on a page.</p>
</section>
<section>
<h3>The DOM: Sample Code</h3>
<pre ><code contenteditable>
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<style>
h1 {
color: red;
}
</style>
</head>
<body>
<h1>My Page</h1>
<p>Hello World!</p>
<img src="http://placekitten.com/200/300" alt="cat"/>
</body>
</html>
</code></pre>
</section>
<section>
<h3>The DOM: Sample Model</h3>
<img src="images/dom-tree.png" alt="Simple DOM Tree"/>
</section>
</section>
<!--Accessing the DOM-->
<section>
<section>
<h3>DOM Access</h3>
<p class="copy--small box--small">Your browser automatically builds a<br/>Document Object to store the DOM of a page.</p>
<p class="copy--small box--small">To change a page:</p>
<ol class="copy--small box--small list--tall">
<li>Find the DOM <i>"node"</i> and store it in a variable</li>
<li>Use methods to manipulate the node</li>
</ol>
</section>
<section>
<h3>DOM Access: By Id</h3>
<p class="copy--small box--small">You can find nodes by id using the method:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
document.getElementById(id);
</code></pre>
<p class="copy--small box--small">To find:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
<img id="kittenPic" src="http://placekitten.com/200/300" alt="cat"/>
</code></pre>
<p class="copy--small box--small">We would use:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
var imgKitten = document.getElementById('kittenPic');
</code></pre>
</section>
<section>
<h3>DOM Access: By Tag Name</h3>
<p class="copy--small box--small">You can find certain types of HTML elements using this method:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
document.getElementsByTagName(tagName);
</code></pre>
<p class="copy--small box--small">To find:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
<li>Daisy</li>
<li>Tulip</li>
</code></pre>
<p class="copy--small box--small">We would use:</p>
<pre style="height: 120px; overflow: hidden;"><code contenteditable>
var listItems = document.getElementsByTagName('li');
for (var i = 0; i < listItems.length; i++) {
var listItem = listItems[i];
}
</code></pre>
</section>
<section>
<h3>DOM Access: HTML 5</h3>
<p class="copy--small box--small">In <i>modern</i> browsers, you can use other methods too.</p>
<p class="copy--small box--small">Available in <a href="http://caniuse.com/#search=getElementsByClassName">IE9+, FF3.6+, Chrome 17+, Safari 5+</a>:</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
document.getElementsByClassName(className);
</code></pre>
<p class="copy--small box--small">Available in <a href="http://caniuse.com/#search=querySelectorAll">IE8+, FF3.6+, Chrome 17+, Safari 5+</a>:</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
document.querySelector(cssQuery);
document.querySelectorAll(cssQuery);
</code></pre>
</section>
<section>
<h3>getElement vs. getElements</h3>
<p class="copy--small box--small">Any methods that starts with "getElement"<br/>will return a <span class = "blue">single</span> node.</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
document.getElementById('uniqueID'); //returns a single node
</code></pre>
<p class="copy--small box--small">Any method that starts with "getElements"<br/>will return a <span class = "blue">array</span> of nodes.</p>
<p class="copy--small box--small">To modify a single node, you will need to use bracket notation to select the correct one.</p>
<pre style="height: 100px; overflow: hidden;"><code contenteditable>
document.getElementsByTagName('p'); //returns multiple nodes
var specficParagraph = document.getElementsByTagName('p')[2];
</code></pre>
</section>
<section>
<h3>DOM Nodes: Attributes</h3>
<p class="copy--small box--small">You can access and change attributes<br/>of DOM nodes using dot notation.</p>
<p class="copy--small box--small">To change this element:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
<img id="kittenPic" src="http://placekitten.com/200/300" alt="cat"/>
</code></pre>
<p class="copy--small box--small">We could change the src attribute this way:</p>
<pre style="height: 100px; overflow: hidden;"><code contenteditable>
var imgKitten = document.getElementById('kittenPic');
var oldSrc = imgKitten.src;
imgKitten.src = 'http://placekitten.com/100/500';
</code></pre>
</section>
<section>
<h3>DOM Nodes: Getting and Setting Attributes</h3>
<p class="copy--small box--small">You can also use <code contenteditable>getAttribute/setAttribute:</code></p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
<img id="kittenPic" src="http://placekitten.com/200/300" alt="cat"/>
</code></pre>
<p class="copy--small box--small">We could change the src attribute this way:</p>
<pre style="height: 100px; overflow: hidden;"><code contenteditable>
var imgKitten = document.getElementById('kittenPic');
var oldSrc = imgKitten.getAttribute('src');
imgKitten.setAttribute('src', 'http://placekitten.com/100/500');
</code></pre>
</section>
<section>
<h3>DOM Nodes: Styles</h3>
<p class="copy--small box--small">You can change page CSS using <code contenteditable>style</code></p>
<p class="copy--small box--small">To apply these CSS styles:</p>
<pre style="height: 100px; overflow: hidden;"><code contenteditable>
body {
color: red;
}
</code></pre>
<p class="copy--small box--small">You could use this JavaScript:</p>
<pre style="height: 100px; overflow: hidden;"><code contenteditable>
var pageNode = document.getElementsByTagName('body')[0];
pageNode.style.color = 'red';
</code></pre>
</section>
<section>
<h3>DOM Nodes: More Styles</h3>
<p class="copy--small box--small">Reference CSS properties named with a <code>-</code> via <i>"camelCase"</i>.</p>
<p class="copy--small box--small">Be sure to include a unit on any number</p>
<p class="copy--small box--small">To apply these CSS styles:</p>
<pre style="height: 120px; overflow: hidden;"><code contenteditable>
body {
background-color: pink;
padding-top: 10px;
}
</code></pre>
<p class="copy--small box--small">You could use this JavaScript:</p>
<pre style="height: 100px; overflow: hidden;"><code contenteditable>
var pageNode = document.getElementsByTagName('body')[0]
pageNode.style.backgroundColor = 'pink';
pageNode.style.paddingTop = '10px';
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Create a simple HTML page or <a href="files/class3.zip">use this sample code.</a></p>
<p class="copy--small box--small">Isolate a node (an element on the page)<br/>and change an attribute or add a new style.</p>
</section>
</section>
<section>
<section>
<h3>DOM innerHTML</h3>
<p class="copy--small box--small">Each DOM node has an <code contenteditable>innerHTML</code> property containing the HTML of all its children. You can use this property to view or change the HTML of a node.</p>
<p class="copy--small box--small">For example, you can overwrite the entire body:</p>
<pre style="height: 120px; overflow: hidden;"><code contenteditable class="javascript">
var pageNode = document.getElementsByTagName('body')[0];
pageNode.innerHTML = '<h1>Oh Noes!</h1> <p>I just changed the whole page!</p>'
</code></pre>
<p class="copy--small box--small">Or just add some new content to the end:</p>
<pre style="height: 120px; overflow: hidden;"><code contenteditable class="javascript">
pageNode.innerHTML += '...just adding this bit at the end of the page.';
</code></pre>
</section>
<section>
<h3>DOM innerHTML continued</h3>
<p class="copy--small box--small">You can also target a particular element</p>
<p class="copy--small box--small">To fill this HTML element:</p>
<pre style="height: 120px; overflow: hidden;"><code contenteditable>
<p id="warning"></p>
</code></pre>
<p class="copy--small box--small">We can select the node and modify it:</p>
<pre style="height: 120px; overflow: hidden;"><code contenteditable>
var warningParagraph = document.getElementById('warning');
warningParagraph.innerHTML = 'Danger Will Robinson!';
</code></pre>
</section>
<section>
<h3>Creating New Nodes</h3>
<p class="copy--small box--small">The <code contenteditable>document</code> object also provides<br/>ways to create nodes from scratch:</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
document.createElement(tagName);
document.createTextNode(text);
document.appendChild();
</code></pre>
</section>
<section>
<h4>Creating New Nodes: Sample Code</h4>
<pre style="height: 300px; overflow: hidden;"><code contenteditable>
var pageNode = document.getElementsByTagName('body')[0];
var newImg = document.createElement('img');
newImg.src = 'http://placekitten.com/400/300';
newImg.style.border = '1px solid black';
pageNode.appendChild(newImg);
var newParagraph = document.createElement('p');
var paragraphText = document.createTextNode('Squee!');
newParagraph.appendChild(paragraphText);
pageNode.appendChild(newParagraph);
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Create a new paragraph element and add it to a div on your page.</p>
</section>
</section>
<section>
<section>
<h3>Resources</h3>
<ul class="copy--small list--tall box--small">
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide" target="_blank">JavaScript Guide</a>, from the Mozilla Developers Network.</li>
<li><a href="http://www.codecademy.com/tracks/javascript" target="_blank">Codecademy</a>, with interactive JavaScript lessons to help you review.</li>
</ul>
</section>
<section>
<h3>Practice!</h3>
<div class="box">
<a href="homework.html#class3">Optional homework!!</a>
<p class="box"><a href="https://gdiaa.slack.com/messages/0416-javascript-wkshp/details/" target="_blank" class="roll"><span>#0416-javascript-wkshp</span></a></p>
<p><small>Not on Slack? <a target="_blank" href="http://bit.ly/gdiaa-slack" class="roll"><span data-title="bit.ly/gdiaa-slack">bit.ly/gdiaa-slack</span></a></small></p>
</div>
</section>
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to JavaScript</span></h3>
<h4><a href="class4.html"><span class="blue">Class 4 »</span></a></h4>
</div>
</section>
</section>
</div><!-- Close .slides -->
<footer>
<div class="copyright">
Intro to JavaScript ~ Girl Develop It ~
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div><!-- Close .copyright -->
</footer>
</div><!-- Close .reveal -->
<script src="js/jquery.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
rollingLinks: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>