-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
956 lines (845 loc) · 27.4 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
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>JS, ES6, lodash, ...</title>
<meta name="description" content="Une formation sur le JS">
<meta name="author" content="Pierre Besson">
<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="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="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 ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<link href='https://fonts.googleapis.com/css?family=Merriweather:700|Source+Sans+Pro:300,600,300italic,600italic|Source+Code+Pro:400,600' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-background='#ffe599'><h1>Javascript</h1><a href='http://get-focus.github.io/formation-js/#/'>http://get-focus.github.io/formation-js/#/</a></section>
<section data-background='https://cdn.nextinpact.com/images/bd/news/124984.png'>
<style scoped>
h1#jquery { color: red; font-weight: bold; background-color: yellow;}
</style>
<h1 id='jquery'>JS !== jQuery</h1>
</section>
<section>
<section data-background='https://media.giphy.com/media/vKZKZ0I9nCAGA/giphy.gif'><h1>Les bases</h1></section>
<section data-markdown>
## Les variables
- `const` Variable constante qui ne peut plus être modifiée
- `var` portée globale ou locale a une fonction
- `let` portée du block courant `{}`
> A retenir on utilise `const` au maximum et `let` dans les autres cas
</section>
<section>
<h2>Les strings</h2>
<pre>
<code class='javascript'>
'déjà' < 'demain' // => false
'déjà'.localeCompare('demain') // => -1 ('déjà' avant 'demain')
'déjà !'.toLocaleUpperCase() // => 'DÉJÀ !'
'ÇA POUTRE'.toLocaleLowerCase() // => 'ça poutre'
'one,two,three'.split(',') // => ['one', 'two', 'three']
'one,,two,three'.split(/\W+/) // => ['one', 'two']
'hello'.substring(1) // => 'ello'
'hello'.slice(1, -2) // => 'el' -> [1;length-2[
</code>
</pre>
</section>
<section>
<h2>Les dates</h2>
<pre>
<code class='javascript'>
new Date // maintenant !
new Date(y,m,d[,h,m,s,ms]) // Valeur découpée. (un peu lourd...)
date.getFullYear() // JAMAIS
date.getMonth() // NAZE Java-like qui démarre à… zéro (janvier). #ugly
date.getDay() // PERDUUUU ! C'est le DOW (Day Of Week), 0 = dim., 6 = s
date.getDate() // Le jour du mois. Super logique.
date.getHours()
date.getMinutes()
date.getSeconds()
date.getMilliseconds() // "Milliseconds", un seul mot : une seule initiale.
// Heure locale du navigateur/système. On a les mêmes en getUTCXxx()…
</code>
</pre>
</section>
<section>
<h2>Les dates</h2>
<pre>
<code class='javascript'>
// "J'ai bien lu la doc"
date.getTime() // => ex. 1290852603042
// "#superSmart"
+date
// Et sinon…
new Date(1301733937452)
// Mais aussi (car souci pour partir de composantes UTC) :
Date.UTC(y, m, d, h, mn, s, ms) // => numérique
</code>
</pre>
</section>
<section>
<blockquote>
A retenir : Momentjs (on voit ça plus tard...)
</blockquote>
</section>
<section>
<h2>Les objets</h2>
<pre>
<code class='javascript'>
let obj = { first: 'David', last: 'Lopez', age: 77 };
obj.first // => 'David'
obj['age'] // => 42
obj.first = 'pierr';
obj['first'] // => 'pierr'
obj[77] = 'roberto';
obj[77] // => 'roberto'
obj['77'] // => 'roberto'
obj.77 // => SyntaxError
</code>
</pre>
</section>
<section>
<h2>Les objets avec ES6</h2>
<pre>
<code class='javascript'>
let defaults = { first: 'John', last: 'Doe', age: 42 };
let trainer = { last: 'Smith', age: 35 };
trainer = { ...defaults, ...trainer, age: 36 }
// => { first: 'John', last: 'Smith', age: 36 }
trainers['a'+ 12] = 'Gino';
trainers.a12;
// Gino
</code>
<pre>
</section>
<section>
<h2>Les tableaux</h2>
<pre>
<code class='javascript'>
let names = ['John', 'David', 'Rodrigo'];
names.length
// => 3. R/W : astuce pour réutiliser un tableau!
names[0]
// => 'John'
names[12] = 'Pierre';
names.length
// => 13
names[9]
// => undefined (comme 10 et 11): c'est appelé "sparse array"
</code>
</pre>
</section>
<section>
<pre>
<h2>Les tableaux</h2>
<code class='javascript'>
let data = [1, 2, 3];
// arr1.concat(arg…) -> arr2 [déroule sur 1 niveau, ni "shallow" ni "deep"]
data.concat(4, 5, 6) // => [1, 2, 3, 4, 5, 6]
data.concat([4, 5, 6]) // => [1, 2, 3, 4, 5, 6]
data.concat(4, [5, 6]) // => [1, 2, 3, 4, 5, 6]
data.concat([4, [5, 6]]) // => [1, 2, 3, 4, [5, 6]] -- 2 niveaux !
data // => [1, 2, 3] -- intact !
// arr.join([sep = ',']) -> String
data.join() // => '1,2,3'
data.join('') // => '123' -- Fréquent en construisant du HTML
// arr1.slice(signedBegin[, signedEnd = length]) -> arr2 -- négatif ok partout !
data.slice(1) // => [2, 3]
data.slice(1, 1) // => []
data.slice(1, 2) // => [2]
data.slice(1, -1) // => [2]
data.slice(-2) // => [2, 3]
data.slice(-2, 2) // => [2]
data.slice(-2, -1) // => [2]
</code>
</pre>
</section>
<section>
<pre>
<h2>Les tableaux en ES6</h2>
<code class='javascript'>
let arr1 = ['one', 'two'], arr2 = ['three', 'four'];
arr1.push(...arr2) // => 4
arr1 // => ['one', 'two', 'three', 'four']
//le map
arr1.map(element => console.log(element))
// for…in itère sur LES PROPRIÉTÉS ÉNUMÉRABLES de N’IMPORTE QUEL OBJET
var arr = ['hello', 'world', , 'cool'];
for (var k in arr){ console.log(k)}; // => 0, 1, 3
// for…of itère sur les éléments d'un objet itérable
for (var k of arr){ console.log(k)}; // => hello, world, undefined, cool
</code>
</pre>
</section>
<section>
<pre>
<h2>"Déguiser" un objet en tableau<h2>
<code class='javascript'>
// Toi aussi, déguise-toi en tableau !
var fakeArray = { 0: '!', 1: 'ça torche', 2: 'JavaScript', length: 3 };
fakeArray.join = [].join; fakeArray.reverse = [].reverse;
fakeArray.reverse().join(' ');
// => 'JavaScript ça torche !'
// Ou alors :
fakeArray.__proto__ = Array.prototype;
fakeArray.reverse().join(' ');
// => 'JavaScript ça torche !'
// Méthodes « génériques » utilisables: hn
// concat, join, pop, push, reverse, shift,
// slice, sort, splice, toString, unshift.
</code>
</pre>
</section>
<section>
<h2>Les boucles</h2>
<pre>
<code class='javascript'>
const ARRAY = [1,2,3,4];
for(let i = 0, _l=ARRAY.length; i < _l; i++ ){console.log(ARRAY[i])}
ARRAY.forEach(function(element, idx){console.log(element, idx)});
ARRAY.map(function(element, idx){console.log(element, idx)});
</code>
</pre>
</section>
<section>
<h2>Les fonctions</h2>
<pre>
<code class='javascript'>
function maFonction() {
return 'test';
}
maFonction() // test;
const a = () => 'test';
a(); //test
</code>
</pre>
</section>
<section>
<h2>Quelques pièges de comparaisons en JS ...</h2>
<pre>
<code>
== ou === ?!
42 == '42' // => true --
null == undefined // => true --
null == 0 // => false --
0 == undefined // => false --
0 == false // => true --
1 == true // => true --
42 == true // => false --
'0' == false // => true --
'' == false // => true --
NaN == NaN // => false --
</code>
</pre>
</section>
<section>
<h2>Pour éviter les écueils, utiliser ===</h2>
<pre>
<code>
// avec ===, fini de jouer : vérif valeur ET TYPE !
42 === '42' // => false
null === undefined // => false
null === 0 // => false
0 === undefined // => false
0 === false // => false
'0' === false // => false
NaN === NaN // => false -- rien à faire !
</code>
</pre>
</section>
<section>
<h2>in et delete</h2>
<pre>
<code class='javascript'>
let person = { name: 'Joe', langs: ['fr', 'en'] };
'name' in person // => true
person.age = 35;
'age' in person // => true
person.age = null;
'age' in person // => true
delete person.age
'age' in person // => false
person.age // => undefined
0 in person.langs // => true
'0' in person.langs // => true
person.langs[3] = 'de';
2 in person.langs // => false
3 in person.langs // => true
delete person.langs[3]
person.langs // => ['fr', 'en', undefined, undefined]
</code>
</pre>
</section>
<section>
<h2>Falsy / Truthy</h2>
</section>
<section>
<h2>XML</h2>
<pre>
<code class='xml'>
<menu id="file" value="File">
<popup>
<menuitem value="New" onclick="CreateNewDoc()"></menuitem>
<menuitem value="Open" onclick="OpenDoc()" ></menuitem>
<menuitem value="Close" onclick="CloseDoc()" ></menuitem>
</popup>
</menu>
</code>
</pre>
</section>
<section>
<h2>JSON</h2>
<pre>
<code class='javascript'>
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{ "value": "New", "onclick": "CreateNewDoc()" },
{ "value": "Open", "onclick": "OpenDoc()" },
{ "value": "Close", "onclick": "CloseDoc()" }
]
}
}
}
</code>
</pre>
</section>
<section>
<h2>Oublions jQuery</h2>
<pre>
<code class='javascript'>
//Avant
const monElement = $('[data-my-selector]');
//Après
const monElement = document.querySelector('[data-my-selector]');
</code>
</pre>
</section>
</section>
<section data-state="theme-color-blue-yellow">
<section data-background=''>
<h1>Les outils</h1>
</section>
<section data-background='https://nodejs.org/static/images/logos/nodejs-green.png'>
</section>
<section>
<h2 style='background-color:rgba(1,1,1, 0.5);'>Node.js</h2>
<ul>
<li>JS côté serveur</li>
<li>Apporte plein d'outils sous forme de modules</li>
<li><a href='npmjs.com'>npm</a> (node package module)</li>
</ul>
</section>
<section >
<h2 style='background-color:rgba(1,1,1, 0.5);'>Node.js</h2>
<ul>
<li>package.json</li>
<li>node_modules</li>
<li><code>npm install -g ma-dependance-globale</code></li>
<li><code>npm install -g rimraf</code></li>
<li><code>npm install --save ma-dependance</code></li>
</ul>
</section>
<section data-background='http://www.kolhamevaser.com/wp-content/uploads/2015/05/Schechter.Bavel_.TowerofBavel.jpg'>
<h1>Babel</h1>
</section>
<section data-background='http://jlongster.com/s/upload/webpack-hot1.png'>
<h1 style='background-color:rgba(1,1,1, 0.5);'><a href='https://webpack.github.io/'>Webpack</a></h1>
</section>
<section data-background=''>
<h1>Debug</h1>
</section>
</section>
<section data-state="theme-color-blue-yellow">
<section data-background='https://media.giphy.com/media/RHUGfUYnjNuMw/giphy.gif'>
<h1>Prototypes / Constructor</h1>
</section>
<section>
<h2>Classes ?</h2>
<ul>
<li>Avant ES6 pas de classe</li>
<li>JS est un langage prototypale</li>
<li>En JS tout est un objet</li>
<li>Une propriété est identifiée par une paire nom / valeur</li>
<li>Une propriété peut être une fonction</li>
</ul>
</section>
<section>
<h2>Constructeur</h2>
<ul>
<li>Fonctions servant à initialiser un nouvel objet. Le nom du constructeur est un peu comme « le nom de la classe »…</li>
<li>Toute fonction peut servir de constructeur : il suffit de l’appeler avec l’opérateur new.</li>
<li>Elle dispose alors d’une variable implicite this, qui représente la nouvelle « instance ».</li>
<li>L’objet créé référence son constructeur : constructor.</li>
</ul>
</section>
<section>
<h2>Constructeur</h2>
<pre>
<code class='javascript'>
function Person(first, last) {
this.first = first;
this.last = last;
}
var joeLopezPerson = new Person('Joe', 'Lopez');
var davidLopezz = new Person('David', 'lopez');
joeLopezPerson.first // => 'Joe'
davidLopezz.first // => 'David'
// Si on jouait aux cons ?!
function LopezPerson(first, last) {
this.first = first;
this.last = last;
return { first: 'Anne', last: 'Pas Lopez' };
}
var oops = new LopezPerson('Henry', 'Lopez');
oops.first // => Anne
</code>
</pre>
</section>
<section>
<h2>Prototypes</h2>
<ul>
<li>Tout constructeur a un prototype : un objet qui définit les propriétés (et donc méthodes) partagées par tous les objets que produit ce constructeur.</li>
<li>Le prototype est « vivant » : si on le triture après l’appel au constructeur, ça marche quand même !</li>
<li>Techniquement, y’a plein d’autres trucs dans un prototype (réf. au constructeur, gestion de propriétés…).</li>
<li>On verra, plutôt vous verrez...</li>
</ul>
</section>
<section>
<h2>Prototype</h2>
<pre>
<code class='javascript'>
function Person(first, last) {
this.first = first;
this.last = last;
}
// On augmente l'existant…
Person.prototype.fullName = function fullName() {
return this.first + ' ' + this.last;
}
Person.prototype.greet = function greet() {
alert('Salut je m’appelle ' + this.first);
}
var john = new Person('John', 'Smith');
john.fullName() // => 'John Smith'
john.greet() // 'Salut je m’appelle John'
</code>
</pre>
</section>
<section>
<h2>Prototype (Don't)</h2>
<pre>
<code class='javascript'>
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = function fullName() {
return this.first + ' ' + this.last;
}
this.greet = function greet() {
alert('Salut je m’appelle ' + this.first);
}
}
var john = new Person('John', 'Smith');
john.fullName() // => 'John Smith'
john.greet() // 'Salut je m’appelle John'
</code>
</pre>
<blockquote>Mauvaise pratique: on copie la fonction dans chaque constructeur</blockquote>
</section>
<section>
<h2>Les classes ES6</h2>
<pre>
<code class='javascript'>
class TodoItem extends Component {
constructor(props, context) {
super(props, context);
this.state = {
editing: false
};
}
handleDoubleClick() {
this.setState({ editing: true });
}
…
}
</code>
</pre>
<blockquote class='fragment'>Attention il s'agit de sucre syntaxique pas d'une classe comme en Java ou .NET. Le JS reste un langage prototypale.</blockquote>
</section>
<section>
<h2>La chaîne d'appel d'une méthode</h2>
<code>obj.prop</code> ou <code>obj['prop']</code> (c'est équivalent)
<ol>
<li class='fragment'>On part de l’objet indexé (obj)</li>
<li class='fragment'>Si on trouve prop dans ses own properties, on s’arrête là</li>
<li class='fragment'>Sinon, on passe sur le prototype du niveau supérieur : celui du constructeur de l’objet en cours*</li>
<li class='fragment'>On reprend à l’étape 2, sauf si on était déjà sur Object.prototype, auquel cas le lookup est fini, et échoue (undefined).</li>
</ol>
<footer>*conceptuellement, <code>constructor.prototype</code> ou <code>__proto__</code></footer>
</section>
<section data-background=''>
<h2>La preuve en live...</h2>
<pre>
<code class='javascript'>
function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.fullName = function fullName() {
return this.first + ' ' + this.last;
};
const davidLopez = new Person('David', 'Lopez');
davidLopez.first // => 'David', own property
davidLopez.fullName() // => 'David Lopez', Person.prototype
davidLopez.toString() // => '[object Object]', Object.prototype
Person.prototype.toString = function personToString() {
return '#Person ' + this.fullName();
};
davidLopez.toString() // => "#Person David Lopez"
</code>
</pre>
</section>
</section>
<section>
<section data-background=''><h1>Closure</h1></section>
<section >
<h2>Closure simple</h2>
<pre>
<code class='javascript'>
function publicFx() {
let dateAppel = Date.now();
return function() { console.log(dateAppel); };
}
let privilegedFx1 = publicFx();
// Attendre un bref instant
let privilegedFx2 = publicFx();
// privilegedFx(1,2) sont en fait les fonctions internes construites au
// sein de publicFx, qui grâce aux règles de portée "voient"
// dateAppel. Elles sont *closed over* par publicFx, ce qui fait
// que les valeurs de dateAppel au moment où les fonctions ont été
// renvoyéees sont préservées
privilegedFx1(); // => affiche la dateAppel du moment de la création de la fonction privilegedFx1!
privilegedFx2(); // => affiche la dateAppel d'après !
</code>
</pre>
</section>
<section>
<h2>Les modules sont une utilisation des closures</h2>
<pre><code class="javascript">
function yourModule(require, module, exports) {
let widgets = {};
let util = require('util');
let Widget = require('widgets/base');
function CoolWidget(elt) { … }
util.inherits(CoolWidget, Widget);
// …
module.exports = Widget;
}
</code></pre>
<blockquote>Dans node on ne voit pas la fonction enrobante...</blockquote>
</section>
<section data-background='http://995642590.r.lightningbase-cdn.com/wp-content/uploads/2013/05/HR-Double-Bind-article-by-John-Sumser-on-HR-Examiner-425x282px.jpg'>
<h2 style='background-color:rgba(1,1,1, 0.5);'>Le binding</h2>
</section>
<section data-background='http://995642590.r.lightningbase-cdn.com/wp-content/uploads/2013/05/HR-Double-Bind-article-by-John-Sumser-on-HR-Examiner-425x282px.jpg'>
<h2>Le problème</h2>
<pre>
<code class='javascript'>
var name = 'Mr X';
let obj = {
name: 'Joe Lopez',
greet: function greet(whom) {
console.log(this)
console.log(this.name + ' salue ' + whom);
},
greetAll: function greetAll(first, second, last) {
console.log(this)
[first, second, last].forEach(this.greet);
}
};
obj.greet("les lopezs de France");
// => 'Joe Lopez salut les lopezs de France !'
let fx = obj.greet;
fx("l’atelier") // => '"Mr X salue l’atelier"'
obj.greetAll('David', 'Joe', 'Guénolé'); // => 'Mr X salue David, Mr X salue Joe, Mr X salue undefined'
</code>
</pre>
</section>
<section data-background='http://995642590.r.lightningbase-cdn.com/wp-content/uploads/2013/05/HR-Double-Bind-article-by-John-Sumser-on-HR-Examiner-425x282px.jpg'>
<h2>Comment faire ? La closure?</h2>
<pre>
<code class='javascript'>
const obj = {
// …
greetAll: function greetAll(first, second, last) {
var that = this;
[first, second, last].forEach(function(name) {
that.greet(name);
});
}
}
</code>
</pre>
</section>
<section data-background=''>
<h2>Comment faire ? ES6 style?</h2>
<pre>
<code class='javascript'>
const obj = {
// …
greetAll(first, second, last) {
[first, second, last].forEach(name => this.greet(name));
//Ultra fat
}
}
</code>
</pre>
</section>
<section>
<h2>call et apply</h2>
<pre>
<code class='javascript'>
//let fx = obj.greet;
fx.call(obj, 'les singes') // Joe Lopez salue les singes
let xy = { 0: 'Zero', 1: 'One', length: 2 };
Array.prototype.join.call(xy, '-') // 'Zero-One'
fx.apply(obj, ['']) // => 'Joe salue l’atelier'
Array.prototype.push.apply(xy, ['Two', 'Three', 'Four']) // => 5
xy // => { 0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', length: 5 }
</code>
</pre>
</section>
</section>
<section >
<section data-background='http://104.236.204.173/wp-content/uploads/2015/05/async.jpg'>
<h1 style='background-color:rgba(1,1,1, 0.5);'>JS et asynchronisme</h1>
</section>
<section>
<h2 style='background-color:rgba(1,1,1, 0.5);'>Callback</h2>
<pre>
<code>
function delayedAlert() {
window.setTimeout(slowAlert, 2000);
}
function slowAlert() {
alert("That was really slow!");
}
</code></pre>
</section>
<section>
<h2>Les promesses</h2>
<p>
Une promesse a un état (pending, fullfilled, rejected).<br>
Elle est asynchrone, et se termine soit par un succès soit par une erreur et renvoie une nouvelle promesse.
</p>
<code class='javascript'>promise.then(successCb,errorCb).then(otherSuccess, otherCb).catch(errorHandlingFn)</code>
</section>
<section>
<h2>Les promesses</h2>
<pre><code class='javascript'>
const myPromise = new Promise((resolve, reject) => {
ajaxCall({success: resolve, error: reject});
})
Promise.resolve([1,2,3,4]);
Promise.reject('ma super erreur')
</code></pre>
</section>
<section>
<H2 style='background-color:rgba(1,1,1, 0.5);'>Les promesses</H2>
<pre class='javascript'><code>
fetch('/users.json')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
}) </code></pre>
</section>
</section>
<section >
<section data-background=''>
<h1>ES6 / 2015</h1>
</section>
<section>
<h2>Les modules de node</h2>
<pre>
<code class='javascript'>
//Dans un fichier
module.exports = monObjetAExporter;
//Utilisation
require('./mon_module_locale');
//Utilisation d'un module npm
require('mon_module');
</code>
</pre>
</section>
<section>
<h2>Les modules de Papa</h2>
<pre>
<code class='javascript'>
import * as types from '../constants/ActionTypes';
export function addTodo(text) {
return { type: types.ADD_TODO, text };
}
import React, { PropTypes, Component } from 'react';
import classnames from 'classnames';
import { SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE } from '../constants/TodoFilters';
import 'http://material.js'
…
export default Footer;
export const ADD_TODO = 'ADD_TODO';
export const DELETE_TODO = 'DELETE_TODO';
export const EDIT_TODO = 'EDIT_TODO';
</code>
</pre>
</section>
<section>
<h2>Destruct</h2>
<pre>
<code class='javascript'>
const { activeCount } = this.props;
…
const { filter: selectedFilter, onShow } = this.props;
const [, filters] = output.props.children;
…
const [,, clear] = output.props.children;
var { op: a, lhs: { op: b }, rhs: c } = getASTNode();
Détails
</code>
</pre>
</section>
<section>
<h2>Les strings interpolation et multi lignes</h2>
<pre>
<code class='javascript'>
const person = { first: 'Thomas', last: 'Anderson', age: 25, nickname: 'Neo' };
// Interpolation de JS quelconque
console.log(`${person.first} aka ${person.nickname}`)
// => 'Thomas aka Neo'
// Multi-ligne !
const markup = `<li>
${person.first} ${person.last}, age ${person.age}
</li>`;
</code>
</pre>
</section>
<section>
<h2>Valeur par défaut dans une fonction</h2>
<pre>
<code class='javascript'>
function add(source, numToAdd = 1){
return source + numToAdd;
}
</code>
</pre>
</section>
<section>
<h2>Les objets</h2>
<pre>
<code class='javascript'>
function editTodo(id, text) {
return { type: types.EDIT_TODO, id, text };
//On créé un objet avec pour clef le nom de la variable.
}
const FILTERS = {
[maVar1]: 'All',
[maVar2]: 'Active',
[maVar3]: 'Completed'
};
</code>
</pre>
</section>
</section>
<section >
<section data-background=''>
<h1 style='background-color:rgba(1,1,1, 0.5);'>LODASH / UNDERSCORE</h1>
</section>
<section>
<h2 style='background-color:rgba(1,1,1, 0.5);'>on ne réinvente pas la roue</h2>
</section>
<section>
<h2 style='background-color:rgba(1,1,1, 0.5);'>Les trucs utiles</h2>
<ul>
<li>Est ce que ma variable est un nombre / function / tableau <code>lodash/lang</code></li>
<li>Filtrer, trier, reduce.... <code>lodash/{collection/object/array}</code></li>
<li>Plein de fonctions prêtes à l'emploi <code>lodash/function</code></li>
</ul>
</section>
</section>
<section >
<section data-background='http://cdn.londonandpartners.com/visit/london-organisations/big-ben/63602-640x360-bigben_tilt_640.jpg'>
<h1 style='background-color:rgba(1,1,1, 0.5);'><a href='http://momentjs.com'>MOMENT</a></h1>
</section>
<section>
<h2 style='background-color:rgba(1,1,1, 0.5);'>on ne réinvente pas la roue</h2>
<iframe webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" sandbox="allow-forms allow-scripts allow-popups allow-same-origin allow-pointer-lock" data-src="http://momentjs.com/" src="http://momentjs.com/" height='50%' width='80%'></iframe>
</section>
<section>
<h2 style='background-color:rgba(1,1,1, 0.5);'>Les trucs utiles</h2>
<ul>
<li>formatter des dates <code>moment().format('L')</code></li>
<li>Manipuler des dates <code>moment(maDate).add('days', 1)</code></li>
<li>Timezone...</li>
</ul>
<blockquote>Si tu manipules des dates tu utilises momentjs</blockquote>
</section>
</section>
<section >
<section>
<h1 style='background-color:rgba(1,1,1, 0.5);'><a href='https://github.com/get-focus/formation-js/tree/master/exercices'>Exercices</a></h1>
</section>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="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,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
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, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
<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-70256271-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>