-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjs4ng-beginners.html
500 lines (456 loc) · 17 KB
/
js4ng-beginners.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript for Those Wanting to Get Started with AngularJS</title>
<meta name="description" content="JavaScript for Those Wanting to Get Started with AngularJS">
<meta name="author" content="Marek Matczak">
<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, minimal-ui">
<link rel="stylesheet" href="../base/reveal.js/css/reveal.css">
<link rel="stylesheet" href="../base/reveal.js/css/theme/sky.css" id="theme">
<link rel="stylesheet" href="css/js4ng-beginners.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="../base/reveal.js/lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? '../base/reveal.js/css/print/pdf.css' : '../base/reveal.js/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<!--[if lt IE 9]>
<script src="../base/reveal.js/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">
<section>
<h1 class="no_upper">JavaScript*</h1>
<h4>* for Those Wanting to Get Started with <img class="middle" src="img/AngularJS-large.png" height="54" width="191"></h4>
<p>
<small>Marek Matczak</small>
</p>
</section>
<section>
<h2>Inspired by</h2>
<div>
<img src="img/javascript_the_good_parts.jpg">
</div>
<div>
<small>Picture taken from: <a href="http://frightanic.com/category/software-development/javascript/">http://frightanic.com/category/software-development/javascript/</a></small>
</div>
</section>
<section>
<h2 class="no_upper">JavaScript...</h2>
<p class="fragment">...has nothing common with Java <small>* except for some key words</small>
</p>
<p class="fragment">...is more like <q cite="http://searchservervirtualization.techtarget.com/definition/Our-Favorite-Technology-Quotations">“Lisp in C's clothing”</q> <small>*Douglas Crockford</small>
</p>
<p class="fragment">...has objects, but is class-free</p>
<p class="fragment">...is of single-threaded nature</p>
<p class="fragment">...has asynchronous programming model</p>
<p class="fragment">...is object oriented and functional at the same time</p>
</section>
<section>
<h2>Declaring variables</h2>
<p>Always use <i>var</i> to declare variables</p>
<p>If you don't, they become <b>global</b> variables</p>
<div class="fragment">
<p>Variable declarations get hoisted to the top of a function...</p>
<p>...which may be confusing:</p>
<pre><code data-trim class="javascript">
var myVar = 'global';
var myFunction = function () {
console.log(myVar); // undefined
var myVar = 'local';
console.log(myVar); // 'local'
};
myFunction();
</code></pre>
</div>
<div class="fragment">
<p>The above code behaves as if it looked like this:</p>
<pre><code data-trim class="javascript">
var myVar = 'global';
var myFunction = function () {
var myVar;
console.log(myVar); // undefined
myVar = 'local';
console.log(myVar); // 'local'
};
myFunction();
</code></pre>
</div>
</section>
<section>
<h2>Single var pattern</h2>
<p>Eliminates logical errors caused by hoisting</p>
<pre><code data-trim class="javascript">
var a = 1,
b = 2,
sum = a + b,
myVar,
hello = function () {
return 'Hello!';
};
</code></pre>
</section>
<section>
<h2>Objects</h2>
<p>can be created in a very convenient way with <i>object literals</i>:</p>
<pre><code data-trim class="javascript">
var emptyObject = {},
person = {
firstName : 'John',
lastName : 'Example',
age : 30
};
</code></pre>
</section>
<section>
<h2>Objects</h2>
<p>Values' retrieval</p>
<pre><code data-trim class="javascript">
var emptyObject = {},
person = {
firstName: 'John',
lastName: 'Example',
age: 30
};
// retrieval
person.firstName // 'John'
person['lastName'] // 'Example'
person.salutation // undefined
salutation = person.salutation || ''; // the default operator
person.address.street // throws TypeError
street = person.address && person.address.street // undefined
</code></pre>
</section>
<section>
<h2>Objects</h2>
<p>Values' update and properties' removal</p>
<pre><code data-trim class="javascript">
var person = {
firstName: 'John',
lastName: 'Example',
age: 30
},
salutation, street;
// update
person['firstName'] = 'Joe';
person.age = 33;
// delete
delete person.age;
</code></pre>
</section>
<section>
<h2>Functions</h2>
<p>are objects</p>
<p>can be created with <i>function literals</i>:</p>
<pre><code data-trim class="javascript">
var add = function (a, b) {
return a + b;
};
add(1, 2); // 3
</code></pre>
</section>
<section>
<h2>Functions</h2>
<p>stored as properties of objects...</p>
<pre><code data-trim class="javascript">
var person = {
firstName: 'John',
lastName: 'Example',
age: 30,
letMeIntroduceMyself: function () {
return 'My name is ' + this.firstName + ' ' + this.lastName;
}
};
person.letMeIntroduceMyself(); // My name is John Example
person.firstName // 'John' - no privacy
</code></pre>
<p>...are called <i>methods</i></p>
</section>
<section>
<h2>Scope</h2>
<p>is defined by functions, and no {} blocks</p>
<pre><code data-trim class="javascript">
var myFun = function (someInput) {
var a = 1,
b = 2,
yourFun = function () {
var b = 22,
c = 3;
// at this point a = 1, b = 22, c = 3
a = 11;
};
// at this point a = 1, b = 2, c is not defined
yourFun();
// at this point a = 11, b = 2, c is not defined
};
myFun();
</code></pre>
</section>
<section>
<h2>Closure</h2>
<p>enables a function to keep its creation context</p>
<pre><code data-trim class="javascript">
var myObjectFactory = function (param) {
var value = 'Value';
return {
theseAreMySecrets: function () {
return 'value = ' + value + ', param = ' + param;
}
};
},
myObject = myObjectFactory('Param');
myObject.theseAreMySecrets(); // value = Value, param = Param
myObject.value; // undefined
</code></pre>
<p>...even if its lifetime is shorter than the function's one</p>
</section>
<section>
<section>
<h2>Module Pattern</h2>
<p>makes use of function scope and closure</p>
<pre><code data-trim class="javascript">
var person = (function () {
var details = {
firstName: 'John',
lastName: 'Example',
age: 30
};
return {
letMeIntroduceMyself: function () {
return 'My name is ' +
details.firstName + ' ' + details.lastName;
}
};
})();
person.letMeIntroduceMyself(); // My name is John Example
person.details; // undefined
</code></pre>
<p>is widely used for programming <i>services</i> in <img class="middle" src="img/AngularJS-large.png" height="54" width="191">
</p>
</section>
<section id="exercise-service">
<h2>Exercise</h2>
<ol>
<li>Create a module <strong><code>todos</code></strong> using the module pattern.</li>
<li>The <strong><code>todos</code></strong> module keeps its list of ToDos private.</li>
<li>Each ToDo is made up of a <strong><code>title</code></strong> and a flag <strong><code>done</code></strong>.</li>
<li>The <strong><code>todos</code></strong> module exposes the <strong><code>numberOfDoneToDos</code></strong> method.</li>
</ol>
</section>
<section>
<h2>Solution</h2>
<pre><code data-trim class="javascript">
var todos = (function () {
var todos = [{title: 'Take part in the Rich Web Client School', done: true},
{title: 'Do exercise', done: false}];
return {
numberOfDoneToDos: function () {
var i, noOfDone = 0;
for (i = 0; i < todos.length; i += 1) {
if (todos[i].done) {
noOfDone += 1;
}
}
return noOfDone;
}
};
})();
</code></pre>
</section>
</section>
<section>
<section>
<h2>Constructor function</h2>
<p>is intended to be invoked with the <i>new</i> prefix <small>(forgetting it makes <i>this</i> bound to the global object!)</small>
</p>
<p>creates a new object with a hidden link to its <i>prototype</i>
</p>
<p>is kept in a variable with a capitalized name by convention</p>
<pre><code data-trim class="javascript">
var Person = function () {
this.firstName = 'John';
this.lastName = 'Example';
this.age = 30;
}, johnExample;
Person.prototype.letMeIntroduceMyself = function () {
return 'My name is ' + this.firstName + ' ' + this.lastName;
};
johnExample = new Person();
johnExample.letMeIntroduceMyself(); // My name is John Example
johnExample.firstName; // 'John' - no privacy
</code></pre>
<p>is used for defining <i>controllers</i> in <img class="middle" src="img/AngularJS-large.png" height="54" width="191">
</p>
</section>
<section id="exercise-controller">
<h2>Exercise</h2>
<ol>
<li>Define a constructor function <strong><code>ToDoCtrl</code></strong> passing it the <strong><code>todos</code></strong> module.</li>
<li><strong><code>ToDoCtrl</code></strong> has a <strong><code>userName</code></strong> property and exposes the <strong><code>summary</code></strong> method.</li>
<li>The <strong><code>summary</code></strong> method calls the <strong><code>todos</code></strong> module and prints a summary on the number of ToDos done by the user till now.</li>
</ol>
</section>
<section>
<h2>Solution 1</h2>
<pre><code data-trim class="javascript">
var
todos = {}, // from the previous exercise
ToDoCtrl = function (todos) {
this.userName = 'John';
this.summary = function () {
console.log(this.userName + ', you have done ' +
todos.numberOfDoneToDos() + ' ToDos');
};
},
myToDoCtrl = new ToDoCtrl(todos);
myToDoCtrl.summary();
</code></pre>
</section>
<section>
<h2>Solution 2</h2>
<pre><code data-trim class="javascript">
var
todos = {}, // from the previous exercise
ToDoCtrl = function (todos) {
this.userName = 'John';
this.todos = todos;
},
myToDoCtrl;
ToDoCtrl.prototype.summary = function () {
console.log(this.userName + ', you have done ' +
this.todos.numberOfDoneToDos() + ' ToDos');
};
myToDoCtrl = new ToDoCtrl(todos);
myToDoCtrl.summary();
</code></pre>
</section>
</section>
<section>
<h2>Prototypal inheritance</h2>
<pre><code data-trim class="javascript">
var create = function (from) { // added to ECMAScript 5 as Object.create
var F = function () {};
F.prototype = from;
return new F();
},
parent = {
firstName : 'John',
lastName : 'Example',
letMeIntroduceMyself : function () {
return 'My name is ' + this.firstName + ' ' + this.lastName;
}
}, child;
child = create(parent);
child.firstName = 'Joe';
parent.letMeIntroduceMyself(); // My name is John Example
child.letMeIntroduceMyself(); // My name is Joe Example
</code></pre>
<p>In <img class="middle" src="img/AngularJS-large.png" height="54" width="191"> <i>scopes</i> prototypically inherit from each other</p>
</section>
<section>
<h2>Functional inheritance</h2>
<pre><code data-trim class="javascript">
var parent = function (my) {
var that = {},
details = {
firstName: 'John',
lastName: 'Example',
};
my = my || {}; // keeps secrets shared in the inheritance chain
my.details = details; // makes details shared by descendants
that.letMeIntroduceMyself = function () {
return 'My name is ' + details.firstName + ' ' + details.lastName;
};
return that;
},
child = function (name) {
var my = {},
that = parent(my);
my.details.firstName = name;
return that;
};
johnExample = parent();
joeExample = child('Joe');
johnExample.letMeIntroduceMyself(); // My name is John Example
joeExample.letMeIntroduceMyself(); // My name is Joe Example
joeExample.firstName; // undefined
</code></pre>
</section>
<section>
<h2>Summary</h2>
<div>
<img src="img/dcrockford-js.jpg">
</div>
<div>
<small>http://www.flickr.com/photos/natekoechley/2853942919/</small>
</div>
</section>
</div>
</div>
<script src="../base/reveal.js/lib/js/head.min.js"></script>
<script src="../base/reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
width: 980,
minScale: 1,
maxScale: 1,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{
src: '../base/reveal.js/lib/js/classList.js',
condition: function () {
return !document.body.classList;
}
},
{
src: '../base/reveal.js/plugin/markdown/marked.js',
condition: function () {
return !!document.querySelector('[data-markdown]');
}
},
{
src: '../base/reveal.js/plugin/markdown/markdown.js',
condition: function () {
return !!document.querySelector('[data-markdown]');
}
},
{
src: '../base/reveal.js/plugin/highlight/highlight.js',
async: true,
condition: function () {
return !!document.querySelector('pre code');
},
callback: function () {
hljs.initHighlightingOnLoad();
}
},
{
src: '../base/reveal.js/plugin/zoom-js/zoom.js',
async: true
},
{
src: '../base/reveal.js/plugin/notes/notes.js',
async: true
}
]
});
</script>
</body>
</html>