forked from girldevelopit/gdi-featured-js-intro
-
Notifications
You must be signed in to change notification settings - Fork 100
/
class3.html
467 lines (429 loc) · 17.2 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 3 ~ JavaScript for Beginners ~ Girl Develop It</title>
<meta name="description" content="This is an introduction to JavaScript curriculum, developed by Sylvia Richardson for the Raleigh/Durham 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="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/gdidefault.css" id="theme">
<link rel="stylesheet" href="css/custom.css">
<!-- For syntax highlighting -->
<!-- light editor<link rel="stylesheet" href="lib/css/light.css">-->
<!-- dark editor--><link rel="stylesheet" href="reveal/lib/css/dark.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="reveal/css/print/pdf.css" type="text/css" media="print">
<!--[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>
<img src="images/gdi_logo_badge.png" alt="GDI logo">
<h3>JavaScript for Beginners</h3>
<h4>Class 3</h4>
</section>
<section>
<h3>Objects</h3>
<p>Objects let us store a collection of properties.</p>
<pre><code>
var objectName = {
propertyName: propertyValue,
propertyName: propertyValue,
...
};
</code></pre>
<pre><code>
var aboutMe = {
hometown: 'Atlanta, GA',
hair: 'Auburn',
likes: ['knitting', 'code'],
birthday: {month: 10, day: 17}
};
</code></pre>
</section>
<section>
<h3>Accessing Objects</h3>
<p>You can retrieve values using "dot notation"</p>
<pre><code>
var aboutMe = {
hometown: 'Atlanta, GA',
hair: 'Auburn'
};
var myHometown = aboutMe.hometown;
</code></pre>
<p>Or using "bracket notation" (like arrays)</p>
<pre><code>
var myHair = aboutMe['hair'];
</code></pre>
</section>
<section>
<h3>Changing Objects</h3>
<p>You can use dot or bracket notation to change properties</p>
<pre><code>
var aboutMe = {
hometown: 'Atlanta, GA',
hair: 'Auburn'
};
aboutMe.hair = 'blue';
</code></pre>
<p>Add new properties</p>
<pre><code>
aboutMe.gender = 'female';
</code></pre>
<p>Or delete them</p>
<pre><code>
delete aboutMe.gender;
</code></pre>
</section>
<section>
<h3>Arrays of Objects</h3>
<p>Since arrays can hold any data type, they can also hold objects</p>
<pre><code>
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>Just like other data types, objects can be passed into functions:</p>
<pre><code>
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>Create an object to hold information on your favorite recipe. It should have properties for recipeTitle (a string), servings (a number), and ingredients (an array of strings).</p>
<p>Try displaying some information about your recipe.</p>
<p>Bonus: Create a loop to list all the ingredients.</p>
</section>
<section>
<h3>Object methods</h3>
<p>Objects can also hold functions.</p>
<pre><code>
var lizzieTheCat = {
age: 18,
furColor: 'grey',
meow: function() {
console.log('meowww');
},
eat: function(food) {
console.log('Yum, I love ' + food);
},
};
</code></pre>
<p>Call object methods using dot notation:</p>
<pre><code>
lizzieTheCat.meow();
lizzieTheCat.eat('brown mushy stuff');
</code></pre>
</section>
<section>
<h3>Built-in methods</h3>
<p>JS provides several built-in objects:</p>
<ul>
<li><code><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array">Array</a></code></li>
<li><code><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code></li>
<li><code><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number">Number</a></code></li>
<li><code><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String">String</a></code></li>
<li><code><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp">RegExp</a></code></li>
<li><code><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date">Date</a></code></li>
<li><code><a target="_blank" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math">Math</a></code></li>
</ul>
</section>
<section>
<h3>Review: Anatomy of a website</h3>
<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>
<p>A website is a way to present your content to the world, using HTML and CSS to present that content & make it look good.</p>
</section>
<section>
<h3>IDs vs. Classes</h3>
<div class = "left-align">
<span class = "yellow">ID</span> -- Should only apply to one element on a webpage, e.g., A webpage only has one footer.
<p>The "#" is how you tell CSS "this is an id."</p>
</div>
<div class = "left-align">
<span class = "yellow">Class</span> -- Lots of elements can have the same class, e.g., There can be many warnings on one webpage.
<p>The "." is how you tell CSS "this is a class name."</p>
</div>
</section>
<section>
<h3>The DOM Tree</h3>
<p>We model the nested structure of an HTML page with the DOM (Document Object Model) Tree. The browser makes a "map" of all the elements on a page.</p>
</section>
<section>
<h3>The DOM: Sample Code</h3>
<pre><code>
<!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>
<!--Accessing the DOM-->
<section>
<h3>DOM Access</h3>
<p>Your browser automatically builds a Document object to store the DOM of a page. To change a page:</p>
<ol>
<li>Find the DOM node 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>You can find nodes by id using the method:</p>
<pre><code>
document.getElementById(id);
</code></pre>
<p>To find:</p>
<pre><code>
<img id="kittenPic" src="http://placekitten.com/200/300" alt="cat"/>
</code></pre>
<p>We would use:</p>
<pre><code>
var imgKitten = document.getElementById('kittenPic');
</code></pre>
</section>
<section>
<h3>DOM Access: By Tag Name</h3>
<p>You can certain types of HTML elements using this method:</p>
<pre><code>
document.getElementsByTagName(tagName);
</code></pre>
<p>To find:</p>
<pre><code>
<li>Daisy</li>
<li>Tulip</li>
</code></pre>
<p>We would use:</p>
<pre><code>
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>In newer browsers, you can use other methods too.</p>
<p>Available in <a href="http://caniuse.com/#search=getElementsByClassName">IE9+, FF3.6+, Chrome 17+, Safari 5+</a>:</p>
<pre><code>
document.getElementsByClassName(className);
</code></pre>
<p>Available in <a href="http://caniuse.com/#search=querySelectorAll">IE8+, FF3.6+, Chrome 17+, Safari 5+</a>:</p>
<pre><code>
document.querySelector(cssQuery);
document.querySelectorAll(cssQuery);
</code></pre>
</section>
<section>
<h3>getElement vs. getElements</h3>
<p>Any method that starts with "getElement" will return a <span class = "blue">single</span> node.</p>
<pre><code>
document.getElementById('uniqueID'); //returns a single node
</code></pre>
<p>Any method that starts with "getElements" will return a <span class = "blue">array</span> of nodes. To modify a single node, you will need to use bracket notation to select the correct one.</p>
<pre><code>
document.getElementsByTagName('p'); //returns multiple nodes
var specficParagraph = document.getElementsByTagName('p')[2];
</code></pre>
</section>
<section>
<h3>DOM Nodes: Attributes</h3>
<p>You can access and change attributes of DOM nodes using dot notation.</p>
<p>To change this element:</p>
<pre><code>
<img id="kittenPic" src="http://placekitten.com/200/300" alt="cat"/>
</code></pre>
<p>We could change the src attribute this way:</p>
<pre><code>
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>You can also use <code>getAttribute/setAttribute</code></p>
<pre><code>
<img id="kittenPic" src="http://placekitten.com/200/300" alt="cat"/>
</code></pre>
<p>We could change the src attribute this way:</p>
<pre><code>
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>You can change page css using<code>style</code></p>
<p>To make this CSS:</p>
<pre><code>
body {
color: red;
}
</code></pre>
<p>Use this JavaScript:</p>
<pre><code>
var pageNode = document.getElementsByTagName('body')[0];
pageNode.style.color = 'red';
</code></pre>
</section>
<section>
<h3>DOM Nodes: More Styles</h3>
<p>Change any CSS property with a "-" to camelCase, and be sure to include a unit on any number</p>
<p>To make this CSS:</p>
<pre><code>
body {
background-color: pink;
padding-top: 10px;
}
</code></pre>
<p>Use this JavaScript:</p>
<pre><code>
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>Create a simple HTML page or <a href="files/class3.zip">use this sample code</a>.</p>
<p>Isolate a node (an element on the page) and change an attribute or add a new style.</p>
</section>
<section>
<h3>DOM innerHTML</h3>
<p>Each DOM node has an <code>innerHTML</code> property with the HTML of all its children. You can use the property to view or change the HTML of a node.</p>
<p>For example, you can overwrite the entire body:</p>
<pre><code>
var pageNode = document.getElementsByTagName('body')[0];
pageNode.innerHTML = '<h1>Oh Noes!</h1> <p>I just changed the whole page!</p>'
</code></pre>
<p>Or just add some new content to the end</p>
<pre><code>
pageNode.innerHTML += '...just adding this bit at the end of the page.';
</code></pre>
</section>
<section>
<h3>DOM innerHTML continued</h3>
<p>You can also target a particular element</p>
<p>To fill this HTML element:</p>
<pre><code>
<p id="warning"></p>
</code></pre>
<p>We can select the node and modify it</p>
<pre><code>
var warningParagraph = document.getElementById('warning');
warningParagraph.innerHTML = 'Danger Will Robinson!';
</code></pre>
</section>
<section>
<h3>Creating New Nodes</h3>
<p>The <code>document</code> object also provides ways to create nodes from scratch:</p>
<pre><code>
document.createElement(tagName);
document.createTextNode(text);
document.appendChild();
</code></pre>
</section>
<section>
<h3>Creating New Nodes: Sample Code</h3>
<pre><code>
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>Create a new paragraph element and add it to a div on your page.</p>
</section>
<section>
<h3>Resources</h3>
<ul>
<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">Code Academy</a>, with interactive JavaScript lessons to help you review.</li>
</ul>
</section>
</div><!-- Close .slides -->
<footer>
<div class="copyright">
JavaScript for Beginners ~ 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="reveal/lib/js/head.min.js"></script>
<script src="reveal/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,
touch: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>