-
Notifications
You must be signed in to change notification settings - Fork 0
/
part-two.html
502 lines (418 loc) · 19.3 KB
/
part-two.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>meet javascript - part two</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);
}
</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 - part two</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 1 summary</h2>
<ul>
<li class="fragment grow">key concepts</li>
<li class="fragment grow">values</li>
<li class="fragment grow">identifyers & reserved words </li>
<li class="fragment grow">operators</li>
<li class="fragment grow">statements</li>
</ul>
</section>
<section>
<section>
<h2>IIFE</h2>
<ul>
<li class="fragment">
= Immediately-Invoked Function Expression
</li>
<li class="fragment">
it is usually an annonimous function that executes after it's defined
</li>
<li class="fragment roll-in">
instead of writing
<pre><code>// define the function
var f = function(){ /*...*/ };
// call the function
f();</code></pre>
we can do
<pre><code>// define and immediately call the function
(function(){ /*...*/ })();</code></pre>
</li>
</ul>
</section>
<section>
<h2>IIFE</h2>
<p>it can be written in may ways:</p>
<ul style="min-width : 400px;">
<li class="fragment roll-in">
<pre><code>function(){ /*...*/ }();</code></pre>
</li>
<li class="fragment roll-in">
<pre><code>(function(){ /*...*/ })();</code></pre>
</li>
<li class="fragment roll-in">
<pre><code>(function(){ /*...*/ }());</code></pre>
</li>
</ul>
</section>
</section>
<section>
<h2>Closures</h2>
<p class="fragment">
A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).
</p>
</section>
<section>
<section>
<h2><span class="main-title">Closures :</span> why do we need them?</h2>
<pre class="fragment roll-in"><code>var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine'];
function digitName(n){
return names[n];
}</code></pre>
<p class="fragment roll-in"><b>but...</b></p>
<p class="fragment roll-in"><i>names</i> is a global variable and anyone can modify it :
<pre class="fragment roll-in"><code>var five = digitName(5); // 'five'
names[5] = 'cinci';
console.log(five == digitName(5)); // false</code></pre>
</p>
</section>
<section>
<h2><span class="main-title">Closures :</span> why do we need them?</h2>
<pre class="fragment roll-in"><code>function digitName(n){
return digitName.names[n];
}
digitName.names = ['zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine'];
</code></pre>
<p class="fragment roll-in"><b>but...</b></p>
<p class="fragment roll-in"><i>names</i> <span class="del">is a global variable</span> is a <i>public</i> member and anyone can modify it :
<pre class="fragment roll-in"><code>var two = digitName(2); // 'five'
digitName.names[2] = 'doi';
console.log(two == digitName(2)); // false</code></pre>
</p>
</section>
<section>
<h2><span class="main-title">Closures :</span> why do we need them?</h2>
<pre class="fragment roll-in"><code>function digitName(n){
var names = ['zero', 'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine'];
return names[n];
}</code></pre>
<p class="fragment roll-in"><b>but...</b></p>
<p class="fragment roll-in"> every time the function is called, the <i>names</i> array gets reinitialised.</p>
</section>
<section>
<h2><span class="main-title">Closures :</span> why do we need them?</h2>
<pre class="fragment roll-in"><code>var digitName = (function(){
var names = ['zero', 'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine'];
return function(){
return names[n];
};
})();</code></pre>
<p class="fragment roll-in"><b>but...</b></p>
<p class="fragment roll-in">there are no buts!</p>
</section>
</section>
<section>
<h2><span class="main-title">Closures :</span> a better definition</h2>
<p class="fragment">
Conceptually, we can think of a closure as the marriage between a function and the environment in which it was declared within:
</p>
<img src="http://lostechies.com/derekgreer/files/2012/02/closure2.png" class="fragment" style="height:300px;">
</section>
<section>
<h2><span class="main-title">Closures :</span> use cases</h2>
<ul>
<li>the module pattern</li>
<li>callbacks in loops</li>
<li>iterators</li>
<li>variable shadowing</li>
<li>prevent global namespace pollution</li>
</ul>
</section>
<section>
<h2><span class="main-title">Closures.usage :</span> the module pattern</h2>
<pre class="fragment roll-in"><code>var module = (function(){
var privateVar;
function privateFunction(){ /* ... */ }
return {
// privileged method that has access to the "private" members
getPrivateVar : function(){
return privateVar;
},
setPrivateVar : function(value){
privateVar = value;
},
};
})();</code></pre>
</section>
<section>
<section>
<h2><span class="main-title">Closures.usage :</span> callbacks in loops</h2>
<pre class="fragment roll-in"><code>var buttons = document.querySelectorAll('button');
for(var i=0; i<buttons.length; i++){
buttons[i].onclick = function(){
alert('you just clicked button #' + i);
}
}
</code></pre>
<div class="fragment">
<a href="demos/closure-1.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-1.html"></iframe>
</div>
</section>
<section>
<h2><span class="main-title">Closures.usage :</span> callbacks in loops</h2>
<pre class="fragment roll-in"><code>var buttons = document.querySelectorAll('button');
for(var i=0; i<buttons.length; i++){
buttons[i].onclick = (function(index){
return function(){
alert('you just clicked button #' + index);
}
})(i);
}
</code></pre>
<div class="fragment">
<a href="demos/closure-2.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-2.html"></iframe>
</div>
</section>
<section>
<h2><span class="main-title">Closures.usage :</span> callbacks in loops</h2>
<pre class="fragment roll-in"><code>for(var i=1; i<5; i++){
setTimeout(function(){
log(i + ' second(s)');
}, i * 1000);
}
</code></pre>
<div class="fragment">
<a href="demos/closure-3.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-3.html"></iframe>
</div>
</section>
<section>
<h2><span class="main-title">Closures.usage :</span> callbacks in loops</h2>
<pre class="fragment roll-in"><code>function closureLog(seconds){
setTimeout(function(){
log(seconds + ' second(s)');
}, seconds * 1000);
}
for(var i=1; i<5; i++){
closureLog(i);
}
</code></pre>
<div class="fragment">
<a href="demos/closure-4.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-4.html"></iframe>
</div>
</section>
</section>
<section>
<h2><span class="main-title">Closures.usage :</span> iterators</h2>
<pre class="fragment roll-in"><code>function iterator(array){
var index = 0;
return function(){
return array[index++];
}
}
var arr = ['a', 'b', 'c'],
getNext = iterator(arr);
log(getNext()); // a
log(getNext()); // b
log(getNext()); // c
</code></pre>
<div class="fragment">
<a href="demos/closure-8.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-8.html"></iframe>
</div>
</section>
<section>
<section>
<h2><span class="main-title">Closures.usage :</span> variable shadowing</h2>
<pre class="fragment roll-in"><code>var a = 1,
b = (function(a){
return function(){
return ++a;
}
})(a);
log(b()); // ?
log(a); // ?
log(b()); // ?
log(b()); // ?
log(a); // ?
</code></pre>
<div class="fragment">
<a href="demos/closure-5.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-5.html"></iframe>
</div>
</section>
<section>
<h2><span class="main-title">Closures.usage :</span> variable shadowing</h2>
<pre class="fragment roll-in"><code>/*
load jQuery => $ == jQuery
and then load prototype => $ now points to prototype
execute normal jQuery code
*/
$.ready(function(){ // TypeError: object has no property 'ready'
});
</code></pre>
<div class="fragment">
<a href="demos/closure-6.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-6.html"></iframe>
</div>
</section>
<section>
<h2><span class="main-title">Closures.usage :</span> variable shadowing</h2>
<pre class="fragment roll-in"><code>/* same scenario */
// make a closure that passes jQuery as $ to its scope
(function($){
log('[inner] ' + ($ == jQuery));
})(jQuery);
log('[outer] ' + ($ == jQuery));
</code></pre>
<div class="fragment">
<a href="demos/closure-7.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-7.html"></iframe>
</div>
</section>
</section>
<section>
<h2><span class="main-title">Closures.usage :</span> prevent global namespace pollution</h2>
<pre class="fragment roll-in"><code>(function(window, undefined){
var foo = 'bar';
})(this);
log(foo); // Error
log(window.foo); // undefined
</code></pre>
<div class="fragment">
<a href="demos/closure-9.html" target="_blank">demo:</a>
<iframe class="demo" src="demos/closure-9.html"></iframe>
</div>
</section>
<section>
<section>
<h2><span class="main-title">Closures :</span> best practices</h2>
<p class="fragment">It is highly recommended that you enclose every script in a IIFE closure: </p>
<pre class="fragment roll-in"><code>;(function(window, $, OL, undefined){
var scriptName = 'someApplication',
app = {};
var init = function(){ /*...*/ };
app.init = function(){
// ...
init();
// ...
};
window[scriptName] = app;
})(this, jQuery.noConflict(), otherLib);</code></pre>
</section>
<section>
<h2><span class="main-title">Closures :</span> best practices</h2>
<p>Main advantages of this approach:</p>
<ul>
<li class="fragment">not polluting the global namespace more than necessary</li>
<li class="fragment">private & public members</li>
<li class="fragment">naming the dependencies however you feel like it</li>
<li class="fragment">the script can run in other environments (node.js, phantom.js, rhino)</li>
<li class="fragment">the code is cleaner, easier to maintain</li>
</ul>
</section>
</section>
<section>
<h2>Homework</h2>
<ul>
<li class="fragment">
write a recursive function that returns the nth number (passed as argument) of the fibonacci sequence. <br>
the function should use a closure in order to cache it's results.
</li>
</ul>
</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 || 'zoom', // 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>