-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs-forward.html
1726 lines (1411 loc) · 63.2 KB
/
js-forward.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>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>前传:源起、战争史歌七部曲</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/wx.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<style media="screen">
ul,small{
width: 100%;
}
</style>
<!-- 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>
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- 章节 -->
<section>
<h1>纪元</h1>
<p>
<ul>
<li>前 言 <span class="fragment highlight-red">星球崛起</span> Browser</li>
<li>第一纪 <span class="fragment highlight-green">上古时期</span> Module?</li>
<li>第二纪<span class="fragment highlight-green">石器时代</span> Script Loader</li>
<li>第三纪 <span class="fragment highlight-green">蒸汽朋克</span> Module Loader</li>
<li>第四纪<span class="fragment highlight-red">号角吹响</span> CommonJS</li>
<li>第五纪 <span class="fragment highlight-blue">双塔奇兵</span> AMD/CMD</li>
<li>第六纪 <span class="fragment highlight-blue">精灵宝钻</span> Browserify/Webpack</li>
<li>第七纪 <span class="fragment highlight-blue">王者归来</span> ES6 Module</li>
</ul>
</p>
</section>
<!-- 前言 星球崛起 -->
<section data-markdown>
<script type="text/template">
### 前言 星球崛起
# *盘古开天地*
从大神说起
<small>在很久很久以前......</small><!-- .element: class="fragment"-->
</script>
</section>
<!-- Tim Berners-Lee -->
<section data-markdown>
<script type="text/template">
<img src="img/lee.jpg" style="
width: 300px;
border-radius: 0;
"/>
## Tim Berners-Lee<!-- .element: class="fragment"-->
</script>
</section>
<!-- 开元年 -->
<section data-markdown>
<script type="text/template">
### 开元年表
- 1989年:初步构想
- 1990年:Nexus(WorldWideWeb)诞生
- 1991年:史上第一个网站( http://info.cern.ch )上线
- 1992年:放弃销售浏览器软件,继续坚守技术指引者角色
- 1994年:万维网联盟(w3c)创建
- 2009年:万维网基金会(w3f)创建
</script>
</section>
<section data-markdown>
<script type="text/template">
### 浏览器
<img src="img/browser-history.jpg" style="
width: 460px;
border-radius: 0;
"/>
<small>从 userAgent 看浏览器的发展史</small><!-- .element: class="fragment"-->
</script>
</section>
<section data-markdown>
<script type="text/template">
### 唐尧虞舜
# *Mosaic*
<small style="color:#1b91ff">马赛克</small><!-- .element: class="fragment"-->
- Nexus不支持显示图片<!-- .element: class="fragment"-->
<small style="color:#1b91ff">无图无真相,导火索点燃</small><!-- .element: class="fragment"-->
- NCSA_Mosaic/2.0 (Windows 3.1)<!-- .element: class="fragment"-->
<small style="color:#1b91ff">为了识别Nexus、Mosic(注:NCSA是马赛克所在的组织名)</small><!-- .element: class="fragment"-->
</script>
</section>
<section data-markdown>
<script type="text/template">
### 楚汉争霸
# *Mozilla*
<small style="color:#1b91ff">Netscape Navigator(网景)</small><!-- .element: class="fragment"-->
- 哥斯拉(Godzilla)谐音<!-- .element: class="fragment"-->
<small style="color:#1b91ff">诚然是一头野心勃勃的怪兽</small><!-- .element: class="fragment"-->
- ”Mosaic Killa”之意<!-- .element: class="fragment"-->
<small style="color:#1b91ff">Killa是俚语中Killer的拼法,即“马赛克的终结者”,赤裸裸的挑战</small><!-- .element: class="fragment"-->
- userAgent为Mozilla/1.0 (Win3.1)<!-- .element: class="fragment"-->
<small style="color:#1b91ff">以自己命名</small><!-- .element: class="fragment"-->
</script>
</section>
<section data-markdown>
<script type="text/template">
### 宋元之战
# *IE*
<small style="color:#1b91ff">Internet Explorer</small><!-- .element: class="fragment"-->
- Mozilla/1.22 (compatible; MSIE 2.0; Windows 95)<!-- .element: class="fragment"-->
<small style="color:#1b91ff">userAgent之加法</small><!-- .element: class="fragment"-->
</script>
</section>
<section data-markdown>
<script type="text/template">
### 康乾盛世
# *Firefox*
<small style="color:#1b91ff">苦逼的命名经过:Phoenix(凤凰)、Firebird(火鸟),全TM被告了</small><!-- .element: class="fragment"-->
- Mozilla/5.0 (Windows; U; Windows NT 5.1; sv-SE; rv:1.7.5) Gecko/20041108 Firefox/1.0<!-- .element: class="fragment"-->
<small style="color:#1b91ff">Gecko(壁虎)是一个浏览器排版引擎,NetScape的大神们围绕Geoko成立了非正式组织Mozilla</small><!-- .element: class="fragment"-->
</script>
</section>
<section data-markdown>
<script type="text/template">
### 师夷长技
# *Konqueror*
<small style="color:#1b91ff">Conqueror的变体,意为征服者(前有航海家网景、探索者IE)</small><!-- .element: class="fragment"-->
- KDE推出,主要用在linux上
- Mozilla/5.0 (compatible; Konqueror/3.2; FreeBSD) (KHTML, like Gecko)<!-- .element: class="fragment"-->
<small style="color:#1b91ff">KHTML跟Gecko一样,都是排版引擎,但用户只买Gecko的账,于是萌萌哒的like出来了</small><!-- .element: class="fragment"-->
</script>
</section>
<section>
<section data-markdown>
<script type="text/template">
### 世界大战
<img src="img/world-war.jpg" alt="" style="width:800px;">
</script>
</section>
<section data-markdown>
<script type="text/template">
Opera:
- Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.51
- Mozilla/5.0 (Windows NT 6.0; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.51
- Opera/9.51 (Windows NT 5.1; U; en)
<small style="color:#1ab11d">狂暴模式,我有炫酷的易容术</small>
</script>
</section>
<section data-markdown>
<script type="text/template">
Safari:
- Mozilla/5.0 (Macintosh; U; PPC Mac OS X; de-de) AppleWebKit/85.7 (KHTML, like Gecko)
<small style="color:#1ab11d">Webkit的排版引擎叫WebCore,是KHTML衍生而来的,所以(KHTML, like Gecko)也继承过来了</small>
</script>
</section>
<section data-markdown>
<script type="text/template">
Chrome:
- Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13
<small style="color:#1ab11d">包含Safari,你懂的</small>
</script>
</section>
<section data-markdown>
<script type="text/template">
## 战争格局
Chrome->Safari->WebKit->KHTML->Gecko->Mozilla
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
### 国内军阀
# *大乱斗*
- 360,百度,QQ,UC,搜狗,猎豹,遨游,世界之窗,淘宝,酷狗,hao123...<!-- .element: class="fragment"-->
- 完美兼容、急速浏览、智能双核,读书人的微创新<!-- .element: class="fragment"-->
- 3Q大战<!-- .element: class="fragment"-->
</script>
</section>
<section data-markdown>
<script type="text/template">
- QQ
<small>
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.7.26717.400)
</small>
- 猎豹
<small>
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER
</small>
</script>
</section>
</section>
<section data-markdown>
<script type="text/template">
### 未来
- userAgent标准:https://www.w3.org/TR/UAAG/
</script>
</section>
<section data-markdown>
<script type="text/template">
# 星球崛起终
</script>
</section>
<!-- 第一纪 上古时期 -->
<section data-markdown>
<script type="text/template">
### 第一纪 上古时期
# *Module?*
从设计模式说起
</script>
</section>
<section data-markdown>
<script type="text/template">
最早,我们这么写代码
```javascript
function foo(){
//...
}
function bar(){
//...
}
```
Global 被污染,很容易命名冲突
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
简单封装:*Namespace* 模式
```javascript
var MYAPP = {
foo: function(){},
bar: function(){}
}
MYAPP.foo();
```
- 减少 Global 上的变量数目
<!-- .element: class="fragment" -->
- 本质是对象,一点都不安全
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
匿名闭包 :*IIFE* 模式
```javascript
var Module = (function(){
var _private = "safe now";
var foo = function(){
console.log(_private)
}
return {
foo: foo
}
})()
Module.foo();
Module._private; // undefined
```
函数是 JavaScript 唯一的 Local Scope
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
再增强一点 :引入依赖
```javascript
var Module = (function($){
var _$body = $("body"); // we can use jQuery now!
var foo = function(){
console.log(_$body); // 特权方法
}
// Revelation Pattern
return {
foo: foo
}
})(jQuery)
Module.foo();
```
<p class="fragment">
这就是<em><b>模块模式</b></em><br>
也是现代模块实现的基石
</p>
</script>
</section>
<!-- 第二纪 石器时代 -->
<section data-markdown>
<script type="text/template">
### 第二纪 石器时代
# *Script Loader*
只有封装性可不够,我们还需要加载
</script>
</section>
<section data-markdown>
<script type="text/template">
## Let's back to script tags
```
body
script(src="jquery.js")
script(src="app.js") // do some $ things...
```
Order is essential
<!-- .element: class="fragment" -->
Load in parallel
<!-- .element: class="fragment" -->
DOM 顺序即执行顺序
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
但现实是这样的…
```
body
script(src="zepto.js")
script(src="jhash.js")
script(src="fastClick.js")
script(src="iScroll.js")
script(src="underscore.js")
script(src="handlebar.js")
script(src="datacenter.js")
script(src="deferred.js")
script(src="util/wxbridge.js")
script(src="util/login.js")
script(src="util/base.js")
script(src="util/city.js")
script(src="util/date.js")
script(src="util/cookie.js")
script(src="app.js")
```
</script>
</section>
<section data-markdown data-background="attach/bm_1.jpg"
style="background:rgba(0,0,0,0.8)"
>
<script type="text/template">
- 难以维护 *Very difficult to maintain!*
- 依赖模糊 *Unclear Dependencies*
- 请求过多 *Too much HTTP calls*
</script>
</section>
<section data-markdown>
<script type="text/template">
## [LABjs](http://labjs.com/) - script loader
(2009)
Loading And Blocking JavaScript
> *Using LABjs will replace all that ugly <br >"script tag soup"*
</script>
</section>
<section data-markdown>
<script type="text/template">
How Does It Works?
```
script(src="LAB.js" async)
```
```javascript
$LAB.script("framework.js").wait()
.script("plugin.framework.js")
.script("myplugin.framework.js").wait()
.script("init.js");
```
<blockquote class="fragment"><p>Executed in parallel? </p></blockquote>
*First-come, First-served <br> (when execution order is not important)*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
#### Sugar
```javascript
$LAB
.script( [ "script1.js", "script2.js", "script3.js"] )
.wait(function(){ // wait for all scripts to execute first
script1Func();
script2Func();
script3Func();
});
```
*Dependency Management<br>基于文件的依赖管理*
<!-- .element: class="fragment" -->
</script>
</section>
<!-- 第三纪 蒸汽朋克 -->
<section data-markdown>
<script type="text/template">
### 第三纪 蒸汽朋克
# *Module Loader*
模块化架构的工业革命
</script>
</section>
<section data-markdown>
<script type="text/template">
## [YUI3](yuilibrary.com) Loader - Module Loader
(2009)
<img src="attach/yui-logo.png" class="logo" style="max-width:150px" />
> *YUI's lightweight core and <br>**modular architecture**<br> make it scalable, fast, and robust.*
</script>
</section>
<section data-markdown>
<script type="text/template">
回顾昔日王者的风采:
```javascript
// YUI - 编写模块
YUI.add('dom', function(Y) {
Y.DOM = { ... }
})
// YUI - 使用模块
YUI().use('dom', function(Y) {
Y.DOM.doSomeThing();
// use some methods DOM attach to Y
})
```
</script>
</section>
<section data-markdown>
<script type="text/template">
Creating Custom Modules
```javascript
// hello.js
YUI.add('hello', function(Y){
Y.sayHello = function(msg){
Y.DOM.set(el, 'innerHTML', 'Hello!');
}
},'3.0.0',{
requires:['dom']
})
```
```
// main.js
YUI().use('hello', function(Y){
Y.sayHello("hey yui loader");
})
```
*基于模块的依赖管理*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
Let's Go A Little Deeper
```javascript
// Sandbox Implementation
function Sandbox() {
// ...
// initialize the required modules
for (i = 0; i < modules.length; i += 1) {
Sandbox.modules[modules[i]](this);
}
// ...
}
```
Y 其实是一个强沙箱
<!-- .element: class="fragment" -->
所有依赖模块通过 attach 的方式被注入沙盒
<!-- .element: class="fragment" -->
*attach:在当前 YUI 实例上执行模块的初始化代码,使得模块在当前实例上可用*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
Still "Script Tag Soup"?
```javascript
script(src="/path/to/yui-min.js") // YUI seed
script(src="/path/to/my/module1.js") // add('module1')
script(src="/path/to/my/module2.js") // add('module2')
script(src="/path/to/my/module3.js") // add('module3')
```
```
YUI().use('module1', 'module2', 'module3', function(Y) {
// you can use all this module now
});
```
<em>
<ul class="fragment">
<li>you don't have to include script tags in a set order</li>
<li>separation of loading from execution</li>
</ul>
</em>
</script>
</section>
<section data-markdown>
<script type="text/template">
漏了一个问题? *Too much HTTP calls*
<!-- .element: class="fragment" -->
## YUI Combo
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
How Combo Works
```
script(src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js")
script(src="http://yui.yahooapis.com/3.0.0/build/dom/dom-min.js")
```
↓ <small style="margin:14px"> magic combo </small>
```
script(src="http://yui.yahooapis.com/combo?
3.0.0/build/yui/yui-min.js&
3.0.0/build/dom/dom-min.js")
```
<!-- .element: class="fragment" -->
*Serves multiple files in a single request*
<!-- .element: class="fragment" -->
GET 请求,需要服务器支持
<!-- .element: class="fragment" -->
[alibaba/nginx-http-concat](https://github.com/alibaba/nginx-http-concat)
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template" >
<img src="attach/joke/3.jpg" height="300px" width="300px" />
- 合并 *Concat*
- 压缩 *Minify*
- 混淆 *Uglify*
</script>
</section>
<!-- 第四话 号角吹响 -->
<section data-markdown>
<script type="text/template">
### 第四纪 号角吹响
# *CommonJS*
征服世界的第一步是跳出浏览器
</script>
</section>
<section data-markdown>
<script type="text/template">
## [CommonJS](http://www.commonjs.org/) - API Standard
(2009.08)
<img src="attach/commonjs-logo.png" class="logo" style="max-width:200px" />
> *javascript:<br> not just for browsers any more!*
</script>
</section>
<section data-markdown>
<script type="text/template">
# [Modules/1.0](http://wiki.commonjs.org/wiki/Modules/1.0)
</script>
</section>
<section data-markdown>
<script type="text/template">
模块的定义与引用
```javascript
// math.js
exports.add = function(a, b){
return a + b;
}
```
```javascript
// main.js
var math = require('math') // ./math in node
console.log(math.add(1, 2)); // 3
```
*Magic Free Variable*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
[NodeJS](http://nodejs.org) : Simple HTTP Server
```javascript
// server.js
var http = require("http"),
PORT = 8000;
http.createServer(function(req, res){
res.end("Hello World");
}).listen(PORT);
console.log("listenning to " + PORT);
```
```
$ node server.js
```
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
#### Synchronously
```javascript
// timeout.js
var EXE_TIME = 2;
(function(second){
var start = +new Date();
while(start + second*1000 > new Date()){}
})(EXE_TIME)
console.log("2000ms executed")
```
```
// main.js
require('./timeout'); // sync load
console.log('done!');
```
*同步/阻塞式加载*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
同步加载对服务器/本地环境并不是问题
<br>
硬盘 I/O | | 网速 I/O
-----|------- | ------|-------
HDD: | *100 MB/s* | ADSL: | *4 Mb/s*
SSD: | *600 MB/s* | 4G: | *100 Mb/s*
SATA-III:| *6000 Mb/s* | Fiber: | *100 Mb/s*
<br>
### *浏览器环境才是问题!*
<!-- .element: class="fragment" -->
</script>
</section>
<!-- 第五话 双塔奇兵 -->
<section data-markdown>
<script type="text/template">
### 第五纪 双塔奇兵
# *AMD/CMD*
浏览器环境模块化方案
</script>
</section>
<section data-markdown>
<script type="text/template">
分歧和冲突
## *[Modules/Async](http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition)*
## *[Modules/Wrappings](http://wiki.commonjs.org/wiki/Modules/Wrappings)*
<div class="fragment">
<h3>*Modules/Transport*</h3>
<h4>*<del>Modules/2.0</del>*</h4>
</div>
<small style="margin-top:15px">
[《前端模块化开发的那点历史 - 玉伯》](https://github.com/seajs/seajs/issues/588)
</small>
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
## *[AMD(Async Module Definition)](http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition)*
RequireJS 对模块定义的规范化产出
<br>
## *[CMD(Common Module Definition)](https://github.com/cmdjs/specification/blob/master/draft/module.md)*
SeaJS 对模块定义的规范化产出
</script>
</section>
<section data-markdown>
<script type="text/template">
## [RequireJS](http://requirejs.org/) - AMD Implementation
(2011)
<img src="attach/requirejs-logo.svg"
class="logo"
style="max-width:130px; margin:0px" />
> *JavaScript file and module loader.<br> It is optimized for in-browser use*
</script>
</section>
<section data-markdown>
<script type="text/template">
If `require()` is async?
```
//CommonJS Syntax
var Employee = require("types/Employee");
function Programmer (){
//do something
}
Programmer.prototype = new Employee();
//如果 require call 是异步的,那么肯定 error
//因为在执行这句前 Employee 模块根本来不及加载进来
```
*this code will not work*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
### *Function Wrapping*
```
//AMD Wrapper
define(
["types/Employee"], //依赖
function(Employee){ //这个回调会在所有依赖都被加载后才执行
function Programmer(){
//do something
};
Programmer.prototype = new Employee();
return Programmer; //return Constructor
}
)
```
looks familiar?
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
*Sugar - simplified CommonJS wrapping*
```javascript
define(function (require) {
var dependency1 = require('dependency1'),
dependency2 = require('dependency2');
return function () {};
});
```
```javascript
// parse out require...
define(
['require', 'dependency1', 'dependency2'],
function (require) {
var dependency1 = require('dependency1'),
dependency2 = require('dependency2');
return function () {};
});
```
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
*AMD vs CommonJS*
### 书写风格
```javascript
// Module/1.0
var a = require("./a"); // 依赖就近
a.doSomething();
var b = require("./b")
b.doSomething();
```
```javascript
// AMD recommended style
define(["a", "b"], function(a, b){ // 依赖前置
a.doSomething();
b.doSomething();
})
```
*Both OK*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
*AMD vs CommonJS*
### 执行时机
```
// Module/1.0
var a = require("./a"); // 执行到此时,a.js 同步下载并执行
```
```
// AMD with CommonJS sugar
define(["require"], function(require){
// 在这里, a.js 已经下载并且执行好了
var a = require("./a")
})
```
*Early Download, Early Executing*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
## [SeaJS](http://seajs.org/docs/) - CMD Implementation
(2011)
<img src="attach/seajs-logo.png" class="logo" />
> *Extremely simple experience of modular development*
</script>
</section>
<section data-markdown>
<script type="text/template">
*More like CommonJS Style*
```javascript
define(function(require, exports) {
var a = require('./a');
a.doSomething();
exports.foo = 'bar';
exports.doSomething = function() {};
});
```
```javascript
// RequireJS 兼容风格
define('hello', ['jquery'], function(require, exports, module) {
return {
foo: 'bar',
doSomething: function() {}
};
});
```
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
*AMD vs CMD - the truly different*
### Still Execution Time
```
// AMD recommended
define(['a', 'b'], function(a, b){
a.doSomething(); // 依赖前置,提前执行
b.doSomething();
})
```
```
// CMD recommanded
define(function(require, exports, module){
var a = require("a");
a.doSomething();
var b = require("b");
b.doSomething(); // 依赖就近,延迟执行
})
```
*Early Download, Lazy Executing*
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">