-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1716 lines (814 loc) · 96.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
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 class="theme-next mist use-motion">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"/>
<link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />
<meta name="description" content="全栈工程师 前端、Java、NodeJS">
<meta property="og:type" content="website">
<meta property="og:title" content="好奇boy">
<meta property="og:url" content="http://www.liyanshan.com/index.html">
<meta property="og:site_name" content="好奇boy">
<meta property="og:description" content="全栈工程师 前端、Java、NodeJS">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="好奇boy">
<meta name="twitter:description" content="全栈工程师 前端、Java、NodeJS">
<script type="text/javascript" id="hexo.configuration">
var NexT = window.NexT || {};
var CONFIG = {
scheme: 'Mist',
sidebar: {"position":"left","display":"post"},
fancybox: true,
motion: true,
duoshuo: {
userId: 0,
author: '博主'
}
};
</script>
<title> 好奇boy </title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?1f5c68947c5379d690f7294cc426aa93";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<div class="container one-collumn sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">好奇boy</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">技术随笔</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-home fa-fw"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-about">
<a href="http://www.babyforlove.com" rel="section">
<i class="menu-item-icon fa fa-user fa-fw"></i> <br />
关于
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-tags fa-fw"></i> <br />
标签
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-archive fa-fw"></i> <br />
目录
</a>
</li>
<li class="menu-item menu-item-github">
<a href="https://github.com/pod4g" rel="section">
<i class="menu-item-icon fa fa-github fa-fw"></i> <br />
pod4g
</a>
</li>
<li class="menu-item menu-item-github2">
<a href="https://github.com/MyNameIsJimGreen" rel="section">
<i class="menu-item-icon fa fa-github fa-fw"></i> <br />
jimGreen
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/07/10/打造你的前端信息流/" itemprop="url">
打造你的前端信息流
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-07-10T14:32:46+00:00" content="2016-07-10">
2016-07-10
</time>
</span>
<span class="post-comments-count">
|
<a href="/2016/07/10/打造你的前端信息流/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/07/10/打造你的前端信息流/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<ol>
<li><p><a href="https://github.com/trending?l=javascript" target="_blank" rel="external">github每日star数量排行榜</a></p>
<p>可以看到现在最火的前端开源项目。</p>
</li>
<li><p><a href="https://www.reddit.com/r/javascript" target="_blank" rel="external">reddit社区的javascript频道</a></p>
<p>每天的javascript资讯。都是最新最火的。</p>
</li>
<li><p><a href="https://hashnode.com/" target="_blank" rel="external">https://hashnode.com</a></p>
<p>资讯订阅。很多大牛在上面哦!每天看看,就知道外国前端在思考那些问题。</p>
</li>
<li><p><a href="https://medium.com/" target="_blank" rel="external">https://medium.com</a></p>
<p>资讯订阅。vue redux等多个开源项目的作者都会在这里发表文章。</p>
</li>
<li><p><a href="http://gold.xitu.io/" target="_blank" rel="external">掘金</a></p>
<p>资讯订阅。国内比较好的技术社区。</p>
</li>
<li><p><a href="http://www.zcfy.cc/" target="_blank" rel="external">众成翻译</a></p>
<p>翻译国外最火的技术与技术分享</p>
</li>
<li><p><a href="http://www.chinaw3c.org/" target="_blank" rel="external">http://www.chinaw3c.org/</a></p>
<p>W3C中文资讯</p>
</li>
<li><p><a href="https://www.quora.com/" target="_blank" rel="external">quora</a></p>
<p>国外问答文章。类似于知乎。在上面提问题,有可能大牛会回答哦!</p>
</li>
<li><p><a href="http://html5weekly.com/" target="_blank" rel="external">http://html5weekly.com/</a></p>
<p>可用邮箱订阅资讯</p>
</li>
</ol>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/06/26/ES规范转型之双等和全等/" itemprop="url">
ES规范转型之双等和全等
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-06-26T15:00:19+00:00" content="2016-06-26">
2016-06-26
</time>
</span>
<span class="post-comments-count">
|
<a href="/2016/06/26/ES规范转型之双等和全等/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/06/26/ES规范转型之双等和全等/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="关于双等"><a href="#关于双等" class="headerlink" title="关于双等"></a>关于双等</h1><p> 在比较双等时,javascript使用的是<code>抽象相等比较算法</code></p>
<p> 算法描述:</p>
<blockquote>
<p>比较运算x==y,其中x和y是值,产生true或者false。这样的比较按如下方式进行:</p>
<pre><code> 1. 若Type(x)与Type(y)相等,则
a. 若Type(x)为Undefined,则返回true。
b. 若Type(x)为Null,则返回true。
c. 若Type(x)为Number,则
i. 若x为NaN,返回false。
ii. 若y为NaN,返回false。
iii. 若x与y为相等数值,则返回true。
iv. 若x为+0,y为-0,返回true。
v. 若x为-0,y为+0,返回true。
vi. 返回false。
d. 若Type(x)为String,则当x和y为完全相等的字符序列(长度相等且相同字符出现在同样位置)时返回true。否则,返回false。
e. 若Type(x)为Boolean,则当x和y同为true或者同为false返回true。否则,返回false。
f. 若Type(x)为Object,则当x和y引用同一个对象时返回true。否则返回false。
2. 若x为null且y为undefined,返回true。
3. 若x为undefined且y为null,返回true。
4. 若Type(x)为Number且Type(y)为String,返回comparison x == ToNumber(y)的结果。
5. 若Type(x)为String且Type(y)为Number,
6. 返回toNumber(x) == y的结果。
7. 若Type(x)为Boolean,返回比较ToNumber(x) == y的结果。
8. 若Type(y)为Boolean,返回比较 x == ToNumber(y)的结果。
8. 若Type(x)为Object且Type(y)为String或Number,返回比较ToPrimitive(x) == y的结果。
10. 返回false。
</code></pre></blockquote>
<p>从规范的定义可知<code>抽象相等比较算法</code>有时候会递归调用。</p>
<p>例如true == “abc”:<br> true == “abc”,根据<code>7</code>,把true转为1,即<br> 1 == “abc”,然后再根据<code>4</code>,比较 1 == ToNumber(“abc”),即<br> 1 == NaN,然后根据<code>1.c.ii</code>,最终返回false</p>
<p>例如true == “1”:<br> true == “1”,根据<code>7</code>,把true转为1,即<br> 1 == “1”,然后在根据<code>4</code>,比较 1 == ToNumber(“1”),即<br> 1 == 1,然后根据<code>1.c.iii</code>,最终返回true</p>
<h1 id="关于全等"><a href="#关于全等" class="headerlink" title="关于全等"></a>关于全等</h1><p> 在比较全等时,javascript使用的是<code>严格相等比较算法</code></p>
<p> 算法描述:</p>
<blockquote>
<p>比较x===y,x和y为值,需要产出true或false。比较过程如下:</p>
<ol>
<li>若Type(x)与Type(y)的结果不一致,返回false,否则</li>
<li>如果Type(x)为Undefined,返回true</li>
<li>如果Type(x)为Null,返回true</li>
<li>如果Type(x)为Number,则<br>a. 如果x为NaN,返回false<br>b. 如果y为NaN,返回false<br>c. 如果x和y为同一个数字,返回true<br>d. 如果x为+0,y为-0,返回true<br>e. 如果x为-0,y为+0,返回true<br>f. 返回false<ol>
<li>如果Type(x)为String,如果x与y为完全相同的字符序列(相同长度和相同的字符对应相同的位置),返回true,否则,返回false</li>
<li>如果Type(x)为Boolean,如果x与y都为true或false,则返回true,否则,返回false</li>
<li>如果x和y引用同一个Obejct对象,返回true,否则,返回false</li>
</ol>
</li>
</ol>
</blockquote>
<p> 从算法描述上可知,全等是双等的一种情况,对应的是双等的<code>1</code></p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/06/09/对DOM的修改永远都是异步的/" itemprop="url">
对DOM的修改永远都是异步的
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-06-09T11:14:26+00:00" content="2016-06-09">
2016-06-09
</time>
</span>
<span class="post-comments-count">
|
<a href="/2016/06/09/对DOM的修改永远都是异步的/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/06/09/对DOM的修改永远都是异步的/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="对DOM的修改永远都是异步的"><a href="#对DOM的修改永远都是异步的" class="headerlink" title="对DOM的修改永远都是异步的"></a>对DOM的修改永远都是异步的</h1><h1 id="这篇文章是错误的观点!"><a href="#这篇文章是错误的观点!" class="headerlink" title="这篇文章是错误的观点!"></a>这篇文章是错误的观点!</h1><p>例如下面的一个场景:</p>
<p>在页面上有2个script标签,第一个script标签内动态创建了一个script,去加载一个config.js文件,<br>第二个script标签内,需要用到动态加载的config</p>
<blockquote>
<p>config.js</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.config = {</span><br><span class="line"> app:<span class="string">"调调"</span>,</span><br><span class="line"> version:<span class="string">"1.1.2"</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<blockquote>
<p>index.html</p>
</blockquote>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">// 第一个script</span><br><span class="line"><script></span><br><span class="line"> (function(){</span><br><span class="line"> var script = document.createElement('script');</span><br><span class="line"> script.src = "config.js";</span><br><span class="line"> document.head.appendChild(script);</span><br><span class="line"> })()</span><br><span class="line"></script></span><br><span class="line"></span><br><span class="line">// 第二个script</span><br><span class="line"><script></span><br><span class="line"> (function(){</span><br><span class="line"> var config = window.config;</span><br><span class="line"> console.log(config); // undefined</span><br><span class="line"> })()</span><br><span class="line"></script></span><br></pre></td></tr></table></figure>
<p>出现上面问题的原因在于:</p>
<p><strong>对dom的修改永远都是异步的</strong></p>
<p>也就是说要等到所有已经加载的脚本执行完毕,再去修改DOM,然后加载并执行新的脚本</p>
<p>要根治这个问题,可以才用成熟的模块加载器如<code>RequireJS</code>,<code>SeaJS</code></p>
<h3 id="写了一段代码进一步验证-对dom的修改永远都是异步的"><a href="#写了一段代码进一步验证-对dom的修改永远都是异步的" class="headerlink" title="写了一段代码进一步验证 对dom的修改永远都是异步的"></a>写了一段代码进一步验证 对dom的修改永远都是异步的</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE html></span></span><br><span class="line"><span class="tag"><<span class="name">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">title</span>></span>dom异步<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"d"</span>></span>默认文本<span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"><<span class="name">script</span>></span><span class="javascript"></span><br><span class="line">d.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">this</span>.innerHTML = <span class="string">"往div里添加的新文本"</span>;<span class="comment">//更改一个节点</span></span><br><span class="line"> <span class="built_in">document</span>.body.appendChild(<span class="built_in">document</span>.createElement(<span class="string">"input"</span>));<span class="comment">//插入一个节点</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>;i<<span class="number">10000000000</span>;i++);</span><br><span class="line">}</span><br><span class="line"></span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure>
<p>原先的理解是:js修改dom是同步的,执行很快,但是渲染线程跟js引擎线程是互斥的,由于下面还有js代码要执行<br>所以渲染线程得不到机会来渲染新的修改后的dom。</p>
<p>原先理解的问题:没有考虑到有些dom是不需要渲染的,只存在于dom树中,例如head内的标签。所以如果修改了一个不需要渲染的标签,其表现也是异步的,那么就可以得出结论<strong>对dom的修改永远都是异步的</strong></p>
<p>现在的理解:js修改dom是异步的,下面的代码执行完毕,然后再来更新dom树,这些都完毕之后,渲染线程才得到机会来渲染新的修改后的dom</p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/06/04/Promise用法全总结/" itemprop="url">
Promise用法总结
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-06-04T14:42:52+00:00" content="2016-06-04">
2016-06-04
</time>
</span>
<span class="post-comments-count">
|
<a href="/2016/06/04/Promise用法全总结/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/06/04/Promise用法全总结/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="一、断链执行"><a href="#一、断链执行" class="headerlink" title="一、断链执行"></a>一、断链执行</h1><p>比如下面的场景:</p>
<p>程序大部分的调用链是这样的:<code>promise.then(A).then(B).then(C).then(D)</code><br>但是B满足某种条件,需要这样调用:<code>promise.then(A).then(B).then(C1).then(C2).then(D)</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">promise.then(A).then(B).then(<span class="function"><span class="keyword">function</span> (<span class="params">state</span>) </span>{</span><br><span class="line"> <span class="keyword">if</span> (state) {<span class="comment">//state可以来自B或A。但对于本次场景state来源与B</span></span><br><span class="line"> <span class="keyword">return</span> C();</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> C1().then(C2);</span><br><span class="line"> }</span><br><span class="line">}).then(D);</span><br></pre></td></tr></table></figure>
<h1 id="二、直接使用"><a href="#二、直接使用" class="headerlink" title="二、直接使用"></a>二、直接使用</h1><h3 id="1-使用new"><a href="#1-使用new" class="headerlink" title="1.使用new"></a>1.使用new</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span><br><span class="line"> 这段代码中的resolve(999),会让这个promise对象立即进入resolved状态,并将</span><br><span class="line"> 999传递给后面then里所指定的resolve函数</span><br><span class="line"> 参数名不一定非要用resolve,也可以使用其他合法标识符,例如success</span><br><span class="line">*/</span></span><br><span class="line"><span class="keyword">var</span> promise = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params"><span class="comment">/* success */</span> resolve</span>)</span>{</span><br><span class="line"> <span class="comment">// success(999);</span></span><br><span class="line"> resolve(<span class="number">999</span>);</span><br><span class="line">})</span><br><span class="line">promise.then( value => <span class="built_in">console</span>.log(value));</span><br></pre></td></tr></table></figure>
<h3 id="2-简写方式"><a href="#2-简写方式" class="headerlink" title="2.简写方式"></a>2.简写方式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这段代码跟使用new方式是一样的。是上面的简写方式</span></span><br><span class="line"><span class="built_in">Promise</span>.resolve(<span class="number">999</span>).then(value => <span class="built_in">console</span>.log(value));</span><br></pre></td></tr></table></figure>
<p>直接使用这种方式,主要用于,后面可能有一系列能被(或者需要被)链式调用的操作,他们也需要一个Promise作为起始点<br>比如说,有两个有依赖的异步操作,<code>getUserById</code>和<code>getPostDetailByPostId</code>,此时<code>userId</code>已经有值<code>999</code>,<br>所以获取<code>userId</code>就不用异步,那后两个可以被链式调用的操作就不能和第一步链起来写了,很不爽。</p>
<p>所以可以使用<em>直接使用</em>的方式来写:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.resolve(<span class="number">999</span>)</span><br><span class="line"> .then(userId => getUserById(userId))</span><br><span class="line"> .then(user => getPostDetailByPostId(user.postId))</span><br><span class="line"> .then(post => <span class="built_in">console</span>.log(post))</span><br></pre></td></tr></table></figure>
<p>使用这种写法的好处:</p>
<ul>
<li>表达因果关系</li>
<li>开发API时对外接口的一致性。例如大多数API都是异步的,现在突然冒出一个数据其实可以同步获取,但对外仍然提供一个统一的异步Promise返回更恰当</li>
</ul>
<p>当然,下面的写法也是可行的:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">getUserById(userId)</span><br><span class="line"> .then(user => getPostDetailByPostId(user.postId))</span><br><span class="line"> .then(post => <span class="built_in">console</span>.log(post));</span><br></pre></td></tr></table></figure>
<h1 id="三、最常用的使用方法"><a href="#三、最常用的使用方法" class="headerlink" title="三、最常用的使用方法"></a>三、最常用的使用方法</h1><p>用Promise包裹一个异步过程,并把这个Promise对象返回出去,以便在外部可以用then链式调用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> getData = <span class="function"><span class="keyword">function</span>(<span class="params">data</span>)</span>{</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>{</span><br><span class="line"> <span class="keyword">try</span>{</span><br><span class="line"> <span class="comment">// .. </span></span><br><span class="line"> <span class="keyword">if</span>(data.state !== <span class="string">"SUCCESS"</span>){</span><br><span class="line"> reject(data);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> resolve(data);</span><br><span class="line"> }</span><br><span class="line"> }<span class="keyword">catch</span>(e){</span><br><span class="line"> reject(e);</span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 第一种写法(推荐)</span></span><br><span class="line">getData()</span><br><span class="line"> .then(data => <span class="built_in">console</span>.log(data))</span><br><span class="line"> .catch(e => <span class="built_in">console</span>.log(e));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 第二种写法</span></span><br><span class="line">getData().then(data => <span class="built_in">console</span>.log(data),e => <span class="built_in">console</span>.log(e));</span><br></pre></td></tr></table></figure>
<h1 id="四、多个Promise对象执行(循环中使用Promise)"><a href="#四、多个Promise对象执行(循环中使用Promise)" class="headerlink" title="四、多个Promise对象执行(循环中使用Promise)"></a>四、多个Promise对象执行(循环中使用Promise)</h1><h3 id="1-并行执行"><a href="#1-并行执行" class="headerlink" title="1.并行执行"></a>1.并行执行</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> userIds = [<span class="number">111</span>,<span class="number">222</span>,<span class="number">333</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> promises = userIds.map( userId => getUserById(userId) );</span><br><span class="line"></span><br><span class="line"><span class="built_in">Promise</span></span><br><span class="line"> .all(promises)</span><br><span class="line"> .then(users => <span class="built_in">console</span>.log(users)); <span class="comment">// users就是根据ID列表得到的user列表</span></span><br></pre></td></tr></table></figure>
<h3 id="2-串行执行"><a href="#2-串行执行" class="headerlink" title="2.串行执行"></a>2.串行执行</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> functions = [...];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> promise = functions.raduce(<span class="function"><span class="keyword">function</span>(<span class="params">now,fn</span>)</span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> now.then(fn);</span><br><span class="line"></span><br><span class="line"> },<span class="built_in">Promise</span>.resolve());</span><br><span class="line"></span><br><span class="line">promise.then(result => <span class="built_in">console</span>.log(result));</span><br></pre></td></tr></table></figure>
<h1 id="五、then链"><a href="#五、then链" class="headerlink" title="五、then链"></a>五、then链</h1><p><code>then(fn())</code> 与 <code>then(fn)</code> 的区别在于<br><code>then(fn())</code> 立即执行<code>fn</code>,<br><code>then(fn)</code> 异步执行<code>fn</code>,此时 <code>then(fn)</code>就是then链中不可或缺的一环了。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 开启第一个异步任务,拿到所有SID</span></span><br><span class="line">getData({url:<span class="string">"v1/getalllist"</span>})</span><br><span class="line"> .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> <span class="comment">// 处理第一个异步任务的结果</span></span><br><span class="line"> <span class="comment">// 开启第二个异步任务</span></span><br><span class="line"> <span class="keyword">return</span> getData({</span><br><span class="line"> url:<span class="string">"v1/getfullsku"</span>,</span><br><span class="line"> data:<span class="built_in">JSON</span>.stringify({</span><br><span class="line"> sids:<span class="built_in">Object</span>.keys(result) <span class="comment">//根据拿到的sid,再次拿全量SKU数据</span></span><br><span class="line"> })</span><br><span class="line"> });</span><br><span class="line"> })</span><br><span class="line"> .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> <span class="comment">// 处理第二个异步任务的结果</span></span><br><span class="line"> <span class="comment">// do something ...</span></span><br><span class="line"> })</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getData</span>(<span class="params">options</span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>{</span><br><span class="line"> $.ajax({</span><br><span class="line"> url:options.url,</span><br><span class="line"> data:options.data,</span><br><span class="line"> success:<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{resolve(result);},</span><br><span class="line"> error:<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>{reject(e);}});</span><br><span class="line"> })</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>下面往then链上添加环节:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">getData({url:<span class="string">"v1/getalllist"</span>})</span><br><span class="line"> .then(<span class="built_in">console</span>.log(<span class="string">"getalllist done !!"</span>))</span><br><span class="line"> .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> <span class="comment">// 处理第一个异步任务的结果</span></span><br><span class="line"> <span class="comment">// 开启第二个异步任务</span></span><br><span class="line"> <span class="keyword">return</span> getData({</span><br><span class="line"> url:<span class="string">"v1/getfullsku"</span>,</span><br><span class="line"> data:<span class="built_in">JSON</span>.stringify({</span><br><span class="line"> sids:<span class="built_in">Object</span>.keys(result) <span class="comment">//根据拿到的sid,再次拿全量SKU数据</span></span><br><span class="line"> })</span><br><span class="line"> });</span><br><span class="line"> })</span><br><span class="line"> .then(<span class="built_in">console</span>.log(<span class="string">"getfullsku done !!"</span>))</span><br><span class="line"> .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> <span class="comment">// 处理第二个异步任务的结果</span></span><br><span class="line"> <span class="comment">// do something ...</span></span><br><span class="line"> })</span><br></pre></td></tr></table></figure>
<blockquote>
<p>结果是:马上打印了<code>getalllist done !!</code>和<code>getfullsku done !!</code>,并不会在接口调用完毕之后才执行相应的打印语句</p>
</blockquote>
<p>再次往then链上添加环节:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">timer</span>(<span class="params"></span>)</span>{</span><br><span class="line"> setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">"timer function execute"</span>);</span><br><span class="line"> },<span class="number">2000</span>)</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">timer2</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve,reject</span>)</span>{</span><br><span class="line"> setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">"timer2 function execute"</span>);</span><br><span class="line"> resolve(result);</span><br><span class="line"> },<span class="number">2000</span>)</span><br><span class="line"> })</span><br><span class="line">}</span><br><span class="line">getData({url:<span class="string">"v1/getalllist"</span>})</span><br><span class="line"> .then(timer())</span><br><span class="line"> .then(timer) <span class="comment">// 报错了,此时timer为then链上的不可或缺的一环,但是在timer中并没有返回任何数据</span></span><br><span class="line"> .then(timer2) <span class="comment">// 不会报错,timer2返回一个promise对象,并且传递了第一个任务返回的数据给下方的then</span></span><br><span class="line"> .then(<span class="built_in">console</span>.log(<span class="string">"getalllist done !!"</span>))</span><br><span class="line"> .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> <span class="comment">// 处理第一个异步任务的结果</span></span><br><span class="line"> <span class="comment">// 开启第二个异步任务</span></span><br><span class="line"> <span class="keyword">return</span> getData({</span><br><span class="line"> url:<span class="string">"v1/getfullsku"</span>,</span><br><span class="line"> data:<span class="built_in">JSON</span>.stringify({</span><br><span class="line"> sids:<span class="built_in">Object</span>.keys(result) <span class="comment">//根据拿到的sid,再次拿全量SKU数据</span></span><br><span class="line"> })</span><br><span class="line"> });</span><br><span class="line"> })</span><br><span class="line"> .then(<span class="built_in">console</span>.log(<span class="string">"getfullsku done !!"</span>))</span><br><span class="line"> .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> <span class="comment">// 处理第二个异步任务的结果</span></span><br><span class="line"> <span class="comment">// do something ...</span></span><br><span class="line"> })</span><br></pre></td></tr></table></figure>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/05/30/nodejs中的模块/" itemprop="url">
nodejs中的模块
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-05-29T16:23:28+00:00" content="2016-05-29">
2016-05-29
</time>
</span>
<span class="post-comments-count">
|
<a href="/2016/05/30/nodejs中的模块/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/05/30/nodejs中的模块/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="模块的分类"><a href="#模块的分类" class="headerlink" title="模块的分类"></a>模块的分类</h1><ol>
<li>核心模块(http/fs/path…)</li>
<li>文件(本地)模块(自定义的js文件)</li>
<li>第三方模块(使用npm安装的第三方模块bluebird/cheerio/q…)</li>
</ol>
<p>在node里面文件和模块是一一对应的关系。</p>
<p>根据模块的类型有几种引入模块的方式,在nodejs里面可以通过文件路径来引用模块,也可以通过模块名来引用,如果使用模块名来引用非核心模块的话,node会把模块名映射到对应的模块文件的路径上。那些包含了核心函数的核心模块会在nodejs启动是被预先加载。</p>
<h1 id="node中关于模块的操作分为4个步骤"><a href="#node中关于模块的操作分为4个步骤" class="headerlink" title="node中关于模块的操作分为4个步骤"></a>node中关于模块的操作分为4个步骤</h1><ol>
<li>定义模块</li>
<li>导出模块</li>
<li>加载(引入)模块</li>
<li>使用模块</li>
</ol>
<h3 id="定义模块"><a href="#定义模块" class="headerlink" title="定义模块"></a>定义模块</h3><p>一个模块就是一个文件,例如<code>Person.class.js</code>,<code>Student.class.js</code></p>
<h3 id="导出模块"><a href="#导出模块" class="headerlink" title="导出模块"></a>导出模块</h3><p>导出模块的2种方式</p>
<p>第一种方式:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Person.class.js</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name.age</span>)</span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> <span class="keyword">this</span>.age = age;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Person.prototype.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(\<span class="string">`My name is <span class="subst">${this.name}</span>\`);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Person.prototype.sayAge = function(){</span><br><span class="line"> console.log(\`My age is <span class="subst">${this.age}</span>\`);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">module.exports = Person;</span></span><br></pre></td></tr></table></figure>
<p>第二种方式:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Student.class.js</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Student</span>(<span class="params">name,age,num</span>)</span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> <span class="keyword">this</span>.age = age;</span><br><span class="line"> <span class="keyword">this</span>.num = num; <span class="comment">// 学号</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Student.prototype.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(\<span class="string">`My name is <span class="subst">${this.name}</span>\`);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Student.prototype.sayAge = function(){</span><br><span class="line"> console.log(\`My age is <span class="subst">${this.age}</span>\`);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Student.prototype.sayNum = function(){</span><br><span class="line"> console.log(\`My student number is <span class="subst">${this.num}</span>\`);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">exports.Student = Student;</span></span><br></pre></td></tr></table></figure>
<h3 id="使用模块"><a href="#使用模块" class="headerlink" title="使用模块"></a>使用模块</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> Person = <span class="built_in">require</span>(<span class="string">"./Person.class"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> stuModule = <span class="built_in">require</span>(<span class="string">"./Student.class"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> Person(<span class="string">"李彦峰"</span>,<span class="number">26</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> stu = <span class="keyword">new</span> stuModule.Student(<span class="string">"李彦峰"</span>,<span class="number">26</span>,<span class="number">455322185</span>);</span><br><span class="line"></span><br><span class="line">p.sayName(); <span class="comment">// My name is 李彦峰</span></span><br><span class="line"></span><br><span class="line">stu.sayNum(); <span class="comment">// My student number is 455322185</span></span><br></pre></td></tr></table></figure>
<p>两种导出模块的方式的区别是:<code>module.exports = Student</code>,在引入模块时,定义的接收变量就相当于<code>Student</code>;<br><code>exports.Student = Student</code>,在引入模块时,定义的接收变量相当于<code>exports</code>对象,要想使用,就必须用<code>接收变量.Student</code>才可以。</p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/05/29/linux中的压缩与解压缩/" itemprop="url">
linux中的压缩与解压缩
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-05-28T23:07:33+00:00" content="2016-05-28">
2016-05-28
</time>
</span>
<span class="post-comments-count">
|
<a href="/2016/05/29/linux中的压缩与解压缩/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/05/29/linux中的压缩与解压缩/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>linux中常用的压缩格式</p>
<ul>
<li>.zip</li>
<li>.gz</li>
<li>.bz2</li>
<li>.tar.gz</li>
<li>.tar.bz2</li>
</ul>
<p>下列所有的压缩命令和打包命令,如果压缩文件和打包文件前面有路径,就压缩或打包到指定的路径下<br>如果路径不存在就报错。</p>
<p>压缩率:.tar.bz2 > .tar.gz > .zip</p>
<h3 id="zip格式"><a href="#zip格式" class="headerlink" title=".zip格式"></a>.zip格式</h3><p>压缩命令为:zip 压缩文件名 源文件</p>
<p>例如:</p>
<ol>
<li>压缩文件 zip test.zip test.txt</li>
<li>压缩目录 zip -r test.zip test/</li>
<li>压缩多个文件 zip test.zuo 1.txt 2.txt</li>
<li>压缩多个目录 zip -r test.zip d1 d2</li>
<li>压缩多个目录和文件 zip -r test.zip d1 test.txt</li>
</ol>
<blockquote>
<p>可操作所有目录下的文件,在前面加上绝对或相对路径即可</p>
</blockquote>
<p>解压缩命令为:unzip 压缩文件名</p>
<p>例如:</p>
<ol>
<li>普通解压命令 unzip test.zip </li>
<li>解压到指定目录 unzip test.zip -d /tmp/</li>
<li>解压到指定目录没有就创建一个 unzip test.zip -d /tmp/test</li>
<li>查看压缩包内容 unzip -v test.zip</li>
</ol>
<h3 id="gz格式"><a href="#gz格式" class="headerlink" title=".gz格式"></a>.gz格式</h3><p>压缩命令为:gzip 源文件</p>
<p>压缩为.gz格式的压缩文件,源文件会消失。</p>
<p>gzip -c 源文件 > 压缩文件<br>如: gzip -c test.txt > text.gz<br>压缩为.gz格式的压缩文件,源文件保留。<br>没有一个像bz2一样的选项可以保留源文件,只能用这种比较trick的方式</p>
<p>gzip -r 目录<br>压缩目录下所有的子文件,但是不能压缩目录本身。</p>
<p>解压缩命令为:</p>
<p>1.gzip -d 压缩文件</p>