-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
307 lines (301 loc) · 8.75 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
<!DOCTYPE html>
<html>
<head>
<title>Stuff You Didn't Know JavaScript Could Do</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"></link>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nova+Square">
<link rel="stylesheet" href="slideshow.css"></link>
<link rel="stylesheet" href="prism.css"></link>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="slideshow.js"></script>
<script src="prism.js"></script>
<script src="demos.js"></script>
</head>
<body class="language-js">
<article>
<section class="visible fadeIn">
<h1>Stuff You Didn't Know JavaScript Could Do</h1>
</section>
<section>
<h2><code>debugger</code> Statement</h2>
<pre><code>function debuggerDemo() {
debugger;
}</code></pre>
<p>
<button id="debuggerDemo">Demo</button>
</p>
</section>
<section>
<h2><code>console.table(arr)</code></h2>
<pre><code>function consoleTableDemo() {
console.table([
{ id: '001', name: 'Bulbasaur', types: 'green, poison' },
{ id: '004', name: 'Charmander', types: 'fire' },
{ id: '007', name: 'Squirtle', types: 'water' },
{ id: '070', name: 'Caterpie', types: 'bug' }
]);
}</code></pre>
<p>
<button id="consoleTableDemo">Demo</button>
</p>
</section>
<section>
<h2><code>console.dir(obj)</code></h2>
<pre><code>function consoleDirDemo() {
console.dir({
"name": "Balbo Baggins",
"spouse": "Berylla Boffin",
"children": [
{
"name": "Mungo Baggins",
"spouse": "Laura Grubb",
"children": [ /* ... */ ],
}/*, ... */
]
});
}</code></pre>
<p>
<button id="consoleDirDemo">Demo</button>
</p>
</section>
<section>
<h2><code>console.trace(msg)</code></h2>
<pre><code>function consoleTraceDemo() {
console.trace('Don\'t blink. Blink and you\'re dead.');
}</code></pre>
<p>
<button id="consoleTraceDemo">Demo</button>
</p>
</section>
<section>
<h2>Template Literals</h2>
<pre><code>function templateLiteralDemo() {
emitProverb(true);
emitProverb(false);
}
function emitProverb(vulcan) {
console.log(`The needs of the ${vulcan ? 'many' : 'one'}
outweigh the needs of the ${vulcan ? 'few' : 'many'}.`);
}</code></pre>
<p>
<button id="templateLiteralDemo">Demo</button>
</p>
</section>
<section>
<h2>Spread Syntax: Apply Array to Arguments</h2>
<pre><code>function spreadSyntaxApplyDemo() {
const args = [ 'Two', 'Hands', 'Blue' ];
riverChant.apply(null, args); // old way
riverChant(...args); // new hotness
}
function riverChant(count, parts, color) {
console.log(`${count} by ${count}, ${parts} of ${color}.`);
}</code></pre>
<p>
<button id="spreadSyntaxApplyDemo">Demo</button>
</p>
</section>
<section>
<h2>Spread Syntax: Expand Arrays Inside Array Literals</h2>
<pre><code>function spreadSyntaxExpandDemo() {
const adjectives = [ 'stuck-up', 'half-witted', 'scruffy-looking' ];
insult(adjectives, 'nerf herder');
}
function insult(adjectives, noun) {
const arr = [ ...adjectives, noun ];
const arr2 = [ ...arr ]; // Look, easy array cloning...
const arr3 = [ ...arr, ...arr ]; // ...and concatenation!
console.log(`Why, you ${arr.join(', ')}!`);
}</code></pre>
<p>
<button id="spreadSyntaxExpandDemo">Demo</button>
</p>
</section>
<section>
<h2>Spread Syntax: Merge Objects</h2>
<pre><code>function spreadSyntaxMergeDemo() {
let fellowship = {
frodo: true, sam: true, merry: true,
pippin: true, aragorn: true
};
console.dir(fellowship);
fellowship = { ...fellowship, ...{
gandalf: true, legolas: true, gimli: true,
boromir: true
}};
console.dir(fellowship);
fellowship = { ...fellowship, ...{
gandalf: false, boromir: false
}};
console.dir(fellowship);
const clone = { ...fellowship }; // Works for cloning, too!
}</code></pre>
<p>
<button id="spreadSyntaxMergeDemo">Demo</button>
</p>
</section>
<section>
<h2>Rest Parameters</h2>
<pre><code>function restParametersDemo() {
ringPoem('rule them all', 'find them', 'bring them all');
}
function ringPoem(...purposes) {
purposes.forEach(function(purpose) {
console.log(`One ring to ${purpose},`);
});
console.log('And in the darkness bind them.');
}</code></pre>
<p>
<button id="restParametersDemo">Demo</button>
</p>
</section>
<section>
<h2>Destructuring Assignments</h2>
<pre><code>function destructuringDemo() {
const [ name, ...nanas ] = [
'Batman!',
'Nana',
'nana',
'nana',
'nana'
];
console.log(name);
console.log(nanas);
const { robin, ...others } = {
robin: 'ally',
alfred: 'ally',
joker: 'enemy',
catwoman: 'complicated'
};
console.log(robin);
console.log(others);
}</code></pre>
<p>
<button id="destructuringDemo">Demo</button>
</p>
</section>
<section>
<h2>Object Literal Shorthand</h2>
<pre><code>function objectLiteralShorthandDemo() {
let name = 'Spider-man';
let key1 = 'power', key2 = 'responsibility';
let hero = {
name,
[key1]: 'great',
[key2]: 'great',
web() {
console.log('Thwip!');
}
});
console.dir(hero);
hero.web();
}</code></pre>
<p>
<button id="objectLiteralShorthandDemo">Demo</button>
</p>
</section>
<section>
<h2>Arrow Functions</h2>
<pre><code>function arrowFunctionDemo() {
const planets = [ /* ... */ ];
// arrow function
let zerg = planets.map((planet) => {
return planet.zerg;
}).reduce((totalZerg, planetZerg) => {
return totalZerg + planetZerg;
});
console.log('Total zerg: ' + zerg);
// shorthand (one arg, single return statement)
zerg = planets
.map(planet => planet.zerg)
.reduce((totalZerg, planetZerg) => totalZerg + planetZerg);
console.log('Total zerg: ' + zerg);
}</code></pre>
<p>
<button id="arrowFunctionDemo">Demo</button>
</p>
</section>
<section>
<h2>Default Parameters</h2>
<pre><code>function defaultParameterDemo() {
equipIndy();
equipIndy('Nazi uniform', 'military hat', 'pistol');
}
function equipIndy(
clothes = 'leather jacket',
hat = 'fedora',
weapon = 'whip'
) {
console.log(`Equip Indiana Jones:
Clothing: ${clothes}
Hat: ${hat}
Weapon: ${weapon}`);
}</code></pre>
<p>
<button id="defaultParameterDemo">Demo</button>
</p>
</section>
<section>
<h2>Page Visibility</h2>
<pre><code>function pageVisibilityDemo() {
if (!window.visListener) {
window.visListener = () => {
const elapsed = (Date.now() - window.lastTime) / 1000;
window.lastTime = Date.now();
if (document.hidden) {
console.log(`Nox. Page was visible for ${elapsed} s.`);
} else {
console.log(`Lumos. Page was hidden for ${elapsed} s.`);
}
};
document.addEventListener('visibilitychange', window.visListener);
window.lastTime = Date.now();
console.log('Page visibility demo started');
} else {
document.removeEventListener('visibilitychange', window.visListener);
delete window.visListener;
console.log('Page visibility demo stopped');
}
}</code></pre>
<p>
<button id="pageVisibilityDemo">Demo</button>
</p>
</section>
<section>
<h2>Speech Synthesis</h2>
<pre><code>function synthDemo() {
const synth = window.speechSynthesis;
if (!synth) {
console.err('Speech synthesis API not available');
return;
}
synth.speak(new SpeechSynthesisUtterance(
'Never go in against a Sicilian when death is on the line!'
));
}</code></pre>
<p>
<button id="synthDemo">Demo</button>
</p>
</section>
<section>
<h2>WebRTC (user media w/o plugins)</h2>
<video id="video" width="320" height="240" autoplay></video>
<pre id="videoCode"><code>function videoDemo() {
if (!window.stream) {
const video = $('video')[0];
navigator.mediaDevices.getUserMedia({ video: true }).then(stream => {
window.stream = stream;
video.srcObject = stream;
}).catch(error => {
console.error(error);
});
} else {
window.stream.getTracks()[0].stop();
delete window.stream;
}
}</code></pre>
<button id="videoDemo">Demo</button>
</section>
</article>
</body>
</html>