-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop.html
553 lines (457 loc) · 21 KB
/
oop.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>meet javascript - oop</title>
<meta name="description" content="an introductive javascript presentation">
<meta name="author" content="gradinariu bogdan">
<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="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<style type="text/css">
h2{
text-transform: none !important;
}
.main-title{
color : black;
}
.reveal .slides section .fragment.highlight-white{
color : #999;
opacity : 1;
}
.reveal .slides section .fragment.highlight-white.visible{
color : #fff;
}
.align-left{
text-align : left!important;
}
.gray {
color : #999;
}
.gray span{
color : #fff;
}
.del{
text-decoration: line-through;
}
/* section iframe{
visibility: hidden;
}
.show-iframe iframe{
visibility: visible!important;
}*/
iframe.demo{
min-height : 250px;
min-width: 400px;
background: rgba(0,0,0,.1);
}
.demo-image{
width : 100%;
height: auto;
}
</style>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Meet javascript - oop</h1>
<p>
<small> Sources are available at <a href="https://github.com/gion/meet-javascript" target="_blank">https://github.com/gion/meet-javascript</a></small>
<small>Created by <a href="mailto:bogdan.gradinariu@xivic.com" target="_blank">Bogdan Gradinariu</a> with <a href="https://github.com/hakimel/reveal.js" target="_blank">reveal.js</a>.</small>
</p>
</section>
<section>
<h2>part 2 summary</h2>
<ul>
<li class="fragment grow">IIFE</li>
<li class="fragment grow">closures</li>
</ul>
</section>
<section>
<section>
<h2>syntax traps</h2>
<p class="fragment roll-in">the following function is syntactically correct:</p>
<pre class="fragment roll-in"><code>function stupidIdentity(a, b){
if(typeof(b, a) != 'undefined' && a instanceof(a.constructor))
delete(a, b);
return(b, a);
}</code></pre>
<pre class="fragment roll-in"><code>stupidIdentity(1, 2); // 1;
stupidIdentity('a'); // 'a';
stupidIdentity([1, 2, 3], 4, 5, 6); // [1, 2, 3];</code></pre>
</section>
<section>
<h2>syntax traps</h2>
<p>is <i>typeof</i> a function or an operator? </p>
<pre class="fragment roll-in"><code>typeof(13); // number
typeof 13; // number</code></pre>
</section>
<section>
<h2>syntax traps</h2>
<p>is <i>return</i> a function or an operator? </p>
<pre class="fragment roll-in"><code>function identity(a){return(a);}
identity(13); // 13
function identity2(a){return a;}
identity2("foo"); // "foo"</code></pre>
<div class="fragment roll-in">
<p>what about this one</p>
<pre class><code>function identity3(a, b, c){return(a, b, c);}
identity3(1); // undefined
identity3(1, 2, 3); // 3</code></pre>
<div class="fragment roll-in">
<p>or this one</p>
<pre class><code>typeof(((('something')))); // 'string'</code></pre>
</div>
</div>
</section>
<section>
<h2><span class="main-title">syntax traps: </span>the comma operator</h2>
<pre class="fragment roll-in"><code>var a = 7, 5; // SyntaxError: Unexpected number
var a = (7, 5);
a; // 5
var x, y, z;
x = (y=1, z=4);
x; // 4
y; // 1
z; // 4</code></pre>
<pre class="fragment roll-in"><code>// single element between round brackets
var foo = ('bar');
foo == 'bar'; // true
foo == ('bar'); //true
123 == (123); // true
typeof(((('something')))) == typeof 'something'; // true</code></pre>
</section>
<section>
<h2><span class="main-title">syntax traps: </span>the comma operator</h2>
<h4>real life usage</h4>
<ul>
<li style="min-width:650px;">
<pre class="fragment roll-in"><code>while(a++, b--, c=Math.random(), b>a && c>0.5)</code></pre>
</li>
<li style="min-width:650px;">
<pre class="fragment roll-in"><code>for(i=n; n--, n>i; i++)</code></pre>
</li>
</ul>
</section>
</section>
<section>
<h2>oop</h2>
<p class="fragment">working with objects != <i>oop</i></p>
<pre class="fragment roll-in" ><code style="min-height:550px">var singleton = {
key : value
};
function maker(name, where, when){
return {
name : name,
where : where,
// getter for the "private" var "some"
getWhen : function(){
// because of the closure, this function has access
// to the outer-scope
return when;
}
}
}
var student = maker('Bob', 'school', 'every day');</code></pre>
</section>
<section>
<section>
<h2><span class="main-title">oop: </span> constructors</h2>
<ul>
<li class="fragment">are nothing but simple functions</li>
<li class="fragment">they create and return a new object</li>
<li class="fragment">inside the constructor, the newly created object can be referentiated through <i>this</i></li>
<li class="fragment">the constructors <u><i>must</i></u> be called with the <i>new</i> keyword</li>
</ul>
<pre class="fragment roll-in"><code>function Point(x, y){
this.x = x;
this.y = y;
// no return statement required
}
var origin = new Point(0, 0);
origin.x; // 0
origin.y++; // 1</code></pre>
</section>
<section>
<h2><span class="main-title">oop: </span> constructors</h2>
<p>if a constructor is called withouth the <i>new</i> keyword </p>
<ul>
<li class="fragment">a new object is <u>not</u> created</li>
<li class="fragment"><i>this</i> points to the outer context</li>
</ul>
<pre class="fragment roll-in"><code>var x = "ics",
invalidPoint = Point(0, 0);
// TypeError: Cannot read property 'x' of undefined
invalidPoint.x;
// TypeError: Cannot read property 'y' of undefined
invalidPoint.y++;</code></pre>
<div class="fragment roll-in">
<p>but wait, there's more</p>
<pre ><code>x == "ics"; // false
window.y; // 0</code></pre>
</div>
</section>
<section>
<h2><span class="main-title">oop: </span> constructors</h2>
<p>rewrite the constructor to check the "presence" of <i>new</i></p>
<pre class="fragment roll-in"><code>function Point(x, y){
if(!(this instanceof Point))
throw new Error('forgot new!');
this.x = x;
this.y = y;
}
var p = Point(-1, 1); // Error : forgot new!</code></pre>
</section>
<section>
<h2><span class="main-title">oop: </span> constructors</h2>
<p>use <i>makers</i> (a.k.a. <i>factories</i>) instead of raw constructors</p>
<pre class="fragment roll-in"><code>function makePoint(x, y){
return new Point(x, y);
}
var p = makePoint(-1, 1),
p2 = new makePoint(13, -1);
p instanceof Point; // true
p2 instanceof Point; // true
p.x == -1; // true
p2.x == 13; // true</code></pre>
<div class="fragment roll-in">
<p> even jQuery does it</p>
<pre ><code>console.log($);
// output
function (a,b){return new e.fn.init(a,b,h)}</code></pre>
</div>
</section>
</section>
<section>
<section>
<h2><span class="main-title">oop: </span> inheritance</h2>
<ul>
<li class="fragment">objects are created with a "secret link" to another object</li>
<li class="fragment">if not specified oterwise, the "secret link" is to the base object "class" called <i>Object</i> </li>
<li class="fragment">if an attempt to acces a name fails, the secret linked object will be used</li>
<li class="fragment">the secret link is not used when storing; new members are only added to the primary object</li>
</ul>
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance</h2>
<pre class="fragment roll-in"><code>var personDefinition = {
age : 0,
name : 'no name',
sayHello : function(){
return "Hi, I'm " + this.name;
}
}
// the constructor for "Person" objects
var Person = function(){};
// add the hidden link
Person.prototype = personDefinition;
</code></pre>
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance</h2>
<pre><code>// notice the "new" keyword
var Bob = new Person();</code></pre>
<img class="demo-image fragment roll-in" src="demos/proto1.png" alt="" >
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance</h2>
<pre><code>Bob.age === undefined; // false
Bob.name === undefined; // false
Bob.firstName === undefined; // true</code></pre>
<img class="demo-image fragment roll-in" src="demos/proto2.png" alt="" >
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance</h2>
<pre><code>Bob.name = "Bob";
Bob.name === personDefinition; // false
Bob.sayHello(); // "Hi, I'm Bob"</code></pre>
<img class="demo-image fragment roll-in" src="demos/proto3.png" alt="" >
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance</h2>
<pre><code>Bob.sayHello(); // "Hi, I'm Bob"
delete personDefinition.sayHello;
Bob.sayHello(); // TypeError: Object has no method 'sayHello'</code></pre>
<img class="demo-image fragment roll-in" src="demos/proto4.png" alt="" >
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance</h2>
<pre><code>Bob.toString(); // ???</code></pre>
<img class="demo-image fragment roll-in" src="demos/proto5.png" alt="" >
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance</h2>
<pre><code>Bob.toString(); // "[object Object]"</code></pre>
<img class="demo-image fragment roll-in" src="demos/proto6.png" alt="" >
</section>
</section>
<section>
<section>
<h2><span class="main-title">oop: </span> inheritance patterns</h2>
<p>say we have the <i>Mammal</i> "class" and we want to extend it and make a new one called "Cat"</p>
<pre class="fragment roll-in"><code>// Mammal constructor
function Mammal(name){
this.name = name;
}</code></pre>
<pre class="fragment roll-in"><code>// Cat constructor
function Cat(name, color){
// same logic as in Mammal constructor
this.name = name;
// additional cat logic
this.color = color;
}
// make "Cat" a "Mammal"
Cat.prototype = new Mammal();</code></pre>
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance patterns</h2>
<p>let's DRY the cat a bit</p>
<pre class="fragment roll-in"><code>function Mammal(name){
this.name = name;
}
// constructor
function Cat(name, color){
// apply the mammal constructor logic
// this points to the newly created Cat instance
Mammal.apply(this, arguments);
// additional cat logic
this.color = color;
}
// make "Cat" a "Mammal"
Cat.prototype = new Mammal();</code></pre>
<pre class="fragment roll-in"><code>// link the constructor back to "Cat"
Cat.prototype.constructor = Cat;
// create a helpful "super" link
Cat.prototype.parent = Mammal.prototype;</code></pre>
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance patterns</h2>
<p>extend methods</p>
<pre class="fragment roll-in"><code>Mammal.prototype.sayHello = function(){
return "Hi";
}
Cat.prototype.sayHello = function(){
return "Hi and Miauu!";
}</code></pre>
</section>
<section>
<h2><span class="main-title">oop: </span> inheritance patterns</h2>
<p>DRY the cat a bit more</p>
<pre class="fragment roll-in"><code>Mammal.prototype.sayHello = function(){
return "Hi";
}
Cat.prototype.sayHello = function(){
var superHello = this.parent.sayHello.apply(this, arguments);
return superHello + ' and Miauu!';
}</code></pre>
</section>
</section>
<section>
<section>
<h2><span class="main-title">oop: </span> an <i>inherit</i> function</h2>
<pre class="fragment roll-in"><code>function inherit(subClass, superClass){
subClass.prototype = new superClass();
subClass.prototype.constructor = subClass;
subClass.prototype.parent = superClass.prototype;
}</code></pre>
</section>
<section>
<h2><span class="main-title">oop: </span> an <i>inherit</i> function</h2>
<pre class="fragment roll-in"><code>function Mammal(name){
this.name = name;
}
Mammal.prototype.sayHello = function(){
return "Hi, I'm " + this.name;
}
function Cat(name, color){
if(this.parent)
this.parent.constructor.apply(this, arguments);
this.color = color;
}
// make Cat inherit from Mammal
inherit(Cat, Mammal);</code></pre>
</section>
<section>
<h2><span class="main-title">oop: </span> an <i>inherit</i> function</h2>
<pre class="fragment roll-in"><code>var c = new Cat('Tom', 'blue');
c instanceof Cat; // true
c instanceof Mammal; // true
c.name; // "Tom"
c.color; // "blue"
c.sayHello(); // "Hi, I'm Tom"</code></pre>
</section>
</section>
<section>
<section>
<h2>Homework</h2>
<p class="fragment"> consider the following Person "class" : <p>
<pre class="fragment roll-in"><code>function Person(options){
// initialize instance members
this.name = options.name || "no name";
this.age = options.age || 0;
}
Person.prototype = {
talk : function(){
return "Hello, I'm " + this.name;
}
};</code></pre>
</section>
<section>
<h2><span class="main-title">Homework: </span> tasks</h2>
<ul>
<li class="fragment roll-in">extend the <i>Person</i> in order to make a "Worker" class, that
<ul>
<li>has a new member called <i>action</i></li>
<li>has a new method called <i>work</i>, that returns a string that describes his work (action)</li>
</ul>
</li>
<li class="fragment roll-in">extend the <i>Worker</i> in order to make a "Programmer" class, that
<ul>
<li>has the <i>action</i> set to "coding"</li>
<li>has a new member called <i>language</i></li>
<li>has a new method called <i>code</i>, that returns a string that describes his work and coding language</li>
</ul>
</li>
</ul>
</section>
</section>
</div>
</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,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'concave', // default/cube/page/concave/zoom/linear/fade/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; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>