-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
947 lines (536 loc) · 43.6 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
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Memos</title>
<meta name="author" content="流年">
<meta name="description" content="使用 CSS 蒙版可以帮助达到类似弹层指引的效果。 css 蒙版的关键点是:黑色表示完全透明展示;白色表示不展示,即图片完全被遮盖,黑色一片;而中间的颜色则表示相应的透明度。 示例代码: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<div id=" …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://liunian.github.io/">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="/javascripts/ender.js"></script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<link href="/atom.xml" rel="alternate" title="Memos" type="application/atom+xml">
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
</head>
<body >
<header role="banner"><hgroup>
<h1><a href="/">Memos</a></h1>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:liunian.github.io" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="main-navigation">
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
<li><a href="/about/">About</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/blog/css-masking/">CSS 蒙版</a></h1>
<p class="meta">
<time datetime="2016-09-06T14:40:54+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><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>
</div>
<footer>
<a rel="full-article" href="/blog/css-masking/">Read on →</a>
</footer>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/html-readonly-attribute/">Html Readonly 属性</a></h1>
<p class="meta">
<time datetime="2016-07-23T13:15:38+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><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>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/es2015-computed-property-name/">ES2015之对象计算属性值(computed Property Name)</a></h1>
<p class="meta">
<time datetime="2016-04-28T10:04:55+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><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>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/arrow-function-in-es6/">Arrow Function in ES6</a></h1>
<p class="meta">
<time datetime="2015-12-28T17:27:28+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><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>
</div>
<footer>
<a rel="full-article" href="/blog/arrow-function-in-es6/">Read on →</a>
</footer>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/rest-parameters-and-defaults-in-es6/">Rest Parameters and Defaults in ES6</a></h1>
<p class="meta">
<time datetime="2015-12-28T17:25:10+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><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>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/template-strings-in-es6/">Template Strings in ES6</a></h1>
<p class="meta">
<time datetime="2015-12-28T17:22:27+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><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>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/generator-in-es6/">Generator in ES6</a></h1>
<p class="meta">
<time datetime="2015-12-28T17:13:20+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><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>
</div>
<footer>
<a rel="full-article" href="/blog/generator-in-es6/">Read on →</a>
</footer>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/iterator-in-es6/">Iterator in ES6</a></h1>
<p class="meta">
<time datetime="2015-12-24T16:17:41+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><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>
</div>
<footer>
<a rel="full-article" href="/blog/iterator-in-es6/">Read on →</a>
</footer>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/shadow-dom/">Shadow Dom</a></h1>
<p class="meta">
<time datetime="2015-09-29T10:17:55+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><p>Shadow DOM 的用途是封装隔离一个元素,从而在组件化的过程中隔离外部样式和 JS 的影响。</p>
<h2>使用</h2>
<h3>创建</h3>
<p>通过在一个元素(A)上创建一个 shadow root 的方式来创建 shadow dom,返回的值成为 shadow root,A 成为 shadow host。</p>
<p>一旦创建了 shadow root,那么宿主元素的原内容将不被展示,而是展示 shadow root 的内容。</p>
<p>可以在一个 shadow host 上创建多个 shadow root(但只有最新的一个会生效,但可以通过 <code><shadow></shadow></code> 来内嵌引用前一个 shadow;并且目前无法移除 shadow root),还可以把一个 shadow root 里的元素作为 shadow host 来继续创建 shadow root。</p>
<figure class='code'><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=''><span class='line'><button>Hello, world!</button>
</span><span class='line'><script>
</span><span class='line'>var host = document.querySelector('button');
</span><span class='line'>var root = host.createShadowRoot();
</span><span class='line'>root.textContent = 'こんにちは、影の世界!';
</span><span class='line'></script></span></code></pre></td></tr></table></div></figure>
<p>通过配合使用 <a href="http://liunian.github.io/blog/html-template/">html template</a> 可以简化创建时的繁琐,如:</p>
<figure class='code'><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=''><span class='line'>var shadow = document.querySelector('#nameTag').createShadowRoot();
</span><span class='line'>var template = document.querySelector('#nameTagTemplate');
</span><span class='line'>var clone = document.importNode(template.content, true);
</span><span class='line'>shadow.appendChild(clone);</span></code></pre></td></tr></table></div></figure>
</div>
<footer>
<a rel="full-article" href="/blog/shadow-dom/">Read on →</a>
</footer>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/html-template/">Html Template</a></h1>
<p class="meta">
<time datetime="2015-09-21T17:49:18+08:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><p>HTML 5.1 计划增加 template 元素,提供一段可被后续被 JavaScript 克隆和插入的代码片段。</p>
<h2>what</h2>
<p><code><template></template></code> 元素是提供页面渲染是不被解析渲染,但在 JavaScript 使用并插入到文档后才解析渲染的代码片段。其可存放任意合法的 HTML 内容,可位于任何合法的非替换元素里,包括 <code><head></code> 等。</p>
<p>如<a href="http://defg.sinaapp.com/demos/template/1.html">示例一</a>:</p>
<pre><code><template>
<style>
div {
color: red;
}
</style>
<div>
<h1>template</h1>
<img src="http://holdit.sinaapp.com/300" />
</div>
<script>
alert('template');
</script>
</template>
</code></pre>
<p>需要注意的是,这是一个可被重复使用的文档片段,并没有像 Mustache 那样的模板插值(“)甚至更高级的流程处理的功能。这是一个静态的模板,不是根据数据来动态生成 HTML 字符串。</p>
</div>
<footer>
<a rel="full-article" href="/blog/html-template/">Read on →</a>
</footer>
</article>
<div class="pagination">
<a class="prev" href="2">← Older</a>
<a href="/blog/archives">Blog Archives</a>
</div>
</div>
<aside class="sidebar">
<section>
<h1>Recent Posts</h1>
<ul id="recent_posts">
<li class="post">
<a href="/blog/css-masking/">CSS 蒙版</a>
</li>
<li class="post">
<a href="/blog/html-readonly-attribute/">html readonly 属性</a>
</li>
<li class="post">
<a href="/blog/es2015-computed-property-name/">ES2015之对象计算属性值(computed property name)</a>
</li>
<li class="post">
<a href="/blog/arrow-function-in-es6/">Arrow function in ES6</a>
</li>
<li class="post">
<a href="/blog/rest-parameters-and-defaults-in-es6/">Rest parameters and defaults in ES6</a>
</li>
</ul>
</section>
</aside>
</div>
</div>
<footer role="contentinfo"><p>
Copyright © 2018 - 流年 -
<span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
</p>
</footer>
</body>
</html>