-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
620 lines (514 loc) · 17.5 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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Please, explain me this!</title>
<meta name="description" content="Javascript this: how to understand it">
<meta name="author" content="Gabriele D'Arrigo - gdarrigo@ebay.com">
<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.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<style>
pre.block-code {
box-shadow: none;
}
pre.block-code code {
padding: 10px;
background-color: rgba(63, 63, 63, 0.95);
}
pre.block-code code span.line-number {
margin-right: 20px;
}
.reveal .slides section .fragment.current-visible.current-fragment {
display: block;
}
.reveal .slides section .fragment.current-visible {
display: none;
}
section a.hidden-link {
color: #FFFFFF;
text-decoration: none;
}
a.hidden-link:active,
a.hidden-link:focus,
a.hidden-link:hover,
a.hidden-link:visited {
color: #FFFFFF;
cursor: text;
text-decoration: none;
}
.background-black {
background-color: rgba(0, 0, 0, 0.9);
padding: 25px;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="intro" data-background="http://i.giphy.com/l46CsaquyQudrz3Lq.gif">
<header>
<h1>This</h1>
<h3>The most infamous Javascript keyword</h3>
</header>
<pre class="block-code">
<code class="javascript line-numbers">
var self = this; // Dammi una lametta...
</code>
</pre>
</section>
<section id="context">
<h3>
This keyword can be used everywhere, not only in objects:
</h3>
<ul>
<li>
In functions
</li>
<li>
Outside functions (top-level scope)
</li>
<li>
In a string passed to eval() (not covered <a href="#/eval" class="hidden-link">here</a>)
</li>
</ul>
</section>
<section id="function">
<section id="functions-context">
<h3>
In functions
</h3>
<p>
The most common use are:
</p>
<ul>
<li>
Functions (<strong class="hljs-string">this</strong> refers to the global object, undefined in strict mode)
</li>
<li>
Constructors (<strong class="hljs-string">this</strong> refers to the object instance)
</li>
<li>
Methods (<strong class="hljs-string">this</strong> refers to the receiver of the method call)
</li>
</ul>
</section>
<section id="modes">
<h4>
Functions
</h4>
<p>
When you call a function the value of <strong class="hljs-string">this</strong> depends on the mode:
<strong class="hljs-keyword">normal</strong> or <strong class="hljs-keyword">strict</strong>
</p>
</section>
<section id="normal-mode">
In normal execution mode <strong class="hljs-string">this</strong> point to the global object: <br />
<strong class="hljs-keyword">window</strong> in browsers, <strong class="hljs-keyword">global</strong>
in Node.js or other server side environment.
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">function log() {
console.log(this);
}
log(); // window</code>
</pre>
</section>
<section id="strict-mode">
<p>
In strict mode the value of <strong class="hljs-string">this</strong> is <strong class="hljs-keyword">undefined</strong>
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
'use strict';
function log() {
console.log(this);
}
log(); // undefined</code>
</pre>
</section>
<section id="change-value-this">
<p>
We can arbitrarily change the value of <strong class="hljs-string">this</strong> with two function:
</p>
<ul>
<li>
<strong class="hljs-keyword">Function.prototype.call</strong>
</li>
<li>
<strong class="hljs-keyword">Function.prototype.apply</strong>
</li>
</ul>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
let robot = {
name: 'Robocop'
};
function kill(toKill) {
console.log(this.name + ' will kill ' + toKill);
}
kill.call(robot, 'you') // Robocop will kill you
kill.apply(robot, ['you']) // Robocop will kill you</code>
</pre>
</section>
<section id="constructor">
<h4>
Constructors
</h4>
<p>
A constructor is a function invoked with the <strong class="hljs-keyword">new</strong> operator that
return the instance of an object.
</p>
</section>
<section id="constructor-invocation">
<p>
When a constructor is invoked with <strong class="hljs-keyword">new</strong>,
<strong class="hljs-string">this</strong> is bound to the newly created object.
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
function Dog(name) {
this.name = name;
}
let pluto = new Dog('Pluto');
pluto.name; // 'Pluto'</code>
</pre>
</section>
<section id="methods">
<h4>
Methods
</h4>
<p>
<strong class="hljs-string">this</strong> refers to the receiver, the object on which the method has been called.
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
let obj = {
prop: 'a prop',
method() {
console.log(this.prop);
}
};
obj.method(); // 'a prop'</code>
</pre>
</section>
</section>
<section id="top-level">
<h3>
Top level scope
</h3>
<p>
In browsers, the top-level scope is the global scope and <strong class="hljs-string">this</strong> refers
to the global object (<strong class="hljs-keyword">window</strong>)
</p>
<pre class="block-code" contenteditable="true">
<code class="line-numbers">
<script>
console.log(this); // window
</script></code>
</pre>
</section>
<section id="mistakes-fixes">
<section id="common-mistakes">
<h3>
Common mistakes
</h3>
<ul>
<li>
Forgetting <strong class="hljs-keyword">new</strong> operator
</li>
<li>
Shadowing the value of <strong class="hljs-string">this</strong>
</li>
<li>
Extracting methods invocation
</li>
</ul>
</section>
<section id="forgetting-new">
<h4>
Forgetting <strong class="hljs-keyword">new</strong> operator
</h4>
<p>
When you call a constructor without the <strong class="hljs-keyword">new</strong> operator you are invoking
it like a real function! <br />
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
function Point(x, y) {
this.x = x;
this.y = y;
}
let point = Point(10, 20);
// Constructor doesn't return the object instance!
console.log(point); // undefined
// Global variables created! Gomblotto!1!1
window.x; // 10
window.y; // 20</code>
</pre>
</section>
<section id="forgetting-new-fix" data-background="http://i.giphy.com/90F8aUepslB84.gif">
<h4>
Fix
</h4>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
'use strict';
function Point(x, y) {
this.x = x;
this.y = y;
}
// TypeError: Cannot set property 'x' of undefined
let point = Point(10, 20);</code>
</pre>
</section>
<section id="shadowing">
<h4>
Shadowing the value of <strong class="hljs-string">this</strong>
</h4>
<p>
Sometimes we forgot about Javascript lexical scope!
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
let component = {
width: 800,
init() {
document.addEventListener('click', function(e) {
console.log(this.width);
});
}
};
// On document's click this.width is undefined
component.init();</code>
</pre>
</section>
<section id="shadowing-fix-one" data-background="http://i.giphy.com/ZFTKZ8zwj38gE.gif">
<h4>
FIX
</h4>
<p>
We can store the value of <strong class="hljs-string">this</strong>
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
let component = {
width: 800,
init() {
// Woooooo
let self = this;
document.addEventListener('click', function(e) {
console.log(self.width);
});
}
};
component.init();</code>
</pre>
</section>
<section id="shadowing-fix-two" data-background="http://i.giphy.com/rIq6ASPIqo2k0.gif">
<h4>
FIX
</h4>
<p>
We can use a sexy <strong class="hljs-keyword">arrow function</strong>*
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
let component = {
width: 800,
init() {
document.addEventListener('click', e => {
console.log(this.width);
});
}
};
component.init();</code>
</pre>
<small>*only for ES6 brave guys!</small>
</section>
<section id="shadowing-fix-three" data-background="http://i.giphy.com/Czak1L6uy06iI.gif">
<h4>
FIX
</h4>
<p>
Or use <strong class="hljs-keyword">Function.prototype.bind</strong>
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
let component = {
width: 800,
init() {
document.addEventListener('click', function(e) {
console.log(this.width);
}.bind(this));
}
};
component.init();</code>
</pre>
</section>
<section id="extracting">
<h4>
Extracting methods invocation
</h4>
<p>
If we retrieve the value of a method, instead of invoking it, we turn that method into a function <br />
pointing <strong class="hljs-string">this</strong> to the global object!
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
let counter = {
count: 0,
increment() {
this.count++;
console.log(this.count);
}
};
setInterval(counter.increment, 1000);</code>
</pre>
</section>
<section id="extracting-fix">
<h4>
Fix
</h4>
<p>
Use <strong class="hljs-keyword">Function.prototype.bind</strong> to create a new function with
<strong class="hljs-string">this</strong> bound
to the right object.
</p>
<pre class="block-code" contenteditable="true">
<code class="javascript line-numbers">
let counter = {
count: 0,
increment() {
this.count++;
console.log(this.count);
}
};
setInterval(counter.increment.bind(counter), 1000);</code>
</pre>
</section>
</section>
<section id="the-end" data-background="http://i.giphy.com/nguAwtOo4nxAY.gif">
<h2>
Thank you!
</h2>
<small>
<a class="hidden-link" href="#exercises">
Exercises
</a>
</small>
</section>
<section id="eval">
<section id="eval-context" data-background="http://i.giphy.com/oKVs1VY0MKfvO.gif">
<div class="background-black">
<h3>
The hidden slide on eval!
</h3>
<h4>
<strong class="hljs-keyword">eval</strong> function can be called:
</h4>
<ul>
<li>
Directly
</li>
<li>
Indirectly
</li>
</ul>
</div>
</section>
<section id="eval-directly">
<p>
When eval is called directly <strong class="hljs-string">this</strong> point to surrounding eval context
</p>
<pre class="block-code" contenteditable="true">
<code class="line-numbers">
let x = 'global';
function directly() {
let x = 'local';
console.log(eval('x')); // local
}
directly();
// Point depending on the context;
function nonStrict() {
console.log(eval('this')); // window
}
nonStrict();
function strict() {
'use strict';
console.log(eval('this')); // undefined
}
strict();</code>
</pre>
</section>
<section id="eval-indirectly">
<p>
When eval is called indirectly <strong class="hljs-string">this</strong> always point to the global object
</p>
<pre class="block-code" contenteditable="true">
<code class="line-numbers">
let x = 'global';
function indirectly() {
let x = 'locals';
console.log(window.eval('x'));
}
indirectly();
// Let's try with this
let obj = {
indirectly: function() {
console.log(window.eval('this'));
}
};
obj.indirectly();</code>
</pre>
</section>
</section>
<section id="exercises" data-background="http://i.giphy.com/cTdsso3i4oGdO.gif">
<div class="background-black">
<h2>Exercises</h2>
<ul>
<li>
<a href="http://codepen.io/anon/pen/bByGXr?editors=0012">
First
</a>
</li>
<li>
<a href="http://codepen.io/anon/pen/NbVWQa?editors=0012">
Second
</a>
</li>
<li>
<a href="http://codepen.io/anon/pen/rWgNpv?editors=0012">
oledata
</a>
</li>
</ul>
</div>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide',
dependencies: [{
src: 'plugins/highlight/highlight.js',
async: true,
callback() {
hljs.initHighlightingOnLoad();
}
}, {
src: 'plugins/line-numbers/line-numbers.js'
}]
});
</script>
</body>
</html>