-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
600 lines (517 loc) · 19 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Object-Oriented JavaScript - Girl Develop It Philly</title>
<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.css">
<link rel="stylesheet" href="css/theme/gdisunny.css" id="theme">
<link rel="stylesheet" href="css/theme/styles.css">
<!-- For syntax highlighting -->
<!-- light editor--><link rel="stylesheet" href="lib/css/light.css">
<!-- dark editor<link rel="stylesheet" href="lib/css/dark.css">-->
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="css/print/pdf.css" type="text/css" media="print">
<link rel="icon" type="image/x-icon" href="css/images/favicon.ico" />
<!--[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">
<!-- Introduction -->
<section>
<h3>Intro to Object-Oriented JavaScript</h3>
<div>Slides: http://gdiphilly.github.io/Intro-to-OOJS</div>
<img src="css/images/gdi_logo_badge.png" alt="Girl Develop It" style='height: 250px; width: 250px; border: 0px;'>
<br/>
<div style='align: center;'>"Object-oriented programming is programming <br/>oriented towards objects." - my coworker</div>
</section>
<section>
<h3>Welcome!</h3>
<div class="left-align">
<p>Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.</p>
<br/>
<p>Some "rules":</p>
<ul>
<li>We are here for you</li>
<li>Every question is important</li>
<li>Help each other</li>
<li>Have fun!</li>
</ul>
</div>
</section>
<section>
<h3>Welcome!</h3>
<div class="left-align">
<p>Tell us about yourself.</p>
<ul>
<li>Who are you?</li>
<li>What do you hope to get out of the class?</li>
<li>Fun Question Of The Day ... </li>
</ul>
</div>
</section>
<!-- End Introduction -->
<section>
<h3>What we'll cover today</h3>
<div class='left-align'>
<ul>
<li>What is an object?</li>
<li>What are attributes and methods?</li>
<li>What is class-based inheritance?</li>
<li>What is prototype-based inheritance?</li>
<li>How do we create objects using inheritance in JavaScript?</li>
<li>How can we extend a prototype in JavaScript?</li>
<li>How do we write maintanable code?</li>
<li>How can we refactor a website's code with Object-Oriented JavaScript?</li>
</ul>
</div>
</section>
<section>
<h3>Warm up exercises</h3>
<div class='left-align'>
<p>Goal: Understanding objects</p>
<ul>
<li>Create an object with a couple properties. For example, you could make a "student" object with the properties "name" and "hometown".</li>
<li>Use console.log() to print out a sentence that includes the properties of that object. For example, "[name] is from [hometown]."</li>
</ul>
</div>
</section>
<section>
<h3>Warm up exercises</h3>
<div class='left-align'>
<p>Goal: Understanding functions</p>
<ul>
<li>Create a function that takes at least one argument. For example, you could make an "announce" function that alerts "Important announcement:" followed by the argument.</li>
<li>Call your function with an argument of your choice. For example, you could announce "Dogs are cute".</li>
</ul>
</div>
</section>
<section>
<h3>Warm up exercises</h3>
<div class='left-align'>
<p>Goal: Understanding "this"</p>
<ul>
<li>In HTML, create a link to the website of your choice. Using a JavaScript "onclick" event, make clicking on the link alert the link’s "href". [Hint: never used the "onclick" event before, and not sure how to use it? Check out w3schools.com for <a href="http://www.w3schools.com/jsref/event_onclick.asp">some documentation and examples</a>]</li>
</ul>
</div>
</section>
<section>
<h3>What is an "Object"?</h3>
<div class='left-align'>
<p>In computer science, an <strong>object</strong> is a location in memory having a value and possibly referenced by an identifier. An object can be a variable, function, or data structure. [source: Wikipedia]</p>
<pre><code contenteditable class='javscript'>
var carObject = {
make: 'Volkswagen',
model: 'Rabbit',
drive: function() {
// Code that makes car go forward
}
};
</code></pre>
</div>
</section>
<section>
<h3>Attributes and Methods</h3>
<div class='left-align'>
<p>When we talk about objects, we're interested in two main concepts:</p>
<ul>
<li class='fragment'><strong>Attributes</strong> - object characteristics</li>
<li class='fragment'><strong>Methods</strong> - object functionality</li>
</ul>
<pre class='fragment'><code contenteditable class='javscript'>
var carObject = {
make: 'Volkswagen', // attribute
model: 'Rabbit', // attribute
drive: function() { // method
// Code that makes car go forward
}
};
</code></pre>
</div>
</section>
<section>
<h3>Group exercise: Shapes</h3>
<div class='left-align'>
<p>Think about these geometric shapes:</p>
</div>
<br/>
<p>circle, square, rectangle, star, rhombus, triangle</p>
<br/>
<div class='left-align'>
<ul>
<li>What are some <strong>attributes</strong> shared by <em>all</em> shapes?</li>
<li>What are some <strong>methods</strong> that can be applied to <em>all</em> shapes?</li>
<li>Are there any attributes or methods that are unique to <em>only one</em> shape? ... to <em>some</em> shapes?</li>
</ul>
</section>
<section>
<h3>Class-based inheritance</h3>
<div class='left-align'>
<p>Imagine going shopping for a new car.</p>
<ul>
<li class='fragment'>You go to the dealer with a concept of a "car" is.</li>
<li class='fragment'>You find a particular car with qualities you like.</li>
<li class='fragment'>You like the way it handles and drives.</li>
<li class='fragment'>You purchase this particular car.</li>
</ul>
<br/>
<br/>
<p class='fragment'>In object-oriented terms, we say that you have purchased an <strong>instance</strong> of the <strong>class of objects</strong> known as <strong>cars</strong>.</p>
</div>
</section>
<section>
<h3>Prototypal inheritance</h3>
<div class='left-align'>
<p>Let's imagine another way of shopping for a new car.</p>
<ul>
<li class='fragment'>You go to the dealer with an old car you already own, and you ask the dealer for a newer model of the same car.</li>
<li class='fragment'>The color, make, and model of the car are the same, but the handling is better than your old car.</li>
<li class='fragment'>You purchase this particular car.</li>
</ul>
<br/>
<br/>
<p class='fragment'>In object-oriented terms, we say that you have purchased an <strong>instance</strong> of a car based off of a <strong>prototype</strong>, from which it <strong>inherited</strong> some attributes and methods.</p>
</div>
</section>
<section>
<h3>Creating new objects</h3>
<div class='left-align'>
<p>In JavaScript, we use a <strong>constructor function</strong> to initialize new objects, and we use the <strong>new</strong> keyword to call the constuctor.
<pre class='fragment'><code contenteditable class='javscript'>
// Create a constructor function for new cars
function Car() {
// Set attributes of all new cars
this.odometer = 0;
}
var myCar = new Car();
// myCar can access properties we defined in Car
console.log(myCar.odometer); // 0
</code></pre>
</div>
</section>
<section>
<h3>Adding Variables</h3>
<div class='left-align'>
<pre><code contenteditable class='javscript'>
// Create a constructor function for new cars
function Car(make, model, year) {
// Set the given attributes of this object
this.make = make;
this.model = model;
this.year = year;
// Set attributes of all new cars, independent of variables
this.odometer = 0;
}
var myCar = new Car('Volkswagen', 'Rabbit', 2006);
// myCar can access properties we defined in Car
console.log(myCar.odometer); // 0
console.log(myCar.make); // Volkswagen
</code></pre>
</div>
</section>
<section>
<h3>Extending the prototype</h3>
<div class='left-align'>
<p>The <strong>prototype</strong> is the collection of all the attributes and methods that the object knows about.</p>
<br/>
<p>The <strong>prototype</strong> property of an object is used primarily for <strong>inheritance</strong>: methods and attributes on a function's prototype property are <strong>available to instances</strong> of that function.</p>
</div>
</section>
<section>
<h3>Extending the Prototype</h3>
<div class='left-align'>
<pre><code contenteditable class='javscript'>
function Car() {
this.odometer = 0;
}
// Extend the prototype to define methods for new Cars
Car.prototype.drive = function() {
this.odometer = this.odometer + 1;
};
// Instantiate a new Car
var myCar = new Car();
// myCar can access methods defined in the prototype
console.log(myCar.odometer); // 0
myCar.drive();
console.log(myCar.odometer); // 1
</code></pre>
</div>
</section>
<section>
<h3>Pro style tip</h3>
<div class='left-align'>
<p>It's common practice for constructor function names to start with an <strong>uppercase</strong> letter.</p>
<p>It's common practice for regular function names to start with a <strong>lowercase</strong> letter.</p>
<pre class='fragment'><code contenteditable class='javscript'>
// capitalized function "Car"? This must be a constructor
function Car() {
this.odometer = 0;
}
// lowercase function "drive"? This must be a regular function
Car.prototype.drive = function() {
this.odometer = this.odometer + 1;
};
</code></pre>
</div>
</section>
<section>
<h3>Putting it all together</h3>
<div class='left-align'>
<pre><code contenteditable class='javscript'>
// Create a constructor function for new cars
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.odometer = 0;
}
// Extend the prototype to define methods for new cars
Car.prototype.drive = function() {
this.odometer = this.odometer + 1;
};
// Instantiate a new car
var myCar = new Car('Volkswagen', 'Rabbit', 2006);
</code></pre>
</div>
</section>
<section>
<h3>Coding exercise 1</h3>
<div class='left-align'>
<p>Let's make our own constructors and objects! We'll be using an exercise put together by GDI Chicago:</p>
<br />
<div><a target='_blank' href='http://bit.ly/oojs_ex_1'>http://bit.ly/oojs_ex_1</a></div>
</div>
</section>
<section>
<h3>Extending objects</h3>
<div class='left-align'>
<p>Suppose you wanted an easy way to talk about different types of cars.</p>
<ul>
<li>All cars start with a make, model, year, and odometer.
<li>Sedan-type cars have 4 doors
<li>Hatchback-type cars can have 3 doors or 5 doors
</ul>
</div>
</section>
<section>
<h3>Creating a hatchback</h3>
<div class='left-align'>
<p>We can create a constructor function for each sub-type of Car that inherits attributes and methods from Car.</p>
<pre><code contenteditable class='javscript'>
// Assume Car() is defined as before
function Hatchback(make, model, year, doors) {
// Call the Car constructor on the attributes
// Important: "this" is the first argument
Car.call(this, make, model, year);
// Set Hatchback-specific properties
this.doors = doors;
}
// Tell the Hatchback to extend the Car object
Hatchback.prototype = Object.create(Car.prototype);
</code></pre>
</div>
</section>
<section>
<h3>Creating a hatchback</h3>
<div class='left-align'>
<p>We can also create methods specific to Hatchback.</p>
<pre><code contenteditable class='javscript'>
function Hatchback(make, model, year, doors) {
Car.call(this, make, model, year);
this.doors = doors;
}
Hatchback.prototype = Object.create(Car.prototype);
// Now add a Hatchback-specific function
Hatchback.prototype.openHatch = function() {
console.log('You opened the hatch!');
};
var myHatch = new Hatchback('Volkswagen', 'Rabbit', 2006, 3);
myHatch.drive();
myHatch.openHatch();
</code></pre>
</div>
</section>
<section>
<h3>Substitutability</h3>
<div class='left-align'>
<p><strong>Substitutability</strong> is a principle of object-oriented programming introduced by Barbara Liskov in a 1987 conference keynote entitled "Data abstraction and hierarchy".</p>
<br/>
<p>In a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program.</p>
</div>
</section>
<section>
<h3>Coding exercise 2</h3>
<div class='left-align'>
<p>Let's extend our Video to make a MusicVideo! We'll be using another exercise put together by GDI Chicago:</p>
<br />
<div><a target='_blank' href='http://bit.ly/oojs_ex_2'>http://bit.ly/oojs_ex_2</a></div>
</div>
</section>
<section>
<h3>Uses for object-oriented programming</h3>
<div class='left-align'>
<ul>
<li class='fragment'>Model physical concepts more clearly in your code
<li class='fragment'>Make your code easier to read
<li class='fragment'>Create code that is more easily maintainable and reusable
</ul>
</div>
</section>
<section>
<h3>Maintainable code</h3>
<div class='left-align'>
<p>When creating an object using our Car constructor, we have to pass in our arguments in a very specific order: make, then model, then year.</p>
<pre class='fragment'><code contenteditable class='javscript'>
var myCar = new Car('Volkswagen', 'Rabbit', 2006);
</code></pre>
<p class='fragment'>This is annoying and hard to maintain.</p>
<br/>
<p class='fragment'>There is a better way.</p>
</div>
</section>
<section>
<h3>A single argument</h3>
<div class='left-align'>
<p>Replace your <strong>list</strong> of arguments with a <strong>single</strong> argument.</p>
<pre><code contenteditable class='javscript'>
// The better way
function Car(args) {
this.make = args.make;
this.model = args.model;
this.year = args.year;
}
// Instantiate your object
// NOTE: You can now list the arguments in any order!
var myCar = new Car({
model: 'Rabbit',
year: 2006,
make: 'Volkswagen'
});
</code></pre>
</div>
</section>
<section>
<h3>Optional default values</h3>
<div class='left-align'>
<p>You can also provide <strong>default values</strong> for your arguments within the constructor.</p>
<pre><code contenteditable class='javscript'>
// Now, with defaults!
function Car(args) {
args = args || {};
this.make = args.make || 'Unknown';
this.model = args.model || 'Unknown';
this.year = args.year || 0;
}
// Instantiate your object
// NOTE: Now there's a backup if you don't know a value
var myCar = new Car({
make: 'Volkswagen',
year: 2006
});
</code></pre>
</div>
</section>
<section>
<h3>Coding exercise 3</h3>
<div class='left-align'>
<p>Let's clean up our constructors using what we learned! We'll be using another exercise put together by GDI Chicago:</p>
<br />
<div><a target='_blank' href='http://bit.ly/oojs_ex_3'>http://bit.ly/oojs_ex_3</a></div>
</div>
</section>
<section>
<h3>Building real websites</h3>
<div class='left-align'>
<p>You can create objects that know how to interact with your website.</p>
<br />
<div><a target='_blank' href='http://bit.ly/oojs_example'>http://bit.ly/oojs_example</a></div>
<br />
<p>In a real-world situation, you would probably create your car objects using data imported from a database!</p>
</div>
</section>
<section>
<h3>Refactoring</h3>
<div class='left-align'>
<p>Once your website code is working correctly, it's a good idea to go back and <strong>refactor</strong> it. Refactoring is a process developers use to restructure their existing code to make it easier to read.</p>
<br/>
<p class='fragment'>Although it takes time to refactor your code now, right now is when you're most familiar with what you wrote.</p>
</div>
</section>
<section>
<h3>Coding exercise 4</h3>
<div class='left-align'>
<p>Let's refactor some JavaScript using objects! We'll be using another exercise put together by GDI Chicago:</p>
<br />
<div><a target='_blank' href='http://bit.ly/oojs_ex_4'>http://bit.ly/oojs_ex_4</a></div>
</div>
</section>
<!-- Outro -->
<section>
<h3>Regroup & Recap</h3>
<ul>
<li>What did you work on?</li>
<li>What was the most challenging part?</li>
<li>What did you learn?</li>
</ul>
</section>
<section>
<h3>What's next?</h3>
<div class='left-align'>
<ul>
<li class='fragment'>Lots of practice!</li>
<li class='fragment'>Intro to Model-View-Controller (MVC) frameworks
<ul>
<li>Your data goes in the "model"</li>
<li>The way to display that data goes in the "view"</li>
<li>The model and view talk to each other and your server-side data using the "controller"</li>
<li>http://todomvc.com/</li>
</ul>
</li>
<li class='fragment'>See the <a target='_blank' href='http://www.meetup.com/Girl-Develop-It-Philadelphia/pages/Suggested_Course_Sequences/'>Suggested Course Sequences</a>: Intro to {Ruby, Java, Android, iOS, Python, Angular, Node.js}
</ul>
</div>
</section>
<section>
<h3>Thank you for attending!</h3>
<p>Please help us make this class better by completing this short survey:</p>
<br/>
<div><a target='_blank' href='http://bit.ly/GDI-OOjs'>http://bit.ly/GDI-OOjs</a></div>
</section>
<!-- End of slides -->
</div>
<footer>
<div class="copyright">
Intro to Object Oriented JavaScript -- Girl Develop It Philly --
<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>
</footer>
</div>
<script src="lib/js/head.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,
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>