-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2195 lines (1909 loc) · 73.2 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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ECMAScript 6 => pure awesomness</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<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', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="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>ECMAScript 6 => `${pure}<br>awesomness`</h1>
</section>
<section>
<h2>About me:</h2>
<ul>
<li>Marcin Kumorek</li>
<li>Front-end developer @platformui</li>
<li><a href="http://twitter.com/qmmr" target="_blank">@qmmr</a></li>
<li><a href="http://plus.google.com/+MarcinKumorekProfile" target="_blank">Google+</a></li>
<li><a href="https://github.com/qmmr">github.com/qmmr</a></li>
</ul>
</section>
<section>
<section>
<h1>A brief history of JavaScript</h1>
<ul>
<li class="fragment">created in 10 days in May 1995 by Brendan Eich, an engineer at Netscape</li>
<li class="fragment">first released with Netscape 2 early in 1996</li>
<li class="fragment">originally called Mocha, renamed to LiveScript to reflect its dynamic nature</li>
<li class="fragment">finally renamed to JavaScript after the popular Sun Microsystem's Java language</li>
</ul>
</section>
<section>
<h2><span style="text-decoration: line-through;">Mocha</span> JavaScript flavors</h2>
<ul>
<li class="fragment">Microsoft released a mostly-compatible version of the language called JScript with IE 3 several months later</li>
<li class="fragment">ECMAScript standard in 1997</li>
<li class="fragment">The standard received a significant update as ECMAScript edition 3 in 1999</li>
<li class="fragment">The fourth edition was abandoned.</li>
<li class="fragment">ECMAScript edition 5, published in December of 2009</li>
<li class="fragment">ECMAScript 6 (ES6 Harmony) - work in progress</li>
</ul>
<aside class="notes">
- LiveScript was renamed to JavaScript (because of popularity of Java) edespite the two having very little in common. This has been a source of confusion ever since.
- Netscape submitted the language to European Computer Manufacturers Association which resulted in the first edition of the ECMAScript standard in 1997.
- ECMAScript edition 3 in 1999 and has stayed pretty much stable ever since
- The fourth edition was abandoned, due to political differences concerning language complexity.
- Many parts of the fourth edition formed a basis of the new ECMAScript edition 5, published in December of 2009
</aside>
</section>
</section>
<section>
<section>
<h1>ES6 finally</h1>
</section>
<section>
<h2>Let's dive in!</h2>
</section>
<section>
<img src="img/almost-there.jpg" alt="ES6 - almost there...">
<p>Almost there...</p>
</section>
<section>
<p>Available resources:</p>
<ul>
<li><a href="http://kangax.github.io/es5-compat-table/es6/">kangax.github.io/es5-compat-table/es6</a></li>
<li><a href="http://www.es6fiddle.net/">www.es6fiddle.net</a></li>
<li><a href="https://github.com/nzakas/understandinges6/blob/master/manuscript/01-The-Basics.md">understanding es6</a></li>
<li><a href="https://github.com/lukehoban/es6features">github.com/lukehoban/es6features</a></li>
<li><a href="https://twitter.com/esdiscuss">@esdiscuss</a></li>
</ul>
</section>
</section>
<section>
<section>
<h1>What's coming?</h1>
</section>
<section>
<h2>Syntax refinements:</h2>
<ul>
<li class="fragment"><a href="#/5/1">=> (arrow)</a></li>
<li class="fragment"><a href="#/5/6">object literals</a></li>
</ul>
</section>
<section>
<h2>New syntax:</h2>
<ul>
<li class="fragment"><a href="#/6/1">let</a></li>
<li class="fragment"><a href="#/6/6">const</a></li>
<li class="fragment"><a href="#/6/10">default parameters</a></li>
<li class="fragment"><a href="#/6/13">rest</a></li>
<li class="fragment"><a href="#/6/16">spread</a></li>
<li class="fragment"><a href="#/6/22">destructuring</a></li>
<li class="fragment"><a href="#/6/29">for..of loop</a></li>
<li class="fragment"><a href="#/6/32">iterators</a></li>
<li class="fragment"><a href="#/6/34">generators</a></li>
<li class="fragment"><a href="#/6/41">comprehensions</a></li>
</ul>
</section>
<section>
<h2>Organizational features:</h2>
<ul>
<li class="fragment">classes</li>
<li class="fragment">modules</li>
</ul>
</section>
<section>
<h2>New standard library features:</h2>
<ul>
<li class="fragment">set</li>
<li class="fragment">map</li>
<li class="fragment">weakmap + weakset</li>
<li class="fragment">promises</li>
<li class="fragment">proxies</li>
</ul>
</section>
<section>
<h2>...rest</h2>
<ul>
<li class="fragment">template strings</li>
// template literals
`string text`
// Multiline strings
`string text line 1
string text line 2`
// String interpolation
`string text ${expression} string text`
var greet = function(greet = 'Hello', name = 'stranger') {
return `${ greet } ${ name },
how are you today?`;
};
console.log(greet());
"Hello stranger,
how are you today?"
console.log(greet('Hi', 'Marcin'));
"Hi Marcin,
how are you today?"
---
var lovem = function(strings, ...vars) {
return `${ vars[0] } <3 ${ strings[1] }!`;
};
var user = 'Marcin';
// syntax - tagged template
console.log(lovem `${ user }ECMAScript2015`);
<li class="fragment">unicode</li>
<li class="fragment">module loaders</li>
<li class="fragment">symbols</li>
<li class="fragment">subclassable built-ins</li>
<li class="fragment">math + number + string + object APIs</li>
<li class="fragment">binary and octal literals</li>
<li class="fragment">reflect api</li>
<li class="fragment">tail calls</li>
</ul>
</section>
</section>
<!-- PART ONE -->
<section>
<section>
<h1>Part one</h1>
</section>
<!-- SYNTAX REFINEMENTS -->
<section>
<h1>Syntax refinements</h1>
</section>
<!-- ### => (arrow) ### -->
<section>
<h1>=> (arrow)</h1>
<ul>
<li>shorter syntax for common case use of anonymous functions</li>
<li>retains lexical scope of <strong>this</strong></p></li>
</ul>
</section>
<section>
<h2>shorter syntax for common case use of anonymous functions</h2>
<p>old way</p>
<pre><code data-trim contenteditable>
var reflect = function(value) {
return value;
};
var reflect = value => value;
or
var reflect = (value) => {
return value;
}
---
var sum = function(a, b) {
return a + b;
};
var sum = (a, b) => a + b;
// OR
var sum = (a, b) => {
return a + b;
};
---
var getName = function() {
return 'Marcin';
};
var getName = () => 'Marcin';
---
var square = function(n) {
return n * n;
};
var numbers = [ 0, 1, 2, 3, 4, 5 ];
var doubles = numbers.map( square ); // [ 0, 1, 4, 9, 16, 25 ]
var numbers = [ 0, 1, 2, 3, 4, 5 ];
var doubles = numbers.map( n => n * n ); // [ 0, 1, 4, 9, 16, 25 ]
</code></pre>
</section>
<section>
<p>new way</p>
<pre><code data-trim contenteditable>
let people = [
{
name: 'Joe',
lastName: 'Doe',
occupation: 'Web Developer',
languages: [ 'HTML', 'CSS', 'JavaScript' ]
},
{
name: 'Jane',
lastName: 'Doe',
occupation: 'Back-end Developer',
languages: [ 'PHP', 'Ruby', 'Python', 'JavaScript' ]
}
]
</code></pre>
</section>
<section>
<h2>retains lexical scope of <strong>this</strong></p></h2>
<p>old way</p>
<pre><code data-trim contenteditable>
var person = {
name: 'Joe',
friends: [ 'Jane', 'Johnny', 'Luke' ],
getName: function() {
setTimeout(function() {
console.log(this.name);
}, 1000);
},
showFriends: function() {
return this.friends.map(function(friend) {
return `${ this.name } is friend with ${ friend }`;
});
}
};
var person = {
name: 'Joe',
friends: [ 'Jane', 'Johnny', 'Luke' ],
getName: function() {
var that = this;
setTimeout(function() {
console.log(that.name);
}, 1000);
},
showFriends: function() {
var self = this;
return this.friends.map(function(friend) {
return `${ self.name } is friend with ${ friend }`;
});
}
};
var person = {
name: 'Joe',
friends: [ 'Jane', 'Johnny', 'Luke' ],
getName: function() {
setTimeout(function() {
console.log(this.name);
}.bind(this), 1000);
},
showFriends: function() {
return this.friends.map(function(friend) {
return `${ this.name } is friend with ${ friend }`;
}, this);
}
};
var person = {
name: 'Joe',
friends: [ 'Jane', 'Johnny', 'Luke' ],
getName() {
setTimeout(() => console.log(this.name), 1000);
},
showFriends() {
return this.friends.map(friend => `${ this.name } is friend with ${ friend }`)
}
};
console.log(person.getName())
console.log(person.showFriends())
</code></pre>
</section>
<section>
<h2>new way</h2>
<pre><code data-trim contenteditable>
let person = {
name: 'Joe',
friends: [ 'Jane', 'Johnny', 'Luke' ],
getName() {
setTimeout(() => console.log( 'Hi, my name is ' + this.name ), 1000);
},
showFriends() {
this.friends.forEach( friend => console.log( this.name + ' is friend with ' + friend ) );
}
};
person.getName();
person.showFriends();
</code></pre>
<aside class="notes">
es6fiddle
</aside>
</section>
<!-- ### refined object literals ### -->
<section>
<h1>Refined object literals</h1>
</section>
<section>
<h2>object literals</h2>
<p>old way</p>
<pre><code data-trim contenteditable>
var teacher = 'Michael'
var student = {
name: 'Joe',
teacher: teacher,
greet: function () {
return 'Hi, ' + this.name + '!'
}
}
student[ '_id_' + +new Date() ] = Math.floor( Math.random() * 10000000 )
</code></pre>
</section>
<section>
<h2>object literals</h2>
<p>new way</p>
<pre>
<code data-trim contenteditable>
// ES5
var title = 'ECMAScript 2015';
var students = [ 'Joe', 'Jane', 'Phil' ];
var id = function() { return 42 };
var course = {
title: title,
students: students,
welcome: function() {
return 'Welcome to ' + this.title + ' course!';
}
};
course[ '_prop_' + id() ] = id();
// ES6
var title = 'ECMAScript 2015';
var students = [ 'Joe', 'Jane', 'Phil' ];
var id = function() { return 42 };
var course = {
title,
students,
welcome() {
return `Welcome to ${ this.title } course!`;
},
[ `_prop_${ id() }` ]: id() // ['_prop42']: 42
};
for (let key of Object.keys(course)) {
console.log(`Course.${ key } = ${ course[key] }`)
}
</code>
</pre>
</section>
<!-- ### new syntax ### -->
<section>
<h1>New syntax</h1>
</section>
<!-- ### let keyword ### -->
<section>
<h1>let</h1>
<h2>is the new var</h3>
<p class="fragment">scoped to the block rather then function</p>
<p class="fragment">does not have hoisting characteristics</p>
<aside class="notes">
block being anything inside curly braces
- loops
- if/else/else if
- switch
- try/catch*
</aside>
</section>
<section>
<h2>scoped inside if clause</h2>
<pre><code data-trim contenteditable>
function printName ( language ) {
var name = 'Marcin';
if ( language === 'English' ) {
let name = 'Martin';
console.log( name ); // Martin
}
console.log( name ); // Marcin
}
---
function func() {
let foo = 5;
if (1) {
let foo = 10; // shadows outer `foo`
console.log(foo); // 10
}
console.log(foo); // 5
}
---
if (true) { // enter new scope, TDZ starts
// Uninitialized binding for `tmp` is created
console.log(typeof tmp); // ReferenceError
tmp = 'abc'; // ReferenceError
console.log(tmp); // ReferenceError
let tmp; // TDZ ends, `tmp` is initialized with `undefined`
console.log(tmp); // undefined
tmp = 123;
console.log(tmp); // 123
}
</code></pre>
<aside class="notes">
you cannot redeclare identifier, throws syntax error
</aside>
</section>
<section>
<h2>Local scopes</h2>
<p>common problem</p>
<pre><code data-trim contenteditable>
function wrapper ( arr ) {
var result = [], i, len;
for ( i = 0, len = arr.length; i < len; ++i ) {
result[i] = function () {
return arr[i];
}
}
return result
}
var wrapped = wrapper([ 'Luke', 'Obi-Wan', 'Yoda' ]);
console.log( wrapped[1]() ); // undefined
---
let arr = [];
for (var i=0; i < 3; i++) {
arr.push(function() {
return i;
});
}
arr.map(function(x) {
return x();
}); // [ 3, 3, 3 ]
</code></pre>
<h2 class="look fragment fade-in">ಠ_ಠ</h2>
</section>
<section>
<h2>Old solution</h2>
<pre><code data-trim contenteditable>
function wrapper ( arr ) {
var result = [], i, len;
for ( i = 0, len = arr.length; i < len; ++i ) {
(function ( j ) {
result[i] = function () { return arr[j] }
})( i )
}
return result;
}
var wrapped = wrapper([ 'Luke', 'Obi-Wan', 'Yoda' ]);
console.log( wrapped[2]() ); // 'Yoda'
</code></pre>
<h2 class="look fragment fade-in">ಠಿ_ಠ</h2>
</section>
<section>
<h2>New solution</h2>
<pre><code data-trim contenteditable>
function wrapper ( arr ) {
var result = [];
for ( let i = 0, len = arr.length; i < len; ++i ) {
result[i] = function () { return arr[i] }
}
return result;
}
var wrapped = wrapper([ 'Luke', 'Obi-Wan', 'Yoda' ]);
console.log( wrapped[1]() ); // 'Obi-Wan'
</code></pre>
<h2 class="look fragment fade-in">\(`0´)/</h2>
</section><!-- end ### let keyword ### -->
<!-- ### const keyword ### -->
<section>
<h1>const</h1>
<p class="fragment">similar to let, block scoped</p>
<aside class="notes">
- block being anything inside curly braces
- loops
- if/else/else if
- switch
- try/catch*
</aside>
</section>
<section>
<h2>once declared, cannot be redeclared</h2>
<pre><code data-trim contenteditable>
const FOO = 'hello world';
FOO = 'Goodbye, world!'; // error
</code></pre>
</section>
<section>
<h2>declaration must be followed<br> by assignment</h2>
<pre><code data-trim contenteditable>
const FOO;
console.log( FOO ); // undefined
FOO = 'hello world!'; // error: cannot reasign const
</code></pre>
<aside class="notes">
- otherwise we have undefined value, that we cannot change (sic!)
- IE will give an error, others will ignore it (Firefox)
</aside>
</section>
<section>
<h2>const can hold any type of variables (array, object etc.)</h2>
<pre><code data-trim contenteditable>
const FOO = [];
FOO.push( 'Hello' );
FOO.push( 'world!' );
console.log(FOO.join( ', ' )); // Hello, world!
</code></pre>
<pre><code data-trim contenteditable>
const BAR = {};
BAR.baz = 'Hello, world!';
console.log( BAR.baz ); // hello, world!
--
Object.assign()
var alias = 'Spider-man';
var person = {
firstName: 'Peter',
lastName: 'Parker'
};
var superpowers = [ 'web', 'wall-climbing' ];
var peter = Object.assign({}, person, { alias: alias }, { superpowers: superpowers });
console.log(JSON.stringify(peter));
/*
{
"alias": "Spider-man",
"firstName": "Peter",
"lastName": "Parker",
"superpowers": [ "web", "wall-climbing" ]
}
*/
---
var alias = 'Spider-man';
var person = {
firstName: 'Peter',
lastName: 'Parker'
};
var superpowers = [ 'web', 'wall-climbing' ];
var peter = Object.assign({}, person, { alias, superpowers });
console.log(JSON.stringify(peter));
/*
{
"alias": "Spider-man",
"firstName": "Peter",
"lastName": "Parker",
"superpowers": [ "web", "wall-climbing" ]
}
*/
</code></pre>
</section><!-- end ### const ### -->
<!-- ### default parameters ### -->
<section>
<h1>default parameters</h1>
<h2 class="look fragment">ʘ‿ʘ</h2>
</section>
<section>
<h2>checking for optional params</h2>
<blockquote>old way</blockquote>
<pre><code data-trim contenteditable>
function createCustomElement (tagName, id, parent) {
if (!id) {
id = generateID();
}
if (!parent) {
parent = document.body;
}
var element = document.createElement( tagName );
element.id = id;
parent.appendChild(element);
return element;
}
function makeRequest(url, timeout, callback) {
timeout = timeout || 2000;
callback = callback || function() {};
// the rest of the function
}
function makeRequest(url, timeout = 2000, callback = function() {}) {
// the rest of the function
}
---
function timeout(timeout = 2000, callback = function() {}) {
// the rest of the function
}
---
function getCallback() {
return function() {};
}
function timeout(timeout = 2000, getCallback()) {
// the rest of the function
}
---
// uses default timeout and callback
makeRequest('/foo');
// uses default callback
makeRequest('/foo', 500);
// doesn't use defaults
makeRequest('/foo', 500, function(el) {
doSomething(el);
});
var getCallback = function() {
return function callback() {}
};
function makeRequest(url, timeout = 2000, callback = getCallback()) {
// the rest of the function
}
</code></pre>
</section>
<section>
<h2>assignment inside parenthesis</h2>
<blockquote>new way</blockquote>
<pre><code data-trim contenteditable>
function createCustomElement (tagName = 'div', id = generateID(), parent = document.body) {
let element = document.createElement(tagName);
element.id = id;
parent.appendChild( element );
return element;
}
</code></pre>
</section><!-- end ### default parameters ### -->
<!-- ### rest ### -->
<section>
<h1>...rest</h1>
<p class="fragment">is an array of params that were not declared</p>
<p class="fragment">solves the arguments problem</p>
<h2 class="look fragment">\(`0´)/</h2>
</section>
<section>
<h2>rest params</h2>
<blockquote>old way</blockquote>
<pre><code data-trim contenteditable>
var sum = function(a, b) {
return a + b;
}
sum(1, 2); // 3
var sum = function(a, b) {
return a + b;
}
sum(1, 2, 3); // ???
var sum = function() {
var args = [].slice.call(arguments);
return args.reduce(function(accu, curr) {
return accu + curr;
}, 0);
}
sum(1, 2, 3, 4, 5) // 15
</code></pre>
<aside class="notes">
sum function with 2 required params ( a, b )
any additional params are optional
</aside>
</section>
<section>
<h2>rest params</h2>
<blockquote>new way</blockquote>
<pre><code data-trim contenteditable>
var sum = function(...rest) {
return rest.reduce(function(prev, current) {
return prev + current;
}, 0);
}
console.log(sum(1, 2, 3, 4, 5)); // 15
function sum(...rest) {
return rest.reduce((prev, current) => prev + current, 0);
}
console.log(sum(1, 2, 3, 4, 5)); // 15
</code></pre>
</section><!-- end ### rest ### -->
<!-- ### spread ### -->
<section>
<h1>...spread</h1>
<p class="fragment">expands array of args into individual variables</p>
</section>
<section>
<h2>getting the highest value</h2>
<blockquote>old way</blockquote>
<pre><code data-trim contenteditable>
var numbers = [ 0, 20, 22, 42 ];
Math.max.apply(null, numbers); // 42
function sum(x, y, z) {
return x + y + z;
}
sum(numbers[0], numbers[1], numbers[2]); // 42
</code></pre>
<blockquote>new way</blockquote>
<pre><code data-trim contenteditable>
var numbers = [ 0, 20, 22, 42 ];
Math.max(...numbers); // 42
function sum(x, y, z) {
return x + y + z;
}
sum(...numbers); // 42
</code></pre>
</section>
<section>
<h2>spread array</h2>
<blockquote>old way</blockquote>
<pre><code data-trim contenteditable>
var getCoords = function(x, y, z) {
return {
x: x,
y: y,
z: z
};
}
var points = [ 100, 50, 42 ];
var coords = getCoords(points[0], points[1], points[2]); // { "x": 100, "y": 50, "z": 42 }
</code></pre>
</section>
<section>
<h2>spread array</h2>
<blockquote>new way</blockquote>
<pre><code data-trim contenteditable>
var getCoords = function(x, y, z) {
return {
x: x,
y: y,
z: z
};
}
var points = [ 100, 50, 42 ];
var coords = getCoords(...points); // { "x": 100, "y": 50, "z": 42 }
var getCoords = function(x, y, z) {
return { x, y, z };
}
var points = [ 100, 50, 42 ];
var coords = getCoords(...points); // { "x": 100, "y": 50, "z": 42 }
var getCoords = (x, y, z) => ({ x, y, z });
var points = [ 100, 50, 42 ];
var coords = getCoords(...points); // { "x": 100, "y": 50, "z": 42 }
var getPoints = ({ x, y, z }) => [ x, y, z];
var coords = getCoords(...getPoints({ "x": 100, "y": 50, "z": 42 })); // { "x": 100, "y": 50, "z": 42 }
</code></pre>
<aside class="notes">
- notice spread into new object literal syntax
</aside>
</section>
<section>
<h2>joining arrays</h2>
<blockquote>old way</blockquote>
<pre><code data-trim contenteditable>
var primes = [ 2, 3, 5 ]
var fibonacci = [ 0, 1, 1 ]
fibonacci = fibonacci.concat( primes ) // [0, 1, 1, 2, 3, 5]
</code></pre>
<div class="fragment">
<blockquote>new way</blockquote>
<pre><code data-trim contenteditable>
let fibonacci = [ 0, 1, 1, ...primes ]; // [0, 1, 1, 2, 3, 5]
</code></pre>
</div>
</section>
<section>
<h2>joining arrays - advanced</h2>
<blockquote>old way</blockquote>
<pre><code data-trim contenteditable>
var primes = [ 2, 3, 5 ];
var fibonacci = [ 0, 1, 1, 8, 13, 21 ];
primes.forEach(function ( num ) {
var index = fibonacci.indexOf( 8 );
fibonacci.splice( index, 0, num );
})
console.log( fibonacci ); // [0, 1, 1, 2, 3, 5, 8, 13, 21]
</code></pre>
<div class="fragment">
<blockquote>new way</blockquote>
<pre><code data-trim contenteditable>
let fibonacci = [ 0, 1, 1, ...primes, 8, 13, 21 ];
console.log( fibonacci ); // [0, 1, 1, 2, 3, 5, 8, 13, 21]
</code></pre>
</div>
</section><!-- end ### spread ### -->
<section>
<h2>The rest of new syntax features in part two</h2>
</section>
</section><!-- END OF PART ONE -->
<!-- PART TWO -->
<section>
<section>
<h1>Part two</h1>
<h2>New syntax continues</h2>
</section>
<!-- ### destructuring ### -->
<section>
<h1>destructuring</h1>
<p class="fragment">extract data from objects and arrays</p>
</section>
<section>
<h2>swaping values of variables</h2>
<blockquote>old way</blockquote>
<pre><code data-trim contenteditable>
var a = 1;
var b = 2;
var tmp = a;
a = b;
b = tmp;
</code></pre>
<h2 class="look fragment fade-in">ಠ_ಠ</h2>
</section>
<section>
<h2>swaping values of variables</h2>
<blockquote>new way</blockquote>
<pre><code data-trim contenteditable>
let a = 1;
let b = 2;
let tmp = a;
a = b;
b = tmp;
let [ a, b ] = [ 1, 2 ];
[ a, b ] = [ b, a ];
console.log(a, b); // 2, 1
</pre></code>
<h2 class="look fragment">ʘ‿ʘ</h2>
</section>
<section>
<h2>extract array</h2>
<pre><code data-trim contenteditable>
#008744 (0,135,68) // green
#0057e7 (0,87,231) // blue
#d62d20 (214,45,32) // red
#ffa700 (255,167,0) // orange
#ffffff (255,255,255) // white
let getColors = function() {
return [ '#008744', '#0057e7', '#d62d20' ];
};
let colors = getColors();
let green = colors[0];
let blue = colors[1];
let red = colors[2];
let [ green, blue, red ] = getColors;
console.log(green, blue, red); // "#008744" "#0057e7" "#d62d20"
---