-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
2875 lines (2291 loc) · 211 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Memos]]></title>
<link href="http://liunian.github.io/atom.xml" rel="self"/>
<link href="http://liunian.github.io/"/>
<updated>2018-07-04T11:52:28+08:00</updated>
<id>http://liunian.github.io/</id>
<author>
<name><![CDATA[流年]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[CSS 蒙版]]></title>
<link href="http://liunian.github.io/blog/css-masking/"/>
<updated>2016-09-06T14:40:54+08:00</updated>
<id>http://liunian.github.io/blog/css-masking</id>
<content type="html"><![CDATA[<p>使用 CSS 蒙版可以帮助达到类似弹层指引的效果。</p>
<p>css 蒙版的关键点是:黑色表示完全透明展示;白色表示不展示,即图片完全被遮盖,黑色一片;而中间的颜色则表示相应的透明度。</p>
<p>示例代码:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><div</span> <span class="na">id=</span><span class="s">"container"</span> <span class="na">style=</span><span class="s">"position: relative;"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><svg</span> <span class="na">style=</span><span class="s">"position: absolute;"</span> <span class="na">width=</span><span class="s">"400"</span> <span class="na">height=</span><span class="s">"280"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><defs></span>
</span><span class='line'> <span class="nt"><mask</span> <span class="na">id=</span><span class="s">"mask3"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><rect</span> <span class="na">x=</span><span class="s">"0"</span> <span class="na">y=</span><span class="s">"0"</span> <span class="na">width=</span><span class="s">"100%"</span> <span class="na">height=</span><span class="s">"100%"</span> <span class="na">style=</span><span class="s">"stroke:none; fill: #888"</span><span class="nt">></rect></span>
</span><span class='line'> <span class="nt"><circle</span> <span class="na">id=</span><span class="s">"circle1"</span> <span class="na">cx=</span><span class="s">"100"</span> <span class="na">cy=</span><span class="s">"100"</span> <span class="na">r=</span><span class="s">"50"</span> <span class="nt">/></span>
</span><span class='line'> <span class="nt"><rect</span> <span class="na">x=</span><span class="s">"200"</span> <span class="na">y=</span><span class="s">"200"</span> <span class="na">width=</span><span class="s">"100"</span> <span class="na">height=</span><span class="s">"50"</span><span class="nt">></rect></span>
</span><span class='line'> <span class="nt"></mask></span>
</span><span class='line'> <span class="nt"></defs></span>
</span><span class='line'>
</span><span class='line'> <span class="nt"><rect</span> <span class="na">x=</span><span class="s">"0"</span> <span class="na">y=</span><span class="s">"0"</span> <span class="na">width=</span><span class="s">"100%"</span> <span class="na">height=</span><span class="s">"100%"</span> <span class="na">style=</span><span class="s">"stroke: none; mask: url(#mask3)"</span><span class="nt">></rect></span>
</span><span class='line'> <span class="nt"></svg></span>
</span><span class='line'>
</span><span class='line'> <span class="nt"><img</span> <span class="na">src=</span><span class="s">"http://img6.cache.netease.com/cnews/2014/11/3/20141103100737855b7.jpg"</span> <span class="nt">/></span>
</span><span class='line'><span class="nt"></div></span>
</span></code></pre></td></tr></table></div></figure>
<p>示例效果:</p>
<!--more-->
<p><a class="jsbin-embed" href="http://jsbin.com/wogunecaqa/embed">JS Bin on jsbin.com</a><script src="http://static.jsbin.com/js/embed.min.js?3.39.15"></script></p>
<h2>参考文档</h2>
<ul>
<li><a href="http://www.html5rocks.com/en/tutorials/masking/adobe/">CSS Masking</a></li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[html readonly 属性]]></title>
<link href="http://liunian.github.io/blog/html-readonly-attribute/"/>
<updated>2016-07-23T13:15:38+08:00</updated>
<id>http://liunian.github.io/blog/html-readonly-attribute</id>
<content type="html"><![CDATA[<p><code>readonly</code> 属性用于控制表单字段不可编辑,但和 <code>disabled</code> 不同,form 表单提交时会把值提交上去。</p>
<p>但对以下表单元素无效,包括 type 是 <code>hidden</code>、<code>range</code>、<code>color</code>、<code>checkbox</code>、<code>radio</code>、<code>file</code> 以及按钮类型(button 或 submit)。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[ES2015之对象计算属性值(computed property name)]]></title>
<link href="http://liunian.github.io/blog/es2015-computed-property-name/"/>
<updated>2016-04-28T10:04:55+08:00</updated>
<id>http://liunian.github.io/blog/es2015-computed-property-name</id>
<content type="html"><![CDATA[<p>对于 JavaScript 普通对象,定义和使用一般如下:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">a</span><span class="o">:</span> <span class="mi">1</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'><span class="nx">obj</span><span class="p">.</span><span class="nx">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">k</span> <span class="o">=</span> <span class="s1">'c'</span><span class="p">;</span>
</span><span class='line'><span class="nx">obj</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span>
</span><span class='line'><span class="nx">obj</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>
<p>如果属性名(property name)是变量,那么引用时需要使用 <code>[]</code> 语法。</p>
<p>ES2015 中,使用字面量创建对象时也可以使用变量来创建属性名,具体方案是使用计算属性名(computed property name),使用形式是 <code>[property]</code>,如:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="o">:</span> <span class="mi">3</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj</span><span class="p">);</span> <span class="c1">// {'1': 3}</span>
</span></code></pre></td></tr></table></div></figure>
<p>并不是只能使用变量,而是可以使用一切表达式,如:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="c1">// Computed property names (ES6)</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="p">[</span><span class="s2">"foo"</span> <span class="o">+</span> <span class="o">++</span><span class="nx">i</span><span class="p">]</span><span class="o">:</span> <span class="nx">i</span><span class="p">,</span>
</span><span class='line'> <span class="p">[</span><span class="s2">"foo"</span> <span class="o">+</span> <span class="o">++</span><span class="nx">i</span><span class="p">]</span><span class="o">:</span> <span class="nx">i</span><span class="p">,</span>
</span><span class='line'> <span class="p">[</span><span class="s2">"foo"</span> <span class="o">+</span> <span class="o">++</span><span class="nx">i</span><span class="p">]</span><span class="o">:</span> <span class="nx">i</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">foo1</span><span class="p">);</span> <span class="c1">// 1</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">foo2</span><span class="p">);</span> <span class="c1">// 2</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">foo3</span><span class="p">);</span> <span class="c1">// 3</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">param</span> <span class="o">=</span> <span class="s1">'size'</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">config</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="p">[</span><span class="nx">param</span><span class="p">]</span><span class="o">:</span> <span class="mi">12</span><span class="p">,</span>
</span><span class='line'> <span class="p">[</span><span class="s2">"mobile"</span> <span class="o">+</span> <span class="nx">param</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">0</span><span class="p">).</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">param</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">)]</span><span class="o">:</span> <span class="mi">4</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">config</span><span class="p">);</span> <span class="c1">// { size: 12, mobileSize: 4 }</span>
</span></code></pre></td></tr></table></div></figure>
<h2>参考链接</h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names">Computed property names</a></li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Arrow function in ES6]]></title>
<link href="http://liunian.github.io/blog/arrow-function-in-es6/"/>
<updated>2015-12-28T17:27:28+08:00</updated>
<id>http://liunian.github.io/blog/arrow-function-in-es6</id>
<content type="html"><![CDATA[<p>Arrow function 的使用一来能简化匿名函数的使用,二来能解决匿名函数中不时碰到的 <code>this</code> 指向的问题。</p>
<p>在 JavaScript 中,大量地使用到匿名函数,比如事件绑定和像 map/reduce/filter 等这些方法中。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">selected</span> <span class="o">=</span> <span class="nx">allJobs</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">job</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="nx">job</span><span class="p">.</span><span class="nx">isSelected</span><span class="p">();</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>使用 arrow function 后,可以简化为下面这样:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">selected</span> <span class="o">=</span> <span class="nx">allJobs</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="nx">job</span> <span class="o">=></span> <span class="nx">job</span><span class="p">.</span><span class="nx">isSelected</span><span class="p">());</span>
</span></code></pre></td></tr></table></div></figure>
<!-- more -->
<p>可以注意到的是,在上面的语句中,不需要显示地 return。</p>
<p>在多参数的情况下或无参数的情况下,参数列表需要用 <code>()</code> 来包围,如:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="c1">// ES5</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">total</span> <span class="o">=</span> <span class="nx">values</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span>
</span><span class='line'><span class="p">},</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// ES6</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">total</span> <span class="o">=</span> <span class="nx">values</span><span class="p">.</span><span class="nx">reduce</span><span class="p">((</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="o">=></span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>
<p>在函数体不是一个语句的时候,需要用 <code>{}</code> 来包住,并且这个时候将需要显示的 <code>return</code> 才会返回值。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="c1">// ES5</span>
</span><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s2">"#confetti-btn"</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">playTrumpet</span><span class="p">();</span>
</span><span class='line'> <span class="nx">fireConfettiCannon</span><span class="p">();</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// ES6</span>
</span><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s2">"#confetti-btn"</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="nx">event</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="nx">playTrumpet</span><span class="p">();</span>
</span><span class='line'> <span class="nx">fireConfettiCannon</span><span class="p">();</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<h2>this</h2>
<p>arrow function 没有自己的 this 值,任何情况下 arrow function 里的 this 的值都是继承自 arrow function 的上下文。</p>
<p>这同时也意味着,即便使用 <code>call</code> 或 <code>apply</code>,也不会改变 <code>this</code> 的指向。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">a</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="s1">'i.a'</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">let</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">a</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="s1">'obj.a'</span>
</span><span class='line'> <span class="p">},</span>
</span><span class='line'> <span class="nx">b</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">setTimeout</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'obj.b'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">a</span><span class="p">())</span>
</span><span class='line'> <span class="p">},</span> <span class="mi">1000</span><span class="p">)</span>
</span><span class='line'> <span class="p">},</span>
</span><span class='line'> <span class="nx">c</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">f</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'obj.c'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">a</span><span class="p">())</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="nx">f</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span>
</span><span class='line'> <span class="p">},</span>
</span><span class='line'> <span class="nx">d</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">f</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=></span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'obj.d'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">a</span><span class="p">())</span>
</span><span class='line'> <span class="nx">f</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">obj</span><span class="p">.</span><span class="nx">b</span><span class="p">()</span> <span class="c1">// obj.b obj.a</span>
</span><span class='line'><span class="nx">obj</span><span class="p">.</span><span class="nx">c</span><span class="p">()</span> <span class="c1">// obj.c i.a</span>
</span><span class='line'><span class="nx">obj</span><span class="p">.</span><span class="nx">d</span><span class="p">()</span> <span class="c1">// obj.d obj.a</span>
</span></code></pre></td></tr></table></div></figure>
<h2>参考资料</h2>
<ul>
<li><a href="https://hacks.mozilla.org/2015/06/es6-in-depth-arrow-functions/">ES6 In Depth: Arrow functions</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">Arrow functions</a></li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Rest parameters and defaults in ES6]]></title>
<link href="http://liunian.github.io/blog/rest-parameters-and-defaults-in-es6/"/>
<updated>2015-12-28T17:25:10+08:00</updated>
<id>http://liunian.github.io/blog/rest-parameters-and-defaults-in-es6</id>
<content type="html"><![CDATA[<p>ES6 给函数增加了剩余参数(rest parameters)和参数默认值的功能。</p>
<h2>剩余参数</h2>
<p>因为 JavaScript 并没有重载功能,以及为了支持可变参数的情况,是通过 <code>arguments</code> 来实现的。ES6 增加了剩余参数这个功能,希望能简化下这种情况下的实现。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span> <span class="nx">containsAll</span><span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="p">...</span><span class="nx">needles</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">needle</span> <span class="nx">of</span> <span class="nx">needles</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">haystack</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">needle</span><span class="p">)</span> <span class="o">===</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">containsAll</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">));</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">containsAll</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">));</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">containsAll</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">));</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">containsAll</span><span class="p">());</span>
</span></code></pre></td></tr></table></div></figure>
<h2>参数默认值</h2>
<p>参数默认值可用来解决以前需要用 <code>param = param || 'default'</code> 或 <code>if</code> 判断设置的情况。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span> <span class="nx">sayHello</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">word</span><span class="o">=</span><span class="s2">"hello"</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="err">`</span><span class="nx">$</span><span class="p">{</span><span class="nx">word</span><span class="p">},</span> <span class="nx">$</span><span class="p">{</span><span class="nx">name</span><span class="p">}</span><span class="err">`</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="nx">sayHello</span><span class="p">(</span><span class="s1">'Jimmy'</span><span class="p">);</span>
</span><span class='line'><span class="nx">sayHello</span><span class="p">(</span><span class="s1">'Kate'</span><span class="p">,</span> <span class="s1">'你好'</span><span class="p">);</span>
</span><span class='line'><span class="nx">sayHello</span><span class="p">(</span><span class="s1">'Jimmy'</span><span class="p">,</span> <span class="kc">undefined</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>
<p>ES6 中,由于默认参数的表达式实在调用时自左而右地计算,所以,默认参数可以使用其之前的参数,如下:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span> <span class="nx">conditionalParameter</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="o">=</span><span class="p">(</span><span class="nx">a</span><span class="o">==</span><span class="mi">1</span> <span class="o">?</span> <span class="mi">2</span> <span class="o">:</span> <span class="mi">3</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">conditionalParameter</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">));</span><span class="c1">// 7</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">conditionalParameter</span><span class="p">(</span><span class="mi">1</span><span class="p">));</span> <span class="c1">// 3</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">conditionalParameter</span><span class="p">(</span><span class="mi">2</span><span class="p">));</span> <span class="c1">// 5</span>
</span></code></pre></td></tr></table></div></figure>
<h2>参考资料</h2>
<ul>
<li><a href="https://hacks.mozilla.org/2015/05/es6-in-depth-rest-parameters-and-defaults/">ES6 In Depth: Rest parameters and defaults</a></li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Template Strings in ES6]]></title>
<link href="http://liunian.github.io/blog/template-strings-in-es6/"/>
<updated>2015-12-28T17:22:27+08:00</updated>
<id>http://liunian.github.io/blog/template-strings-in-es6</id>
<content type="html"><![CDATA[<p>ES6 中的 template string 并不是 Mustache 这类模板库的替代品,最重要的是提供字符串插值功能,但并不提供诸如 if/for 等控制功能</p>
<h2>定义</h2>
<p>普通的字符串是使用 <code>'</code> 或 <code>"</code> 来定义,template string 是使用 ““`。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">let</span> <span class="nx">s</span> <span class="o">=</span> <span class="err">`</span><span class="nx">This</span> <span class="nx">is</span> <span class="nx">a</span> <span class="nx">template</span> <span class="nx">string</span><span class="err">`</span>
</span></code></pre></td></tr></table></div></figure>
<p>不同的是,template string 可以直接用来定义多行的字符串,并且保留其换行和空白。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">let</span> <span class="nx">ss</span> <span class="o">=</span> <span class="err">`</span><span class="nx">This</span> <span class="nx">is</span> <span class="nx">a</span>
</span><span class='line'><span class="nx">multiple</span> <span class="nx">line</span> <span class="nx">templat</span> <span class="nx">string</span>
</span><span class='line'> <span class="nx">which</span> <span class="nx">reserves</span> <span class="nx">space</span><span class="err">`</span>
</span></code></pre></td></tr></table></div></figure>
<h2>插值</h2>
<p>和普通字符串相比,除了能够方便地定义多行的字符串外,还支持字符串插值,插值处支持任何 JavaScript 表达式,如果表达式返回值不是字符串,那么则按照通用的规则来做转换。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">let</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">name</span><span class="o">:</span> <span class="s1">'obj'</span><span class="p">,</span>
</span><span class='line'> <span class="nx">say</span><span class="o">:</span> <span class="p">()</span> <span class="o">=></span> <span class="p">{</span><span class="k">return</span> <span class="mi">3</span><span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">let</span> <span class="nx">c</span> <span class="o">=</span> <span class="err">`</span><span class="nx">The</span> <span class="nx">$</span><span class="p">{</span><span class="nx">obj</span><span class="p">.</span><span class="nx">name</span><span class="p">}</span> <span class="nx">says</span><span class="o">:</span> <span class="nx">$</span><span class="p">{</span><span class="nx">obj</span><span class="p">.</span><span class="nx">say</span><span class="p">()}</span><span class="err">`</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span> <span class="c1">// The obj says: 3</span>
</span></code></pre></td></tr></table></div></figure>
<h2>参考资料</h2>
<ul>
<li><a href="https://hacks.mozilla.org/2015/05/es6-in-depth-template-strings-2/">ES6 In Depth: Template strings</a></li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Generator in ES6]]></title>
<link href="http://liunian.github.io/blog/generator-in-es6/"/>
<updated>2015-12-28T17:13:20+08:00</updated>
<id>http://liunian.github.io/blog/generator-in-es6</id>
<content type="html"><![CDATA[<p>生成器(Generator)是一个由生成器函数返回的对象,其既是一个迭代器,也是一个可迭代对象。</p>
<p>生成器函数则是一个使用 <code>function*</code> 来声明的函数,如:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">gen</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<h2>能够暂停的函数</h2>
<p>由于 JavaScript 是单线程的,所以一旦执行一个函数,那么一定是按顺序执行完函数体里的语句才能执行这个函数后面的语句。</p>
<p>生成器提供一个能力,可以在指定地方暂停,然后再在需要的时候回到暂停处继续执行。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">gen</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
</span><span class='line'> <span class="k">yield</span><span class="p">;</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">it</span> <span class="o">=</span> <span class="nx">gen</span><span class="p">();</span>
</span><span class='line'><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
</span><span class='line'><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>
<p>上面代码中,log 的顺序将是 <code>1 3 2</code>。</p>
<p>首先,调用生成器函数不会运行里面的代码,而是返回一个生成器。对于一个生成器而言,调用 <code>next</code> 方法来开始运行,然后在里面碰到 <code>yield</code> 时暂停里面的运行,而回到 <code>next</code> 调用处继续往下执行;再次调用 <code>next</code>,则回到生成器上次停止的地方继续往下执行,直至碰到 <code>yield</code> 而暂停里面回到外面或执行完毕。</p>
<!-- more -->
<h2>作为可迭代对象使用</h2>
<p>上面说到,生成器也是一个可迭代对象,那么,这样就可以使用 <code>for-of</code> 来迭代。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">yield</span> <span class="mi">1</span><span class="p">;</span>
</span><span class='line'> <span class="k">yield</span> <span class="mi">2</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="mi">100</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">it</span> <span class="o">=</span> <span class="nx">foo</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'=== for of ==='</span><span class="p">);</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">v</span> <span class="nx">of</span> <span class="nx">foo</span><span class="p">())</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">v</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>由于 <code>for-of</code> 是直接迭代值,所以,log 出来的直接就是 <code>1</code>、<code>2</code> 这样的值。</p>
<p>但需要注意的是,<code>for-of</code> 仅能遍历通过 <code>yield</code> 返回的值,通过 <code>return</code> 返回的值并不在其遍历范围。但 <code>next</code> 是可以拿到的,且 <code>return</code> 时拿到的迭代结果中的 <code>done</code> 是 <code>true</code>。</p>
<h2>生成器内外双向传值</h2>
<p>在调用 <code>next</code> 触发生成器运行到下一个 <code>yield</code> 处时,会同时返回迭代结果;下一次调用 <code>next</code> 时,可以传递一个参数进去,而这个值将是 <code>yield</code> 表达式的值。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">first</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">y</span> <span class="o">=</span> <span class="p">(</span><span class="k">yield</span> <span class="s1">'foo'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="nx">y</span><span class="p">);</span>
</span><span class='line'> <span class="k">yield</span> <span class="s1">'ok'</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">it</span> <span class="o">=</span> <span class="nx">first</span><span class="p">();</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span>
</span><span class='line'><span class="c1">// send 3 into the place of the first yield as the pass-in value</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">(</span><span class="mi">3</span><span class="p">));</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span>
</span></code></pre></td></tr></table></div></figure>
<p>上面代码输出结果将是</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="p">{</span><span class="nx">done</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span> <span class="nx">value</span><span class="o">:</span> <span class="s1">'foo'</span><span class="p">}</span>
</span><span class='line'><span class="mi">4</span>
</span><span class='line'><span class="p">{</span><span class="nx">done</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span> <span class="nx">value</span><span class="o">:</span> <span class="s1">'ok'</span><span class="p">}</span>
</span><span class='line'><span class="p">{</span><span class="nx">done</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">value</span><span class="o">:</span> <span class="kc">undefined</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<ol>
<li>第一次调用 <code>next</code>,生成器在运行了 <code>yield 'foo'</code> 后暂停,把 <code>foo</code> 给 <code>next</code> 返回;</li>
<li>调用 <code>next(3)</code>,回到第一次 yield 的地方,把传入的 <code>3</code> 作为 yield 表达式的值赋给 <code>y</code>;然后运行到第二个 <code>yield</code> 处暂停,向 <code>next</code> 调用提供 <code>'ok'</code>;中间 log 了一下;</li>
<li>最后调用 <code>next</code>,生成器运行至结束</li>
</ol>
<h2><code>yield*</code> 委托</h2>
<p><code>yield*</code> 的作用是迭代一个可迭代对象,把拿到的值再 yield 出去。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">yield</span> <span class="mi">3</span><span class="p">;</span>
</span><span class='line'> <span class="k">yield</span> <span class="mi">4</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">bar</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">yield</span> <span class="mi">1</span><span class="p">;</span>
</span><span class='line'> <span class="k">yield</span> <span class="mi">2</span><span class="p">;</span>
</span><span class='line'> <span class="k">yield</span><span class="o">*</span> <span class="nx">foo</span><span class="p">();</span>
</span><span class='line'> <span class="k">yield</span> <span class="mi">5</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">v</span> <span class="nx">of</span> <span class="nx">bar</span><span class="p">())</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">v</span><span class="p">);</span> <span class="c1">// 1 2 3 4 5</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p><code>yield*</code> 这个表达式本身的值是迭代可迭代对象最后返回的值(第一次 done 是 true 时的值),一般地是 <code>undefined</code>,如果是委托了一个生成器,并且这个生成器最后有 return,那么表达式的值则是 return 的值。</p>
<h2>错误捕捉</h2>
<p>可以对生成器函数体做捕捉,也可以对生成器的调用做捕捉。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">try</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="k">yield</span> <span class="mi">3</span><span class="p">;</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span>
</span><span class='line'> <span class="nx">x</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Error: '</span> <span class="o">+</span> <span class="nx">err</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">it</span> <span class="o">=</span> <span class="nx">foo</span><span class="p">();</span>
</span><span class='line'><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
</span><span class='line'><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="c1">// catch from the outside</span>
</span><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">bar</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="k">yield</span> <span class="mi">3</span><span class="p">;</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span>
</span><span class='line'> <span class="nx">x</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">it2</span> <span class="o">=</span> <span class="nx">bar</span><span class="p">();</span>
</span><span class='line'><span class="nx">it2</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
</span><span class='line'><span class="k">try</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">it2</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<h2>异步</h2>
<p>生成器能在 yield 时暂停,next 的时候再继续运行。这样,或许能在碰到异步操作时 yield 来暂停,异步完成后再 next 继续运行,从而解决回调嵌套过深以及带来的理解问题。</p>
<h3>常见的嵌套回调</h3>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span> <span class="nx">asyncA</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">setTimeout</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">callback</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="mi">1000</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">function</span> <span class="nx">asyncB</span><span class="p">(</span><span class="nx">input</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">setTimeout</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">callback</span><span class="p">(</span><span class="nx">input</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">1000</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">asyncA</span><span class="p">((</span><span class="nx">data</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'asyncA data'</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span>
</span><span class='line'> <span class="nx">asyncB</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="p">(</span><span class="nx">newData</span><span class="p">)</span> <span class="o">=></span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'asyncB data'</span><span class="p">,</span> <span class="nx">newData</span><span class="p">));</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<h3>尝试使用 generator</h3>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">it</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">function</span> <span class="nx">a</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">setTimeout</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="mi">1000</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">function</span> <span class="nx">b</span><span class="p">(</span><span class="nx">input</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">setTimeout</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">(</span><span class="nx">input</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">1000</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">function</span><span class="o">*</span> <span class="nx">gc</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">dataA</span> <span class="o">=</span> <span class="k">yield</span> <span class="nx">a</span><span class="p">();</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'dataA'</span><span class="p">,</span> <span class="nx">dataA</span><span class="p">);</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">dataB</span> <span class="o">=</span> <span class="k">yield</span> <span class="nx">b</span><span class="p">(</span><span class="nx">dataA</span><span class="p">);</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'dataB'</span><span class="p">,</span> <span class="nx">dataB</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">it</span> <span class="o">=</span> <span class="nx">gc</span><span class="p">();</span>
</span><span class='line'><span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>
<p>两个异步的调用和依赖看起来是和同步一样一条路下来了,但却对异步函数本身造成了入侵。一来依赖了一个外部迭代器;二来,无法在多次运行的场景保证正确。</p>
<h3>generator 配合 promise</h3>
<p>上面的例子中,之所以会造成入侵,是因为需要在异步完成后通知生成器来继续运行,而异步代码本身没提供完成态的钩子。</p>
<p>ES6 中提供了 Promise,可以设置异步完成或错误时的相应行为。</p>
<p>这样,可以异步行为的函数返回一个 promise,然后在 promise 的 then 中调用 next。但这个过程不应手动,否则直接使用 promise 就可以了,没必要再配合 generator。应该让 next 的过程自动完成,方法是使用一个运行器。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span> <span class="nx">runGenerator</span><span class="p">(</span><span class="nx">g</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">it</span> <span class="o">=</span> <span class="nx">g</span><span class="p">(),</span> <span class="nx">ret</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="p">(</span><span class="kd">function</span> <span class="nx">iter</span><span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">it</span><span class="p">.</span><span class="nx">next</span><span class="p">(</span><span class="nx">val</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">ret</span><span class="p">.</span><span class="nx">done</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">// 简化,假定一定是返回 promise</span>
</span><span class='line'> <span class="nx">ret</span><span class="p">.</span><span class="nx">value</span><span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">iter</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">})();</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">function</span> <span class="nx">a2</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="k">new</span> <span class="nx">Promise</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">resolve</span><span class="p">,</span> <span class="nx">reject</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">setTimeout</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">resolve</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="mi">1000</span><span class="p">);</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">function</span> <span class="nx">b2</span><span class="p">(</span><span class="nx">input</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="k">new</span> <span class="nx">Promise</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">resolve</span><span class="p">,</span> <span class="nx">reject</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">setTimeout</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">resolve</span><span class="p">(</span><span class="nx">input</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">1000</span><span class="p">);</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">runGenerator</span><span class="p">(</span><span class="kd">function</span><span class="o">*</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">res1</span> <span class="o">=</span> <span class="k">yield</span> <span class="nx">a2</span><span class="p">();</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'res1'</span><span class="p">,</span> <span class="nx">res1</span><span class="p">);</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">res2</span> <span class="o">=</span> <span class="k">yield</span> <span class="nx">b2</span><span class="p">(</span><span class="nx">res1</span><span class="p">);</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'res2'</span><span class="p">,</span> <span class="nx">res2</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>可以看到,这样,异步函数本身保持了纯净,调用过程也能和同步一样看起来顺畅,把复杂性放到了运行器中。当然,这个运行器只是一个简单的模拟,实际需要考虑不是返回 promise 的情况,promise 里是 reject 的情况。</p>
<h2>参考资料</h2>
<ul>
<li><a href="https://hacks.mozilla.org/2015/05/es6-in-depth-generators/">ES6 In Depth: Generator</a></li>
<li><a href="https://davidwalsh.name/async-generators">async generators</a></li>
<li><a href="http://www.2ality.com/2015/03/no-promises.html">No promises: asynchronous JavaScript with only generators</a></li>
<li><a href="https://www.twilio.com/blog/2015/10/asyncawait-the-hero-javascript-deserved.html">Async/Await: The Hero JavaScript Deserved</a></li>
<li><a href="https://tc39.github.io/ecmascript-asyncawait/">Async Functions</a></li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Iterator in ES6]]></title>
<link href="http://liunian.github.io/blog/iterator-in-es6/"/>
<updated>2015-12-24T16:17:41+08:00</updated>
<id>http://liunian.github.io/blog/iterator-in-es6</id>
<content type="html"><![CDATA[<p><code>for-of</code> 使用迭代器(iterator)来迭代一个可迭代(iterable)的对象,主要是想取代传统的比较繁琐的 <code>for</code> 及会遍历非自带属性以及无法 <code>continue/break/return</code> 的 <code>forEach</code>,其基本示例如:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="nx">of</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p><code>for-of</code> 默认支持了数组、字符串、类数组以及 ES6 中的 Map 和 Set 等,但不支持平对象(字面量对象)(实际支持情况还看浏览器,比如在此刻 firefox 支持了 NodeList,但 Chrome 还不支持)。</p>
<p>迭代 Map 可以像上面那样使用单变量来在每次迭代时得到一个数组,也可以配合列表结构来直接得到 key 和 value。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">let</span> <span class="nx">aMap</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Map</span><span class="p">();</span>
</span><span class='line'><span class="nx">aMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'><span class="nx">aMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">]);</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="p">[</span><span class="nx">k</span><span class="p">,</span> <span class="nx">v</span><span class="p">]</span> <span class="nx">of</span> <span class="nx">aMap</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">k</span><span class="p">,</span> <span class="nx">v</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<!-- more -->
<h2>迭代器</h2>
<p>一个对象只要遵循迭代器协议就是一个迭代器。</p>
<h3>迭代器协议</h3>
<p>实现了 <code>.next()</code> 方法,该方法返回一个字面量对象,里面需要有两个属性,<code>done</code> 表示是否迭代了最后,<code>value</code> 则是每次迭代需要返回的值。</p>
<h2>可迭代对象</h2>
<p>一个对象只要遵循了下面的协议,那么就是可迭代对象。</p>
<h3>可迭代协议</h3>
<p>一个对象或其原型上有 <code>@@iterator</code> 函数( <code>[Symbol.iterator]</code>),该函数无实参,返回一个遵循迭代器协议的对象。</p>
<p>下面是一个简单的迭代器(同时也是一个可迭代对象),for-of 将会输出 5 4 3 2 1.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">fiveTimesIterator</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">_count</span><span class="o">:</span> <span class="mi">5</span><span class="p">,</span>
</span><span class='line'> <span class="p">[</span><span class="nx">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span><span class="o">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'> <span class="p">},</span>
</span><span class='line'> <span class="nx">next</span><span class="o">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_count</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">_count</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
</span><span class='line'> <span class="k">return</span> <span class="p">{</span><span class="nx">done</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">value</span><span class="o">:</span> <span class="mi">0</span><span class="p">};</span>
</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="p">{</span><span class="nx">done</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span> <span class="nx">value</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">_count</span><span class="o">--</span><span class="p">};</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">f</span> <span class="nx">of</span> <span class="nx">fiveTimesIterator</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">f</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<h2>参考资料</h2>
<ul>
<li><a href="https://hacks.mozilla.org/2015/04/es6-in-depth-iterators-and-the-for-of-loop/">ES6 In Depth: Iterators and the for-of loop</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">Iteration protocols</a></li>
</ul>