-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
573 lines (482 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>An introduction to javascript for newbies</title>
<meta name="description" content="An introduction to javascript for newbies">
<meta name="author" content="https://plus.google.com/+GabrieleRabbiosi">
<meta name="google-site-verification" content="7e9Q1ZOEXFr2CYeqIvg5kJZwI6lOpmNmWdQuXn0ndsg" />
<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">
<link rel="stylesheet" href="css/custom.css">
<!-- 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>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-41055822-3', 'redaemn.github.io');
ga('send', 'pageview');
</script>
</head>
<body>
<div class="reveal">
<div class="fixed-header">
<div class="container">
<div class="social">
<span style="margin-right: 10px;">If you find this presentation useful, <em>please</em> let me know</span>
<div class="social-buttons">
<iframe src="http://ghbtns.com/github-btn.html?user=redaemn&repo=javascript-introduction&type=watch&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="110" height="20"></iframe>
<div class="g-plusone" data-annotation="inline" data-width="210" data-href="http://redaemn.github.io/javascript-introduction/"></div>
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://redaemn.github.io/javascript-introduction/" data-via="redaemn">Tweet</a>
</div>
</div>
<div class="author-info">
made with ♡ by <a href="https://github.com/redaemn" target="_blank">ReDaemn</a> using <a href="https://github.com/hakimel/reveal.js" target="_blank">reveal.js</a>
</div>
</div>
</div>
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Javascript</h1>
<blockquote cite="http://javascript.crockford.com/popular.html">
<p>
“The World's Most Misunderstood Programming Language (Has Become the World's Most Popular Programming Language)”
</p>
<p>
<small>
-- Douglas Crockford --
<br/>
<a href="http://javascript.crockford.com/" target="_blank">http://javascript.crockford.com/</a>
</small>
</p>
</blockquote>
<aside class="notes">
Despite its popularity, few know that JavaScript is a very nice dynamic object-oriented general-purpose programming language.<br/>
You can do anything with it!! (cloud9, this presentation!!)
</aside>
</section>
<section>
<h2>Presentation Goal</h2>
<ul>
<li class="fragment">Explain some <strong>peculiar concepts</strong> about JavaScript, not found in any classical OO language ...</li>
<li class="fragment">
... so that you will be able to understand <em>what</em> this code does and <em>why</em> it works
<pre><code data-trim>
/* The .bind method from Prototype.js */
Function.prototype.bind = function(){
var fn = this,
args = Array.prototype.slice.call(arguments),
object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
</code></pre>
</li>
</ul>
<aside class="notes">
At the end of this presentation, you should be able to go out there, pick the source code of a javascript framework (jQuery, Kendo, ...), read it and understand why it works (and hopefully learn many more interesting things!!)
</aside>
</section>
<section>
<h2 style="text-transform: none;">function()</h2>
<ul>
<li class="fragment">
function declaration
<pre style="width: auto;"><code data-trim>
function diameter(radius) {
return 2 * radius;
}
</code></pre>
</li>
<li class="fragment">
function expression
<pre style="width: auto;"><code data-trim>
var diameter = function (radius) {
return 2 * radius;
};
</code></pre>
</li>
</ul>
<aside class="notes">
- functional language, functions are first class citizens<br/>
- note the semicolon (;) at the end of the expression
</aside>
</section>
<section>
<h2 style="text-transform: none;">function.call()</h2>
<ul>
<li>
directly using parentheses
<pre><code data-trim>
diameter(2);
</code></pre>
</li>
<li class="fragment">
using <code>Function</code> object methods
<pre><code data-trim>
diameter.call(null, 2);
diameter.apply(null, [2]);
</code></pre>
</li>
<li class="fragment">
The <code>arguments</code> object
<pre><code data-trim>
function test() {
console.log(arguments);
}
test();
test("a");
test("a", 5);
</code></pre>
</li>
</ul>
<aside class="notes">
JavaScript performs no check on the number of arguments. Any formal parameter that does not receive an actual value will have undefined as its value.
</aside>
</section>
<section>
<h2>scope</h2>
<ul>
<li>
NO block scope
<pre style="width: auto;"><code data-trim>
function test() {
/* console.log(i);
console.log(j); */
for (var i = 1; i < 5; i++) {
var j = i+1;
}
console.log(i);
console.log(j);
}
test();
</code></pre>
</li>
<li class="fragment">
just two types of scope
<ul>
<li>function scope</li>
<li>global scope</li>
</ul>
</li>
</ul>
<aside class="notes">
Indeed, a variable is defined even BEFORE its "var" definition, show a demo. We'll see why<br/>
We'll see what global scope means in a moment
</aside>
</section>
<section>
<section>
<h2>Javascript execution</h2>
<ul>
<li class="fragment">
Execution context
<pre><code data-trim contenteditable>
function adder(base) {
return function(num) {
return base + num;
};
}
</pre></code>
</li>
<li class="fragment">
Variable Object (Activation Object)
</li>
<li class="fragment">
Global Object
</li>
</ul>
<aside class="notes">
- a function's execution context delimits a function's scope and includes everything that's inside curly brackets ({}) in a function's definition (it's defined statically, based on how the code is written)<br/>
- An execution context has a variable object. Any variables and functions defined inside the function are added as properties on this object<br/>
- Before running any code, the JavaScript engine creates a global object whose initial properties are the built-ins defined by ECMAScript, such as Object, String, Array and others, in addition to host defined properties<br/>
- all these are NOT objects usable by your program, they're used by the javascript runtime to execute the code<br/>
</aside>
</section>
<section>
<h2>Javascript execution</h2>
<ol>
<li class="fragment highlight-current-blue">
For any formal parameters, add corresponding properties on the variable object and let their values be the values passed as arguments to the function.
</li>
<li class="fragment highlight-current-blue">
For any function declarations, add corresponding properties on the variable object whose values are the functions. If a function declaration uses the same identifier as one of the formal parameters, the property is overwritten.
</li>
<li class="fragment highlight-current-blue">
For any variable declarations, add corresponding properties on the variable object and initialize the properties to <code>undefined</code>, regardless of how the variables are initialized in source code. If a variable uses the same identifier as an already defined property (i.e., a parameter or function), do not overwrite it.
</li>
</ol>
<aside class="notes">
Now you undestand the behaviour seen previously. <br/>
Best practice to avoid problems: declare all variables at the start of a scope (i.e. a function)
</aside>
</section>
</section>
<section>
<h2>Scope Chain</h2>
<ul>
<li>
Whenever a function is called, control enters a new execution context.
</li>
<li class="fragment">
The activation object is used for identifier resolution inside the function. In fact, <strong>identifier resolution occurs through the scope chain</strong>, which starts with the activation object of the current execution context. At the end of the scope chain is the global object.
</li>
</ul>
<pre class="fragment"><code data-trim>
function adder(base) {
return function(num) {
return base + num;
};
}
var inc = adder(1);
console.log(inc(5));
</code></pre>
</section>
<section>
<h2>Javascript objects <span style="text-transform: none;">(intro)</span></h2>
<p>
Container of key-value pairs
</p>
<pre><code data-trim>
var o1 = {
name: “circle”,
radius: 2,
diameter: function () { /* ... */ }
};
console.log(o1.name);
var o2 = {
“invalid-key-constant”: “value”
};
console.log(o2[“invalid-key-constant”]);
</code></pre>
</section>
<section>
<section>
<h2>The <code style="text-transform: none;">this</code> keyword</h2>
<ul>
<li>
<code>this</code> is set <strong>implicitly</strong> when calling a function using parentheses
</li>
<li class="fragment">
calling it as a function causes <code>this</code> to be set to the <strong>global object</strong>
<pre><code data-trim contenteditable>
var a = "a";
function test() {
return this.a;
}
test();
</code></pre>
</li>
</ul>
<aside class="notes">
this is not caused by the fact that the "test" function is declared in the same scope of the "a" variable; show an example with a function defined inside "test" function
</aside>
</section>
<section>
<h2>The <code style="text-transform: none;">this</code> keyword</h2>
<ul>
<li>
calling it as a method causes <code>this</code> to be the <strong>object through which the function is called</strong>
<pre><code data-trim>
var person = {
name: "World",
sayHi: function() {
return "Hello " + this.name;
}
};
person.sayHi();
</code></pre>
</li>
<li class="fragment">
allows JavaScript objects to <strong>share function objects</strong> and still have them <strong>execute on the right object</strong>
<pre><code data-trim>
var person2 = {
name: "Gabriele",
sayHi: person.sayHi
};
person2.sayHi();
</code></pre>
</li>
</ul>
</section>
<section>
<h2>The <code style="text-transform: none;">this</code> keyword</h2>
<ul>
<li><strong>Explicitly</strong> setting <code>this</code></li>
<pre style="width: 110%;"><code data-trim>
person.sayHi.call({ name: "John" });
</code></pre>
</ul>
</section>
</section>
<section>
<h2>Javascript is single-threaded</h2>
<img src="http://ejohn.org/files/Timers.png" alt="Javascript timers" style="height: 500px;">
<small>Image taken from <a href="http://ejohn.org/blog/how-javascript-timers-work/" target="_blank">http://ejohn.org/blog/how-javascript-timers-work/</a></small>
</section>
<section>
<h2>Closure</h2>
<pre><code data-trim>
function adder(base) {
return function(num) {
return base + num;
};
}
</pre></code>
<ul>
<li class="fragment">inside the anonymous function returned by <code>adder</code>, the <code>base</code> variable is known as a <em>free variable</em></li>
<li class="fragment">it <strong>lives on</strong> after the <code>adder</code> function has finished executing, <strong>as long as someone holds a reference</strong> to the returned function</li>
</ul>
<aside class="notes">
JavaScript supports nested functions, which allows for closures that can keep private state, and can be used to implement many useful patterns
</aside>
</section>
<section>
<h2>Stateful Functions</h2>
<ul>
<li>A closure can <strong>maintain state through its free variables</strong>.</li>
<li class="fragment">The scope chain that allows access to these free variables is only accessible from within the scope chain itself, which means that free variables by definition are <strong>private</strong>.</li>
</ul>
<pre class="fragment"><code data-trim>
function uniqueIdGenerator(start) {
var currentId = start || 0;
return function() {
return currentId++;
}
}
</pre></code>
<aside class="notes">
Every new execution of "uniqueIdGenerator" creates a new execution context and a new scope chain; this means that a generator's state cannot interfere with the state of a previously created generator
</aside>
</section>
<section>
<h2>Immediately Called Anonymous Functions</h2>
<pre><code data-trim>
(function () {
/* ... */
}());
</code></pre>
<ul>
<li class="fragment"><strong>avoid littering the global scope</strong> with temporary variables by simply wrapping our code in a self-executing closure</li>
<li class="fragment">simulate block scope</li>
</ul>
<pre class="fragment"><code data-trim>
var names = ['John', 'Gabriele'];
(function() {
var i;
for (i = 0; i < names.length; i++) {
console.log("Hello " + names[i]);
}
}());
console.log(i);
</code></pre>
<aside class="notes">
JavaScript only has global scope and function scope, we need to avoid leaking objects into the global scope because doing so increases our chances of naming collisions with other scripts (and bugs in our scripts)<br/>
This pattern is used EVERYWHERE, see some jQuery plugin (i.e. cycle jquery plugin)
</aside>
</section>
<section>
<h2>Namespaces</h2>
<p>
A good strategy to stay out of the global scope is to use some kind of namespacing
</p>
<pre class="fragment"><code data-trim>
var reply = {
applicationConfig: {
config1: "...",
config2: 42
}
customCalendar: function() { /* ... */ }
};
</code></pre>
<aside class="notes">
You could create your personal framework and it should have a single entry point stored inside the global object.<br/>
This pattern too is used EVERYWHERE (jQuery, Kendo, ...)
</aside>
</section>
<section>
<h2>Resources and Credits</h2>
<div>
<small><em>(in random order)</em></small>
</div>
<ul>
<li><a href="http://javascript.crockford.com/" target="_blank">http://javascript.crockford.com/</a></li>
<li><strong>JavaScript: The Good Parts</strong> <em>by Douglas Crockford</em></li>
<li>
<strong>Test-Driven JavaScript Development</strong> <em>by Christian Johansen</em>
(<a href="http://tddjs.com/" target="_blank">http://tddjs.com/</a>)
</li>
<li><a href="https://developer.mozilla.org" target="_blank">https://developer.mozilla.org</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript" target="_blank">A re-introduction to JavaScript (JS Tutorial)</a></li>
</ul>
</section>
<section>
<h2>Final quiz</h2>
<a href="http://madebyknight.com/javascript-scope/" target="_blank">Start!</a>
</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({
maxScale: 2.0,
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
// Parallax scrolling
// parallaxBackgroundImage: 'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg',
// parallaxBackgroundSize: '2100px 900px',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.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: '//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.10/socket.io.min.js', async: true },
{ src: 'plugin/multiplex/client.js', async: true }
],
multiplex: {
// Example values. To generate your own, see the socket.io server instructions.
secret: null, // Obtained from the socket.io server. Gives this (the master) control of the presentation
id: '41da9d963d1e786f', // Obtained from socket.io server
url: 'http://javascript-introduction-c9-redaemn.c9.io:80' // Location of socket.io server
}
});
</script>
<!-- G+ button -->
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
<!-- Twitter button -->
<script>
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');
</script>
</body>
</html>