-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
1581 lines (803 loc) · 62.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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html class="theme-next use-motion ">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"/>
<link href='//fonts.lug.ustc.edu.cn/css?family=Lato:300,400,700,400italic&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=0.4.5.2" rel="stylesheet" type="text/css" />
<meta name="description" content="忙时码代码,无事乱涂鸦---涂鸦码龙 | ECMAScript | JavaScript | CSS3 | HTML5 | Node.js | Mobile Web | 前端技术积累,技术文章翻译" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="alternate" href="/atom.xml" title="Alon's Blog" type="application/atom+xml" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=0.4.5.2" />
<meta name="description" content="忙时码代码,无事乱涂鸦---涂鸦码龙 | ECMAScript | JavaScript | CSS3 | HTML5 | Node.js | Mobile Web | 前端技术积累,技术文章翻译">
<meta property="og:type" content="website">
<meta property="og:title" content="Alon's Blog">
<meta property="og:url" content="http://jinlong.github.io/index.html">
<meta property="og:site_name" content="Alon's Blog">
<meta property="og:description" content="忙时码代码,无事乱涂鸦---涂鸦码龙 | ECMAScript | JavaScript | CSS3 | HTML5 | Node.js | Mobile Web | 前端技术积累,技术文章翻译">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Alon's Blog">
<meta name="twitter:description" content="忙时码代码,无事乱涂鸦---涂鸦码龙 | ECMAScript | JavaScript | CSS3 | HTML5 | Node.js | Mobile Web | 前端技术积累,技术文章翻译">
<script type="text/javascript" id="hexo.configuration">
var CONFIG = {
scheme: 'Mist',
sidebar: 'post',
motion: true
};
</script>
<title> Alon's Blog </title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<!--[if lte IE 8]>
<div style=' clear: both; height: 59px; padding:0 0 0 15px; position: relative;margin:0 auto;'>
<a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
<img src="http://7u2nvr.com1.z0.glb.clouddn.com/picouterie.jpg" border="0" height="42" width="820"
alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today or use other browser ,like chrome firefox safari."
style='margin-left:auto;margin-right:auto;display: block;'/>
</a>
</div>
<![endif]-->
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?2bd777ce3d5910b5c44deb753c24f3e3";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<div class="container one-column
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Alon's Blog</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">一边翻译一边学习---涂鸦码龙</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
<nav class="site-nav">
<ul id="menu" class="menu ">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-home fa-fw"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories" rel="section">
<i class="menu-item-icon fa fa-th fa-fw"></i> <br />
分类
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-archive fa-fw"></i> <br />
归档
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-tags fa-fw"></i> <br />
标签
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="https:/jinlong/jinlong.github.com/issues" itemprop="url">
后期文章更新
</a>
</h1>
<div class="post-meta">
<span class="post-time">
发表于
<time itemprop="dateCreated" datetime="2018-04-29T20:33:31+08:00" content="2018-04-29">
2018-04-29
</time>
</span>
</div>
</header>
<div class="post-body">
最新的文章通过 Issues 形式发布了,请移步 <a href="https:/jinlong/jinlong.github.com/issues">Issues</a>。
PS. 老了,hexo 也懒得折腾了,😅
</div>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/05/08/optimising-the-front-end-for-the-browser/" itemprop="url">
浏览器前端优化
</a>
</h1>
<div class="post-meta">
<span class="post-time">
发表于
<time itemprop="dateCreated" datetime="2017-05-08T20:33:31+08:00" content="2017-05-08">
2017-05-08
</time>
</span>
<span class="post-category" >
| 分类于
<span itemprop="about" itemscope itemtype="https://schema.org/Thing">
<a href="/categories/前端优化/" itemprop="url" rel="index">
<span itemprop="name">前端优化</span>
</a>
</span>
</span>
<span class="post-comments-count">
|
<a href="/2017/05/08/optimising-the-front-end-for-the-browser/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/05/08/optimising-the-front-end-for-the-browser/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body">
<blockquote>
<p>原文:<a href="https://hackernoon.com/optimising-the-front-end-for-the-browser-f2f51a29c572">Optimising the front end for the browser</a><br>笔记:<a href="http://weibo.com/newwave">涂鸦码龙</a></p>
</blockquote>
<p>优化关乎速度和满意度。</p>
<ul>
<li>从用户体验(UX)角度,我们希望前端网页可以快速加载</li>
<li><p>从开发体验(DX)角度,我们希望前端是快速,简洁,规范的</p>
<h1 id="浏览器都做了什么">浏览器都做了什么</h1></li>
</ul>
<p>我们希望浏览器打开一个简单的网页</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="meta"><!DOCTYPE html></span></div><div class="line"><span class="tag"><<span class="name">html</span>></span></div><div class="line"> <span class="tag"><<span class="name">head</span>></span></div><div class="line"> <span class="tag"><<span class="name">title</span>></span>The "Click the button" page<span class="tag"></<span class="name">title</span>></span></div><div class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></div><div class="line"> <span class="tag"><<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"stylesheet"</span> <span class="attr">href</span>=<span class="string">"styles.css"</span> /></span></div><div class="line"> <span class="tag"></<span class="name">head</span>></span></div><div class="line"> </div><div class="line"> <span class="tag"><<span class="name">body</span>></span></div><div class="line"> <span class="tag"><<span class="name">h1</span>></span></div><div class="line"> Click the button.</div><div class="line"> <span class="tag"></<span class="name">h1</span>></span></div><div class="line"> </div><div class="line"> <span class="tag"><<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span>></span>Click me<span class="tag"></<span class="name">button</span>></span></div><div class="line"> </div><div class="line"> <span class="tag"><<span class="name">script</span>></span><span class="undefined"></div><div class="line"> var button = document.querySelector("button");</div><div class="line"> button.style.fontWeight = "bold";</div><div class="line"> button.addEventListener("click", function () {</div><div class="line"> alert("Well done.");</div><div class="line"> });</div><div class="line"> </span><span class="tag"></<span class="name">script</span>></span></div><div class="line"> <span class="tag"></<span class="name">body</span>></span></div><div class="line"><span class="tag"></<span class="name">html</span>></span></div></pre></td></tr></table></figure>
<h2 id="浏览器如何渲染网页">浏览器如何渲染网页</h2><ol>
<li>使用 HTML 创建文档对象模型(<strong>DOM</strong>)</li>
<li>使用 CSS 创建 CSS 对象模型(<strong>CSSOM</strong>)</li>
<li>基于 DOM 和 CSSOM 执行脚本(<strong>Scripts</strong>)</li>
<li>合并 DOM 和 CSSOM 形成渲染树(<strong>Render Tree</strong>)</li>
<li>使用渲染树布局(<strong>Layout</strong>)所有元素</li>
<li>渲染(<strong>Paint</strong>)所有元素</li>
</ol>
<p><img src="/image/browser-fe-optimizing/p1.png" alt="附图"></p>
<div class="post-more-link text-center">
<a class="btn" href="/2017/05/08/optimising-the-front-end-for-the-browser/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/02/04/javascript-array-methods-mutating-vs-non-mutating/" itemprop="url">
JavaScript 数组方法对比
</a>
</h1>
<div class="post-meta">
<span class="post-time">
发表于
<time itemprop="dateCreated" datetime="2017-02-04T18:17:47+08:00" content="2017-02-04">
2017-02-04
</time>
</span>
<span class="post-category" >
| 分类于
<span itemprop="about" itemscope itemtype="https://schema.org/Thing">
<a href="/categories/JavaScript/" itemprop="url" rel="index">
<span itemprop="name">JavaScript</span>
</a>
</span>
</span>
<span class="post-comments-count">
|
<a href="/2017/02/04/javascript-array-methods-mutating-vs-non-mutating/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/02/04/javascript-array-methods-mutating-vs-non-mutating/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body">
<blockquote>
<p>原文:“<a href="http://lorenstewart.me/2017/01/22/javascript-array-methods-mutating-vs-non-mutating/">JavaScript Array Methods: Mutating vs. Non-Mutating</a>”<br>笔记:<a href="http://weibo.com/newwave">涂鸦码龙</a> </p>
</blockquote>
<p>JavaScript 提供了多种新增,移除,替换数组元素的方法,但是有些会影响原来的数组;有些则不会,它是新建了一个数组。</p>
<p><strong>注意</strong>:区分以下两个方法的不同点:</p>
<ol>
<li><code>array.splice()</code> 影响原来的数组</li>
<li><code>array.slice()</code> 不影响原来的数组</li>
</ol>
<h1 id="I-_新增:影响原数组">I. 新增:影响原数组</h1><p>使用 <code>array.push()</code> 和 <code>array.ushift()</code> 新增元素会影响原来的数组。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> mutatingAdd = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>, <span class="string">'e'</span>]; </div><div class="line">mutatingAdd.push(<span class="string">'f'</span>); <span class="comment">// ['a', 'b', 'c', 'd', 'e', 'f']</span></div><div class="line">mutatingAdd.unshift(<span class="string">'z'</span>); <span class="comment">// ['z', 'b', 'c', 'd', 'e' 'f']</span></div></pre></td></tr></table></figure>
<div class="post-more-link text-center">
<a class="btn" href="/2017/02/04/javascript-array-methods-mutating-vs-non-mutating/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/16/react-native-component-communication/" itemprop="url">
React Native 的组件通信方式
</a>
</h1>
<div class="post-meta">
<span class="post-time">
发表于
<time itemprop="dateCreated" datetime="2016-12-16T13:11:24+08:00" content="2016-12-16">
2016-12-16
</time>
</span>
<span class="post-category" >
| 分类于
<span itemprop="about" itemscope itemtype="https://schema.org/Thing">
<a href="/categories/React-Native/" itemprop="url" rel="index">
<span itemprop="name">React Native</span>
</a>
</span>
</span>
<span class="post-comments-count">
|
<a href="/2016/12/16/react-native-component-communication/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/12/16/react-native-component-communication/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body">
<p>题外话,说几句我对 React 与 React Native 关系的理解:</p>
<ul>
<li>React 主要用于浏览器端实现一些 UI 组件,也可用于服务端渲染。React 可以使用 HTML 提供的标签,也可封装自定义的组件,React 也提供直接操作 DOM 的方法;</li>
<li>React Native 主要用于实现客户端应用(App)的 UI 组件,它只能使用 Facebook 封装的 Native 组件,或者自己封装的 Native 组件。开发中主要借助 JavaScript,基本告别 HTML 和 CSS 了,不过优点是可以用 ES6。</li>
</ul>
<hr>
<p>言归正传,正文开始</p>
<p>之前没搞过 React ,直接开撸的 React Native,使用过程也是各种踩坑填坑,磕磕绊绊,这里简单总结一下我用过的组件通信的几种方式吧。</p>
<h1 id="React_最基础的_props_和_state">React 最基础的 props 和 state</h1><h2 id="组件内部用_state">组件内部用 state</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">constructor</span>(props) {</div><div class="line"> <span class="keyword">super</span>(props);</div><div class="line"></div><div class="line"> <span class="keyword">this</span>.state = {</div><div class="line"> <span class="attr">isOnline</span>: <span class="literal">true</span> <span class="comment">//组件 state</span></div><div class="line"> };</div><div class="line">}</div><div class="line">render() {</div><div class="line"> <span class="keyword">if</span>(<span class="keyword">this</span>.state.isOnline){</div><div class="line"> <span class="comment">//...剩余代码</span></div><div class="line"> }</div><div class="line"> <span class="comment">//...剩余代码</span></div><div class="line">}</div></pre></td></tr></table></figure>
<div class="post-more-link text-center">
<a class="btn" href="/2016/12/16/react-native-component-communication/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/05/15/Filtered-background-with-fallback-for-legibility/" itemprop="url">
针对易读性的背景滤镜兼容方案(CSS filter 兼容方案)
</a>
</h1>
<div class="post-meta">
<span class="post-time">
发表于
<time itemprop="dateCreated" datetime="2016-05-15T21:47:29+08:00" content="2016-05-15">
2016-05-15
</time>
</span>
<span class="post-category" >
| 分类于
<span itemprop="about" itemscope itemtype="https://schema.org/Thing">
<a href="/categories/CSS3/" itemprop="url" rel="index">
<span itemprop="name">CSS3</span>
</a>
</span>
</span>
<span class="post-comments-count">
|
<a href="/2016/05/15/Filtered-background-with-fallback-for-legibility/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/05/15/Filtered-background-with-fallback-for-legibility/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body">
<blockquote>
<p>原文:<a href="http://codepen.io/tigt/post/blurred-background-image-with-fallback">Filtered background with fallback for legibility</a><br>翻译:<a href="http://weibo.com/newwave">涂鸦码龙</a></p>
</blockquote>
<p>你知道现在有多火吗?用这种很大的,高质量的,支持 Retina 屏的<a href="https://www.google.com/search?q=blurred+background+web&tbm=isch">模糊的 JPEG 图片作为 header 背景</a> :</p>
<p data-height="265" data-theme-id="0" data-slug-hash="RaprZK" data-default-tab="css,result" data-user="tigt" data-embed-version="2" class="codepen">See the Pen <a href="http://codepen.io/tigt/pen/RaprZK/">Web site header, circa 2016</a> by Taylor Hunt (<a href="http://codepen.io/tigt">@tigt</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
<p>潜在的问题是如果浏览器不支持滤镜 filter,<a href="https://www.nngroup.com/articles/text-over-images/">文字将不可读</a> 。这违背了可访问性的原则,再完美的视觉也无济于事。</p>
<div class="post-more-link text-center">
<a class="btn" href="/2016/05/15/Filtered-background-with-fallback-for-legibility/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/05/01/4-Types-of-Memory-Leaks-in-JavaScript-and-How-to-Get-Rid-Of-Them/" itemprop="url">
4类 JavaScript 内存泄漏及如何避免
</a>
</h1>
<div class="post-meta">
<span class="post-time">
发表于
<time itemprop="dateCreated" datetime="2016-05-01T21:16:27+08:00" content="2016-05-01">
2016-05-01
</time>
</span>
<span class="post-category" >
| 分类于
<span itemprop="about" itemscope itemtype="https://schema.org/Thing">
<a href="/categories/JavaScript/" itemprop="url" rel="index">
<span itemprop="name">JavaScript</span>
</a>
</span>
</span>
<span class="post-comments-count">
|
<a href="/2016/05/01/4-Types-of-Memory-Leaks-in-JavaScript-and-How-to-Get-Rid-Of-Them/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/05/01/4-Types-of-Memory-Leaks-in-JavaScript-and-How-to-Get-Rid-Of-Them/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body">
<blockquote>
<p>原文:<a href="https://auth0.com/blog/2016/01/26/four-types-of-leaks-in-your-javascript-code-and-how-to-get-rid-of-them/">4 Types of Memory Leaks in JavaScript and How to Get Rid Of Them</a><br>笔记:<a href="http://weibo.com/newwave">涂鸦码龙</a></p>
<p>译者注:本文并没有逐字逐句的翻译,而是把我认为重要的信息做了翻译。如果您的英文熟练,可以直接阅读原文。</p>
</blockquote>
<p>本文将探索常见的客户端 JavaScript 内存泄漏,以及如何使用 Chrome 开发工具发现问题。</p>
<h1 id="简介">简介</h1><p>内存泄漏是每个开发者最终都要面对的问题,它是许多问题的根源:反应迟缓,崩溃,高延迟,以及其他应用问题。</p>
<h2 id="什么是内存泄漏?">什么是内存泄漏?</h2><p>本质上,内存泄漏可以定义为:应用程序不再需要占用内存的时候,由于某些原因,内存没有被操作系统或可用内存池回收。编程语言管理内存的方式各不相同。只有开发者最清楚哪些内存不需要了,操作系统可以回收。一些编程语言提供了语言特性,可以帮助开发者做此类事情。另一些则寄希望于开发者对内存是否需要清晰明了。</p>
<h2 id="JavaScript_内存管理">JavaScript 内存管理</h2><p>JavaScript 是一种垃圾回收语言。垃圾回收语言通过周期性地检查先前分配的内存是否可达,帮助开发者管理内存。换言之,垃圾回收语言减轻了“内存仍可用”及“内存仍可达”的问题。两者的区别是微妙而重要的:仅有开发者了解哪些内存在将来仍会使用,而不可达内存通过算法确定和标记,适时被操作系统回收。</p>
<h1 id="JavaScript_内存泄漏">JavaScript 内存泄漏</h1><p>垃圾回收语言的内存泄漏主因是不需要的引用。理解它之前,还需了解垃圾回收语言如何辨别内存的可达与不可达。</p>
<h2 id="Mark-and-sweep">Mark-and-sweep</h2><p>大部分垃圾回收语言用的算法称之为 Mark-and-sweep 。算法由以下几步组成:</p>
<ol>
<li>垃圾回收器创建了一个“roots”列表。Roots 通常是代码中全局变量的引用。JavaScript 中,“window” 对象是一个全局变量,被当作 root 。window 对象总是存在,因此垃圾回收器可以检查它和它的所有子对象是否存在(即不是垃圾);</li>
<li>所有的 roots 被检查和标记为激活(即不是垃圾)。所有的子对象也被递归地检查。从 root 开始的所有对象如果是可达的,它就不被当作垃圾。</li>
<li>所有未被标记的内存会被当做垃圾,收集器现在可以释放内存,归还给操作系统了。</li>
</ol>
<p>现代的垃圾回收器改良了算法,但是本质是相同的:可达内存被标记,其余的被当作垃圾回收。</p>
<p>不需要的引用是指开发者明知内存引用不再需要,却由于某些原因,它仍被留在激活的 root 树中。在 JavaScript 中,不需要的引用是保留在代码中的变量,它不再需要,却指向一块本该被释放的内存。有些人认为这是开发者的错误。</p>
<p>为了理解 JavaScript 中最常见的内存泄漏,我们需要了解哪种方式的引用容易被遗忘。</p>
<div class="post-more-link text-center">
<a class="btn" href="/2016/05/01/4-Types-of-Memory-Leaks-in-JavaScript-and-How-to-Get-Rid-Of-Them/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/04/24/Debouncing-and-Throttling-Explained-Through-Examples/" itemprop="url">
实例解析防抖动(Debouncing)和节流阀(Throttling)
</a>
</h1>
<div class="post-meta">
<span class="post-time">
发表于
<time itemprop="dateCreated" datetime="2016-04-24T22:30:00+08:00" content="2016-04-24">
2016-04-24
</time>
</span>
<span class="post-category" >
| 分类于
<span itemprop="about" itemscope itemtype="https://schema.org/Thing">
<a href="/categories/JavaScript/" itemprop="url" rel="index">
<span itemprop="name">JavaScript</span>
</a>
</span>
</span>
<span class="post-comments-count">
|
<a href="/2016/04/24/Debouncing-and-Throttling-Explained-Through-Examples/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/04/24/Debouncing-and-Throttling-Explained-Through-Examples/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body">
<blockquote>
<p>原文:<a href="https://css-tricks.com/debouncing-throttling-explained-examples/">Debouncing and Throttling Explained Through Examples</a><br>笔记:<a href="http://weibo.com/newwave">涂鸦码龙</a></p>
</blockquote>
<p>防抖(<strong>Debounce</strong>)和节流(<strong>throttle</strong>)都是用来控制某个函数在一定时间内执行多少次的技巧,两者相似而又不同。</p>
<p>当我们给 DOM 绑定事件的时候,加了防抖和节流的函数变得特别有用。为什么呢?因为我们在事件和函数执行之间加了一个控制层。记住,我们是无法控制 DOM 事件触发频率的。</p>
<p>看下滚动事件的例子:</p>
<p data-height="268" data-theme-id="0" data-slug-hash="PZOZgB" data-default-tab="result" data-user="dcorb" class="codepen">See the Pen <a href="http://codepen.io/dcorb/pen/PZOZgB/">Scroll events counter</a> by Corbacho (<a href="http://codepen.io/dcorb">@dcorb</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
<p>当使用触控板,滚动滚轮,或者拖拽滚动条的时候,一秒可以轻松触发30次事件。经我的测试,在智能手机上,慢慢滚动一下,一秒可以触发事件100次之多。这么高的执行频率,你的滚动回调函数压力大吗?</p>
<p>早在2011年,Twitter 网站抛出了一个问题:向下滚动 Twitter 信息流的时候,变得很慢,很迟钝。John Resig 发表了<a href="http://ejohn.org/blog/learning-from-twitter">一篇博客解释这个问题</a>,文中解释到直接给 <code>scroll</code> 事件关联昂贵的函数,是多么糟糕的主意。</p>
<p>John(5年前)建议的解决方案是,在 <code>onScroll</code> 事件外部,每 250ms 循环执行一次。简单的技巧,避免了影响用户体验。</p>
<p>现如今,有一些稍微高端的方式处理事件。我来结合用例介绍下 Debounce,Throttle 和 requestAnimationFrame 吧。</p>
<div class="post-more-link text-center">
<a class="btn" href="/2016/04/24/Debouncing-and-Throttling-Explained-Through-Examples/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/04/09/Say-Hello-To-ES2015/" itemprop="url">
你好 ES2015
</a>
</h1>
<div class="post-meta">
<span class="post-time">
发表于
<time itemprop="dateCreated" datetime="2016-04-09T11:22:43+08:00" content="2016-04-09">
2016-04-09
</time>
</span>
<span class="post-category" >
| 分类于
<span itemprop="about" itemscope itemtype="https://schema.org/Thing">
<a href="/categories/JavaScript/" itemprop="url" rel="index">
<span itemprop="name">JavaScript</span>
</a>
</span>
</span>
<span class="post-comments-count">
|
<a href="/2016/04/09/Say-Hello-To-ES2015/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/04/09/Say-Hello-To-ES2015/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body">
<blockquote>
<p>原文: <a href="https://tutor.mantrajs.com/say-hello-to-ES2015/introduction">Say Hello To ES2015</a><br>笔记:<a href="http://weibo.com/newwave">涂鸦码龙</a></p>
</blockquote>
<h1 id="介绍_ES2015">介绍 ES2015</h1><p>ES2015 是新版的 JavaScript,Node.js 已经完全支持,浏览器端可以用 <a href="https://babeljs.io/">Babel</a> 库编译。</p>
<p>运行本文的示例代码,可以用 <a href="https://jsbin.com/defeba/edit?js,console">JSBin 环境</a>,也可以结合原文中的测试题检测学习效果。</p>
<h1 id="使用_let_和_const">使用 let 和 const</h1><p>ES2015 可以用 <code>const</code> 或 <code>let</code> 替换 <code>var</code> ,它们定义了块级作用域变量。</p>
<p>示例代码:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span> (<span class="keyword">var</span> lc=<span class="number">0</span>; lc < <span class="number">10</span>; lc++) {</div><div class="line"> <span class="keyword">let</span> value = lc;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="built_in">console</span>.log(value); <span class="comment">//抛出错误</span></div></pre></td></tr></table></figure>
<p>变量 <code>value</code> 只能在 <code>for</code> 循环中使用。</p>
<p><code>const</code> 跟 <code>let</code> 很像,但是它定义的变量值无法改变。</p>
<p>示例代码:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> user = {<span class="attr">name</span>: <span class="string">"Arunoda"</span>};</div><div class="line">user.name = <span class="string">"Susiripala"</span>;</div><div class="line"><span class="built_in">console</span>.log(user.name);</div></pre></td></tr></table></figure>
<p>改变的是变量 <code>user</code> 内部的属性,并没有改变 <code>user</code> 本身。</p>
<p>许多人更喜欢用 <code>const</code> 代替 <code>let</code> 。</p>
<h1 id="使用箭头函数整理你的代码">使用箭头函数整理你的代码</h1><p>熟悉的方式:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> numbers = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">50</span>];</div><div class="line"><span class="keyword">const</span> multiplyBy10 = numbers.map(<span class="function"><span class="keyword">function</span>(<span class="params">a</span>) </span>{</div><div class="line"> <span class="keyword">return</span> a * <span class="number">10</span>;</div><div class="line">});</div><div class="line"><span class="built_in">console</span>.log(multiplyBy10);</div></pre></td></tr></table></figure>
<p>使用箭头函数以后:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> numbers = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">50</span>];</div><div class="line"><span class="keyword">const</span> multiplyBy10 = numbers.map(<span class="function"><span class="params">a</span> =></span> a * <span class="number">10</span>);</div><div class="line"><span class="built_in">console</span>.log(multiplyBy10);</div></pre></td></tr></table></figure>
<p>如果方法接受不止1个参数,可以这么写:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> numbers = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">50</span>];</div><div class="line"><span class="keyword">const</span> multiplyByIndex = numbers.map(<span class="function">(<span class="params">a, i</span>) =></span> a * i);</div><div class="line"><span class="built_in">console</span>.log(multiplyByIndex);</div></pre></td></tr></table></figure>