-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.xml
1743 lines (1690 loc) · 227 KB
/
index.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" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Icy Gao's Blog</title>
<link>https://js0205.github.io/</link>
<description>Recent content on Icy Gao's Blog</description>
<generator>Hugo -- gohugo.io</generator>
<language>zh-cn</language>
<lastBuildDate>Thu, 08 Dec 2022 22:48:15 +0800</lastBuildDate><atom:link href="https://js0205.github.io/index.xml" rel="self" type="application/rss+xml" /><item>
<title>Mobx的使用和原则</title>
<link>https://js0205.github.io/p/mobx%E7%9A%84%E4%BD%BF%E7%94%A8%E5%92%8C%E5%8E%9F%E5%88%99/</link>
<pubDate>Thu, 08 Dec 2022 22:48:15 +0800</pubDate>
<guid>https://js0205.github.io/p/mobx%E7%9A%84%E4%BD%BF%E7%94%A8%E5%92%8C%E5%8E%9F%E5%88%99/</guid>
<description><h2 id="前言">前言</h2>
<p>项目中接触到了mobx,因此记录一下对mobx的使用和理解</p>
<h2 id="常用api">常用API</h2>
<h3 id="创建observable">创建observable</h3>
<p><strong>makeObservable</strong></p>
<p>用法:makeObservable(target, annotations?, options?)</p>
<p>属性、整个对象、数组、Maps和Sets都可以被转化成observale</p>
<h3 id="actions">Actions</h3>
<p>Action就是任何一段修改状态的代码</p>
<p><strong>runInAction</strong></p>
<p>用法:runInAction(fn)</p>
<p>创建一个被立即调用的一次性action</p>
<h3 id="与react的整合">与React的整合</h3>
<p>来自mobx-react或mobx-react-lite包</p>
<p><strong>observer</strong></p>
<p>用法:observer(component)</p>
<p>一个高阶组件,可用来在observerable发生改变时将一个React函数组件或类组件重新渲染</p>
<h2 id="原则">原则</h2>
<p>Mobx使用<strong>单向数据流</strong>,利用action改变state,进而更新所有受影响的view</p>
<p><img src="https://js0205.github.io/p/mobx%E7%9A%84%E4%BD%BF%E7%94%A8%E5%92%8C%E5%8E%9F%E5%88%99/image1.png"
width="2064"
height="225"
srcset="https://js0205.github.io/p/mobx%E7%9A%84%E4%BD%BF%E7%94%A8%E5%92%8C%E5%8E%9F%E5%88%99/image1_hu743ce7e2f42fb02b83fb420a26ab8dbe_417958_480x0_resize_box_3.png 480w, https://js0205.github.io/p/mobx%E7%9A%84%E4%BD%BF%E7%94%A8%E5%92%8C%E5%8E%9F%E5%88%99/image1_hu743ce7e2f42fb02b83fb420a26ab8dbe_417958_1024x0_resize_box_3.png 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="917"
data-flex-basis="2201px"
></p>
<ol>
<li>所有的derivations将在state改变时<strong>自动且原子化地更新</strong>。因此不可能观察中间值</li>
<li>所有的derivations默认将会<strong>同步</strong>更新,这意味着action可以在state改变之后安全的直接获得computed值</li>
<li>computed value的更新是<strong>惰性</strong>的,任何computed value在需要他们的副作用发生之前都是不激活的</li>
<li>所有的computed value都应是纯函数,他们不应该修改state</li>
</ol>
</description>
</item>
<item>
<title>Sentry</title>
<link>https://js0205.github.io/p/sentry/</link>
<pubDate>Wed, 07 Dec 2022 13:05:46 +0800</pubDate>
<guid>https://js0205.github.io/p/sentry/</guid>
<description><h2 id="前言">前言</h2>
<ol>
<li>目前问题:工程师在处理线上问题的时候基本来自于工单,工程师只是被动接收客户发现的问题</li>
<li>目前痛点:兼容性问题自测难覆盖、接口报错难复现、问题发现不及时、客户反馈太紧急</li>
</ol>
<h2 id="技术方案">技术方案</h2>
<ul>
<li>badjs</li>
<li>fundebug</li>
<li>Sentry</li>
</ul>
<h2 id="什么是sentry">什么是Sentry</h2>
<p>Sentry是一个开源的监控系统,可以收集项目中的异常信息,便于开发人员第一时间发现问题,定位问题,解决问题。</p>
<h2 id="sentry的特点">Sentry的特点</h2>
<ul>
<li>100%开源:源码开放,性能卓越,易于扩展</li>
<li>支持主流的编程语言</li>
<li>支持Sass版和私有化部署</li>
<li>支持多个第三方集成,如gitlab、github、jira、webhook</li>
</ul>
<h2 id="sentry的架构">Sentry的架构</h2>
<h2 id="前端应用如何接入sentry">前端应用如何接入Sentry</h2>
<h3 id="私有化部署">私有化部署</h3>
<h3 id="项目接入">项目接入</h3>
<h3 id="钉钉邮箱报警">钉钉/邮箱报警</h3>
<h2 id="数据埋点指标">数据埋点指标</h2>
<ul>
<li>CLS:累积布局偏移</li>
<li>FID:首次输入延迟</li>
<li>FCP:首次内容绘制</li>
<li>LCP:最大内容绘制</li>
<li>TTFB:首字节时间</li>
</ul>
<h2 id="参考网址">参考网址</h2>
<p>[1] <a class="link" href="https://sentry.io/welcome/" target="_blank" rel="noopener"
>Sentry官网</a></p>
<p>[2] <a class="link" href="https://docs.sentry.io/" target="_blank" rel="noopener"
>Sentry 官方文档</a></p>
<p>[3] <a class="link" href="https://github.com/getsentry/sentry" target="_blank" rel="noopener"
>Sentry github网址</a></p>
<p>[4] <a class="link" href="https://ludd.link/blog/monitor/sentry.html" target="_blank" rel="noopener"
>异常监控系统之 Sentry</a></p>
<p>[5] <a class="link" href="https://zhuanlan.zhihu.com/p/381930994" target="_blank" rel="noopener"
>神策数据王朋:如何搭建一套高可用的前端异常监控系统?</a></p>
<p>[6] <a class="link" href="https://juejin.cn/post/6954303116783124487" target="_blank" rel="noopener"
>【得物技术】前端项目使用Sentry错误监控实践</a></p>
<p>[7] <a class="link" href="https://cloud.tencent.com/developer/article/1887800" target="_blank" rel="noopener"
>Sentry Web 前端监控 - 最佳实践(官方教程)</a></p>
<p>[8] <a class="link" href="https://jishuin.proginn.com/p/763bfbd5e203" target="_blank" rel="noopener"
>SourceMap 与前端异常监控</a></p>
</description>
</item>
<item>
<title>Redux</title>
<link>https://js0205.github.io/p/redux/</link>
<pubDate>Sun, 04 Dec 2022 20:08:23 +0800</pubDate>
<guid>https://js0205.github.io/p/redux/</guid>
<description><h2 id="什么是redux">什么是Redux?</h2>
<p>Redux是一个JavaScript应用状态管理的库,当项目很复杂的时候,属性传递已经达不到我们预期,可以使用Redux解决数据传递问题,统一状态管理。换句话说,Redux就是用来处理和管理应用的状态/数据</p>
<p><img src="https://js0205.github.io/p/redux/image1.png"
width="600"
height="308"
srcset="https://js0205.github.io/p/redux/image1_hu9cb7130331358735bfd29c554eaa7e6f_108988_480x0_resize_box_3.png 480w, https://js0205.github.io/p/redux/image1_hu9cb7130331358735bfd29c554eaa7e6f_108988_1024x0_resize_box_3.png 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="194"
data-flex-basis="467px"
></p>
<p>redux是一个独立专门用于做状态管理的JS库</p>
<p><img src="https://js0205.github.io/p/redux/image2.png"
width="720"
height="407"
srcset="https://js0205.github.io/p/redux/image2_hu043fd32ca0ade1bfa5affa9c170f6d50_94283_480x0_resize_box_3.png 480w, https://js0205.github.io/p/redux/image2_hu043fd32ca0ade1bfa5affa9c170f6d50_94283_1024x0_resize_box_3.png 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="176"
data-flex-basis="424px"
></p>
<p>例子:如果要进行加一操作流程如下:</p>
<p><img src="https://js0205.github.io/p/redux/image3.png"
width="720"
height="405"
srcset="https://js0205.github.io/p/redux/image3_hu323411497491cc4192ca028331d14814_97178_480x0_resize_box_3.png 480w, https://js0205.github.io/p/redux/image3_hu323411497491cc4192ca028331d14814_97178_1024x0_resize_box_3.png 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="177"
data-flex-basis="426px"
></p>
<h2 id="核心概念">核心概念</h2>
<h3 id="store存储数据">Store(存储数据)</h3>
<p>在redux里面,只有一个Store,整个应用需要管理的数据都在这个Store里面。这个Store我们不能直接取改变,我们只能通过返回一个新的Store去更改它。redux提供了一个createStore来创建state</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">createStore</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;redux&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">store</span> <span class="o">=</span> <span class="nx">createStore</span><span class="p">(</span><span class="nx">reducer</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="action方法type名称payload参数">action(方法:type名称,payload参数)</h3>
<p>这个action指的是视图层发起的一个操作,告诉Store我们需要改变。比如用户点击了按钮,我们就要去请求列表,列表的数据就会变更。每个action必须有一个type属性,这表示action的名称,然后还可以有一个payload属性,这个属性可以带一些参数,用作Store变更:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">action</span> <span class="o">=</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;ADD_ITEM&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"> <span class="nx">payload</span><span class="o">:</span> <span class="s1">&#39;new item&#39;</span><span class="p">,</span><span class="c1">//可选属性
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="dispatch发出action">dispatch(发出action)</h3>
<p>在上面我们定义了一个Action,但是Action不会自己主动发出变更操作到Store,所以这里我们需要一个叫dispatch的东西,它专门用来发出action,不过还好,这个dispatch不需要我们自己定义和实现,redux已经帮我们写好了,在redux里面,**store.dispatch()**是View发出Action的唯一方法</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"> <span class="nx">store</span><span class="p">.</span><span class="nx">dispatch</span><span class="p">({</span>
</span></span><span class="line"><span class="cl"> <span class="nx">type</span><span class="o">:</span><span class="s1">&#39;ADD_ITEM&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"> <span class="nx">payload</span><span class="o">:</span><span class="s1">&#39;new item&#39;</span><span class="p">,</span> <span class="c1">//可选属性
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="p">})</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="reducer计算新的store的">Reducer(计算新的store的)</h3>
<p>当dispatch发起了一个action之后,会到达reducer,那么这个reducer用来干什么呢?顾名思义,这个<strong>reducer就是用来计算新的store的</strong>,reducer接收两个参数:<strong>当前的state</strong>和<strong>接收到的action</strong>,然后它经过计算,会返回一个新的state。(不能直接更改state,必须通过返回一个新的state来进行变更。)</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">reducer</span><span class="o">=</span><span class="kd">function</span><span class="p">(</span><span class="nx">prevState</span><span class="p">,</span><span class="nx">action</span><span class="p">){</span>
</span></span><span class="line"><span class="cl"> <span class="p">...</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">newState</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="state数据集合">State(数据集合)</h3>
<p>store某个节点对应的数据集合就是state。state是被托管的数据,也就是每次出发监听事件,我们要操作的数据。可以通过store.getState()获得:store.getState()即可获取到reducer中的state</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nx">store</span><span class="o">=</span><span class="nx">createStore</span><span class="p">(</span><span class="nx">fn</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nx">state</span><span class="o">=</span><span class="nx">store</span><span class="p">.</span><span class="nx">getState</span><span class="p">();</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="redux核心api">Redux核心API</h2>
<p>引入必要组件:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span><span class="nx">createStore</span><span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;redux&#39;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>生成store:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">store</span><span class="o">=</span><span class="nx">createStore</span><span class="p">(</span><span class="nx">reducer</span><span class="p">,</span><span class="nx">state初始状态</span><span class="p">[</span><span class="nx">可选</span><span class="p">]);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>取得当前时刻的state:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">state</span><span class="o">=</span><span class="nx">store</span><span class="p">.</span><span class="nx">getState</span><span class="p">();</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>发出action:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="nx">store</span><span class="p">.</span><span class="nx">dispatch</span><span class="p">({</span>
</span></span><span class="line"><span class="cl"> <span class="nx">type</span><span class="o">:</span><span class="s1">&#39;ADD_TODO&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"> <span class="nx">text</span><span class="o">:</span><span class="s1">&#39;Learn Redux&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"><span class="p">})</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>Reducer函数里面不能改变State,必须返回一个全新的对象,请参考下面的写法。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="c1">//State是一个对象
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">function</span> <span class="nx">reducer</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span> <span class="nx">action</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">assign</span><span class="p">({},</span> <span class="nx">state</span><span class="p">,</span> <span class="p">{</span> <span class="nx">thingToChange</span> <span class="p">});</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//或者
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="k">return</span> <span class="p">{</span> <span class="p">...</span><span class="nx">state</span><span class="p">,</span> <span class="p">...</span><span class="nx">newState</span> <span class="p">};</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="c1">//State是一个数组
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">function</span> <span class="nx">reducer</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span> <span class="nx">aciton</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">[...</span><span class="nx">state</span><span class="p">,</span> <span class="nx">newItem</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="redux的优势">Redux的优势</h2>
<p>没使用redux:</p>
<p><img src="https://js0205.github.io/p/redux/image4.gif"
width="400"
height="226"
srcset="https://js0205.github.io/p/redux/image4_hud4b311444370da490e495d0b199d49f9_48415_480x0_resize_box_1.gif 480w, https://js0205.github.io/p/redux/image4_hud4b311444370da490e495d0b199d49f9_48415_1024x0_resize_box_1.gif 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="176"
data-flex-basis="424px"
></p>
<p>使用redux:</p>
<p><img src="https://js0205.github.io/p/redux/image5.png"
width="400"
height="226"
srcset="https://js0205.github.io/p/redux/image5_hu8dc2034508f5e3f1678c16fa5ba43405_27714_480x0_resize_box_3.png 480w, https://js0205.github.io/p/redux/image5_hu8dc2034508f5e3f1678c16fa5ba43405_27714_1024x0_resize_box_3.png 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="176"
data-flex-basis="424px"
></p>
<h3 id="redux使状态可预测">Redux使状态可预测</h3>
<p>在Redux中,状态始终是可预测的。如果将相同的状态和动作传递给减速器,则总是会产生相同的结果,因为减速器是纯函数。状态也是不可变的,永远不会改变。这使得执行诸如无限撤销和重做之类的艰巨任务成为可能。还可以在之前的状态之间来回移动并实时查看结果</p>
<h3 id="redux是可维护的">Redux是可维护的</h3>
<p>Redux的写法比较固定,团队应用中风格比较稳定,提高协作中的可维护性</p>
<h3 id="redux调试简单">Redux调试简单</h3>
<p>在控制台中使用Redux Dev Tools</p>
<h3 id="性能优势">性能优势</h3>
<p>我们可能会假设保持应用程序的状态全局会导致一些性能下降。在很大程度上,情况并非如此,因为React Redux在内部实现了许多性能优化,因此我们自己的连接组件仅在实际需要时才重新渲染</p>
<h3 id="易于测试">易于测试</h3>
<p>由于函数用于更改纯函数的状态,因此测试Redux应用程序很容易</p>
<h3 id="状态持久化">状态持久化</h3>
<p>我们可以将应用程序的一些状态持久化到本地存储并在刷新后恢复它。</p>
<h3 id="服务端渲染">服务端渲染</h3>
<p>Redux也可以用于服务端渲染。有了它,我们可以通过将应用程序的状态连同它对服务器请求的响应发送到服务器来处理应用程序的初始呈现。然后所需的组件以HTML格式呈现并发送到客户端</p>
<h2 id="实战项目">实战项目</h2>
<p>安装redux</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="nx">npm</span> <span class="nx">install</span> <span class="nx">redux</span> <span class="o">--</span><span class="nx">save</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="redux的简单使用">Redux的简单使用</h3>
<p>(1)新建store</p>
<p>(2)获取状态:</p>
<p>读取:store.getState()即可获取到reducer中的state</p>
<p>(3)派发事件:</p>
<p>修改: store.dispatch(action)通过dispatch一个action来出发reducer中的修改行为</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">createStore</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;redux&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="c1">//1.新建store
</span></span></span><span class="line"><span class="cl"><span class="c1">//通过reducer建立
</span></span></span><span class="line"><span class="cl"><span class="c1">//根据老的state和action生成新的state
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">function</span> <span class="nx">counter</span><span class="p">(</span><span class="nx">state</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">action</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">switch</span> <span class="p">(</span><span class="nx">action</span><span class="p">,</span> <span class="nx">type</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">case</span> <span class="s1">&#39;addGun&#39;</span><span class="o">:</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">state</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">case</span> <span class="s1">&#39;removeGun&#39;</span><span class="o">:</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">state</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">default</span><span class="o">:</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="mi">10</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="c1">//1. 新建store
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">store</span> <span class="o">=</span> <span class="nx">createStore</span><span class="p">(</span><span class="nx">counter</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">//2.获取状态
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">init</span> <span class="o">=</span> <span class="nx">store</span><span class="p">.</span><span class="nx">getState</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">listener</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">current</span> <span class="o">=</span> <span class="nx">store</span><span class="p">.</span><span class="nx">getState</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`现在的个数</span><span class="si">${</span><span class="nx">current</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="c1">//每次派发事件之后,都会调用,进而获取状态
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">store</span><span class="p">.</span><span class="nx">subscribe</span><span class="p">(</span><span class="nx">listener</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">//3.派发事件
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">store</span><span class="p">.</span><span class="nx">dispatch</span><span class="p">({</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;addGun&#39;</span> <span class="p">});</span>
</span></span><span class="line"><span class="cl"><span class="nx">store</span><span class="p">.</span><span class="nx">dispatch</span><span class="p">({</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;addGun&#39;</span> <span class="p">});</span>
</span></span><span class="line"><span class="cl"><span class="nx">store</span><span class="p">.</span><span class="nx">dispatch</span><span class="p">({</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;removeGun&#39;</span> <span class="p">});</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>输出:现在的个数11现在的个数12现在的个数11</p>
<h3 id="react与redux组合使用">react与redux组合使用</h3>
<p>安装redux-thunk,解决异步问题</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="nx">npm</span> <span class="nx">install</span> <span class="nx">redux</span><span class="o">-</span><span class="nx">thunk</span> <span class="o">--</span><span class="nx">save</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>(1)把store.dispatch方法传递给组件,内部可以调用修改状态</p>
<p>(2)subscribe订阅render函数,每次修改都重新渲染</p>
<p>(3)redux相关的内容,移动到单独的文件index.redux.js单独管理</p>
<p>(4)npm install redux-thunk &ndash;save解决异步请求的问题</p>
<p>(5)使用apply/Middleware开启thunk中间件</p>
<p><strong>步骤一</strong>:在src下新建App.js、index.redux.js,在index.js中负责页面结构的组建、store的建立</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">import</span> <span class="nx">React</span> <span class="nx">from</span> <span class="s1">&#39;react&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">import</span> <span class="nx">ReactDom</span> <span class="nx">from</span> <span class="s1">&#39;react-dom&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">import</span> <span class="nx">App</span> <span class="nx">from</span> <span class="s1">&#39;./App&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">createStore</span><span class="p">,</span> <span class="nx">applyMiddleware</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;redux&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">import</span> <span class="nx">thunk</span> <span class="nx">from</span> <span class="s1">&#39;redux-thunk&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">counter</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;./index.redux&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">addGun</span><span class="p">,</span> <span class="nx">removeGun</span><span class="p">,</span> <span class="nx">addGunAsync</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;./index.redux&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="c1">//applyMiddleware可以使用异步操作
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">store</span> <span class="o">=</span> <span class="nx">createStore</span><span class="p">(</span><span class="nx">counter</span><span class="p">,</span> <span class="nx">applyMiddleware</span><span class="p">(</span><span class="nx">thunk</span><span class="p">));</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">render</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">ReactDom</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span> <span class="o">&lt;</span><span class="nx">App</span> <span class="nx">store</span> <span class="o">=</span> <span class="p">{</span><span class="nx">store</span><span class="p">}</span> <span class="nx">addGun</span> <span class="o">=</span> <span class="p">{</span><span class="nx">addGun</span><span class="p">}</span> <span class="nx">removeGun</span> <span class="o">=</span> <span class="p">{</span><span class="nx">removeGun</span><span class="p">}</span> <span class="nx">addGunAsync</span> <span class="o">=</span> <span class="p">{</span><span class="nx">addGunAsync</span><span class="p">}</span><span class="o">/&gt;</span><span class="p">,</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;root&#39;</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="nx">render</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="c1">//监听状态的变化,每次重新渲染组件
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">store</span><span class="p">.</span><span class="nx">subscribe</span><span class="p">(</span><span class="nx">render</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>步骤二:在App.js中完成组件内容的编写、页面的渲染以及相关事件的派发</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">import</span> <span class="nx">React</span> <span class="nx">from</span> <span class="s1">&#39;react&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">class</span> <span class="nx">App</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">component</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//constructor(props){
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="c1">//super(props)
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="c1">//}
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="nx">render</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//接收从父组件index.js中传过来的store
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="kr">const</span> <span class="nx">store</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">store</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//获取机枪数量状态的变化
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="kr">const</span> <span class="nx">num</span> <span class="o">=</span> <span class="nx">store</span><span class="p">.</span><span class="nx">getState</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//通过组件传递action,降低耦合度
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="kr">const</span> <span class="nx">addGun</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">addGun</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">removeGun</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">removeGun</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">addGunAsync</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">props</span><span class="p">.</span><span class="nx">addGunAsync</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl"> <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="o">&lt;</span><span class="nx">h2</span><span class="o">&gt;</span> <span class="nx">现在有机枪</span> <span class="p">{</span> <span class="nx">num</span> <span class="p">}</span> <span class="o">&lt;</span><span class="err">/h2&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">{</span><span class="cm">/* 当点击按钮时执行事件的派发,即调用action */</span><span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{()=&gt;</span><span class="nx">store</span><span class="p">.</span><span class="nx">dispatch</span><span class="p">(</span><span class="nx">addGun</span><span class="p">())}</span><span class="o">&gt;</span><span class="nx">申请武器</span><span class="o">&lt;</span><span class="err">/button&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{()=&gt;</span><span class="nx">store</span><span class="p">.</span><span class="nx">dispatch</span><span class="p">(</span><span class="nx">removeGun</span><span class="p">())}</span><span class="o">&gt;</span><span class="nx">回收武器</span><span class="o">&lt;</span><span class="err">/button&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="o">&lt;</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{()=&gt;</span><span class="nx">store</span><span class="p">.</span><span class="nx">dispatch</span><span class="p">(</span><span class="nx">addGunAsync</span><span class="p">())}</span><span class="o">&gt;</span><span class="nx">延迟回收武器</span><span class="o">&lt;</span><span class="err">/button&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="o">&lt;</span><span class="err">/div &gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kr">export</span> <span class="k">default</span> <span class="nx">App</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>步骤三:index.redux.js中负责的store中对状态变化的处理、action事件的编写</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">ADD_GUN</span> <span class="o">=</span> <span class="s1">&#39;addGun&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">REMOVE_GUN</span> <span class="o">=</span> <span class="s1">&#39;removeGun&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">//store
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">export</span> <span class="kd">function</span> <span class="nx">counter</span><span class="p">(</span><span class="nx">state</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">action</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//当action被调用执行时,会到此处进行判断,执行状态的变化
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="k">switch</span> <span class="p">(</span><span class="nx">action</span><span class="p">.</span><span class="nx">type</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">case</span> <span class="s1">&#39;addGun&#39;</span><span class="o">:</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">state</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">case</span> <span class="s1">&#39;removeGun&#39;</span><span class="o">:</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">state</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">default</span><span class="o">:</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="mi">10</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="c1">//action
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">export</span> <span class="kd">function</span> <span class="nx">addGun</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">{</span> <span class="nx">type</span><span class="o">:</span> <span class="nx">ADD_GUN</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kr">export</span> <span class="kd">function</span> <span class="nx">removeGun</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">{</span> <span class="nx">type</span><span class="o">:</span> <span class="nx">REMOVE_GUN</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kr">export</span> <span class="kd">function</span> <span class="nx">addGunAsync</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">dispatch</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">setTimeout</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">dispatch</span><span class="p">(</span><span class="nx">addGun</span><span class="p">())</span>
</span></span><span class="line"><span class="cl"> <span class="p">},</span> <span class="mi">2000</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="redux使用的解耦react与redux自动连接">Redux使用的解耦(react与redux自动连接)</h3>
<p>redux-thunk</p>
<p>(1)基本介绍</p>
<ul>
<li>redux-thunk改写了dispatch API,使其具备<strong>接受一个函数作为参数</strong>的能力,从而达到middleware的效果,即在redux的dispatch action=&gt;reducer=&gt;store这个流程中,在<strong>action被发起之后,到达reducer之前</strong>的扩展点,加入相关操作,比如发生请求、log信息等</li>
<li>提供Redux的异步解决方案,弥补Redux功能的不足</li>
</ul>
<p><img src="https://js0205.github.io/p/redux/image6.png"
width="720"
height="407"
srcset="https://js0205.github.io/p/redux/image6_huda4f98dfb58100d19332aa95b8c3b234_107144_480x0_resize_box_3.png 480w, https://js0205.github.io/p/redux/image6_huda4f98dfb58100d19332aa95b8c3b234_107144_1024x0_resize_box_3.png 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="176"
data-flex-basis="424px"
></p>
</description>
</item>
<item>
<title>事件循环</title>
<link>https://js0205.github.io/p/%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF/</link>
<pubDate>Wed, 30 Nov 2022 23:28:05 +0800</pubDate>
<guid>https://js0205.github.io/p/%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF/</guid>
<description><h2 id="js是单线程-浏览器是多线程的">JS是单线程 浏览器是多线程的</h2>
<p>JavaScript的主要用途是与用户互动,以及操作DOM。比如,假定JavaScript同时有两个线程,一个线程在某个DOM节点上添加内容,另一个线程删除了这个节点,这时浏览器应该以哪个线程为准?</p>
<h2 id="宏任务和微任务的概念是什么">宏任务和微任务的概念是什么</h2>
<p><strong>同步</strong>:会立即执行的任务,直接进入到主线程中执行。(主执行栈:后进先出)</p>
<p><strong>异步</strong>:(先同步后异步)不会立即执行的任务(宏任务与微任务)(任务队列:先进先出)</p>
<p><strong>宏任务</strong>(marcotask):是指消息队列中的等待被主线程执行的事件,宏任务执行时都会重新创建栈,然后调用宏任务中的函数,栈也会随着变化,但宏任务执行结束时,栈也会随之销毁。<strong>类型:script、setTimeout、setInterval、setImmediate、I/O、UI rendering</strong></p>
<p><strong>微任务</strong>(microtask):可以把微任务看成是一个需要异步执行的函数,执行时机是在主函数执行结束之后,当前宏任务结束之前。<strong>类型:原生Promise.then()、process.nextTick、MutationObserver</strong></p>
<p>注:</p>
<ul>
<li>执行栈-&gt;微任务队列(清空)-&gt;一个宏任务队列入执行栈-&gt;微任务队列(清空)-&gt;一个宏任务队列入执行栈-&gt;微任务队列(清空)-&gt;&hellip;&hellip;</li>
<li>宏任务setTimeout setInterval setImmedate<strong>也有先后顺序,先执行setTimeout,后执行setImmediate</strong></li>
<li>微任务promise.then() process.nextTick()<strong>有先后顺序,promise.then()后执行</strong></li>
</ul>
<h2 id="事件循环event-loop">事件循环(Event Loop)</h2>
<p>(1)所有的<strong>同步任务</strong>都在主线程上执行,形成一个<strong>执行栈</strong></p>
<p>(2)除了主线程之外,还存在一个<strong>任务队列</strong>,只要异步任务有了运行结果,就在任务队列中植入一个时间标记</p>
<p>(3)主线程完成所有任务(<strong>执行栈清空</strong>),就会读取<strong>任务队列</strong>、Event Loop把微任务队列执行清空</p>
<p>(4)<strong>微任务队列</strong>清空后,进入<strong>宏任务队列</strong>的第一项任务放入Stack(栈)中执行,执行完成后,查看微任务队列是否有任务,有的话,清空<strong>微任务队列</strong>。重复4,继续从宏任务中取任务执行,执行完成之后,继续清空微任务,如此反复循环,直至清空所有的任务</p>
<p><img src="https://js0205.github.io/p/%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF/image1.png"
width="741"
height="385"
srcset="https://js0205.github.io/p/%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF/image1_hu26f686ca456c3c4c8f6f8edebad8f8fe_151598_480x0_resize_box_3.png 480w, https://js0205.github.io/p/%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF/image1_hu26f686ca456c3c4c8f6f8edebad8f8fe_151598_1024x0_resize_box_3.png 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="192"
data-flex-basis="461px"
></p>
<p><img src="https://js0205.github.io/p/%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF/image2.png"
width="793"
height="515"
srcset="https://js0205.github.io/p/%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF/image2_hu14e341aec3250ffc3114217b685cd519_286624_480x0_resize_box_3.png 480w, https://js0205.github.io/p/%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF/image2_hu14e341aec3250ffc3114217b685cd519_286624_1024x0_resize_box_3.png 1024w"
loading="lazy"
class="gallery-image"
data-flex-grow="153"
data-flex-basis="369px"
></p>
</description>
</item>
<item>
<title>数组扁平化</title>
<link>https://js0205.github.io/p/%E6%95%B0%E7%BB%84%E6%89%81%E5%B9%B3%E5%8C%96/</link>
<pubDate>Wed, 30 Nov 2022 22:04:48 +0800</pubDate>
<guid>https://js0205.github.io/p/%E6%95%B0%E7%BB%84%E6%89%81%E5%B9%B3%E5%8C%96/</guid>
<description><p><strong>数组扁平化就是将多维数组转为一维数组</strong></p>
<h2 id="方式一es6的flat方法">方式一:ES6的flat方法</h2>
<p>数组自带的扁平化方法,flat的参数代表的是需要展开几层,如果是Infinity的话,就是不管嵌套几层,全部都展开</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]]],</span> <span class="mi">6</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">arr</span><span class="p">.</span><span class="nx">flat</span><span class="p">(</span><span class="kc">Infinity</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="c1">//输出结果:[1,2,3,4,5,6]
</span></span></span></code></pre></td></tr></table>
</div>
</div><h2 id="方法二使用正则">方法二:使用正则</h2>
<p>(1)首先是使用JSON.stringify把arr转为字符串</p>
<p>(2)接着使用正则把字符串里面的[和]去掉</p>
<p>(3)然后再拼接数组括号转为数组对象</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]]],</span> <span class="mi">6</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">res</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">arr</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\[|\]/g</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">res2</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="s1">&#39;[&#39;</span> <span class="o">+</span> <span class="nx">res</span> <span class="o">+</span> <span class="s1">&#39;]&#39;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">res2</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">//输出结果:[1,2,3,4,5,6]
</span></span></span></code></pre></td></tr></table>
</div>
</div><h2 id="方法三使用递归">方法三:使用递归</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">array</span> <span class="o">=</span> <span class="p">[];</span>
</span></span><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">fn</span> <span class="o">=</span> <span class="p">(</span><span class="nx">arr</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">arr</span><span class="p">[</span><span class="nx">i</span><span class="p">]))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">fn</span><span class="p">(</span><span class="nx">arr</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">array</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">arr</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="nx">fn</span><span class="p">(</span><span class="nx">arr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">array</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="方法四使用reduce">方法四:使用reduce</h2>
<p>reduce方法:可以用来给数组求和</p>
<p>concat()方法:用于连接两个或多个数组</p>
<p>concat()方法不会更改现有数组,而是返回一个新数组,其中包含已连接数组的值</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">newArr</span> <span class="o">=</span> <span class="p">(</span><span class="nx">arr</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">reduce</span><span class="p">((</span><span class="nx">pre</span><span class="p">,</span> <span class="nx">cur</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">pre</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">cur</span><span class="p">)</span> <span class="o">?</span> <span class="nx">newArr</span><span class="p">(</span><span class="nx">cur</span><span class="p">)</span> <span class="o">:</span> <span class="nx">cur</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">},</span> <span class="p">[])</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">newArr</span><span class="p">(</span><span class="nx">arr</span><span class="p">),</span> <span class="s1">&#39;reduce方法&#39;</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="方法五使用栈的思想实现flat函数">方法五:使用栈的思想实现flat函数</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">arr</span> <span class="o">=</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="mi">4</span><span class="p">,</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 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 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="mi">5</span><span class="p">,</span> <span class="s1">&#39;string&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="nx">name</span><span class="o">:</span> <span class="s1">&#39;前端收割机&#39;</span> <span class="p">}];</span>
</span></span><span class="line"><span class="cl"><span class="c1">//栈思想
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">function</span> <span class="nx">myflat</span><span class="p">(</span><span class="nx">arr</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">res</span> <span class="o">=</span> <span class="p">[];</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">stack</span> <span class="o">=</span> <span class="p">[].</span><span class="nx">concat</span><span class="p">(</span><span class="nx">arr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="k">while</span> <span class="p">(</span><span class="nx">stack</span><span class="p">.</span><span class="nx">length</span> <span class="o">!==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">val</span> <span class="o">=</span> <span class="nx">stack</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">val</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">stack</span><span class="p">.</span><span class="nx">push</span><span class="p">(...</span><span class="nx">val</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">res</span><span class="p">.</span><span class="nx">unshift</span><span class="p">(</span><span class="nx">val</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">res</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">newrr</span> <span class="o">=</span> <span class="nx">myflat</span><span class="p">(</span><span class="nx">arr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">newarr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 输出结果:[1,2,3,4,1,2,3,1,2,3,1,2,3,5,&#39;string&#39;, {name:&#39;前端收割机&#39; }]
</span></span></span></code></pre></td></tr></table>
</div>
</div></description>
</item>
<item>
<title>call bind apply三者的异同</title>
<link>https://js0205.github.io/p/call-bind-apply%E4%B8%89%E8%80%85%E7%9A%84%E5%BC%82%E5%90%8C/</link>
<pubDate>Wed, 30 Nov 2022 22:04:27 +0800</pubDate>
<guid>https://js0205.github.io/p/call-bind-apply%E4%B8%89%E8%80%85%E7%9A%84%E5%BC%82%E5%90%8C/</guid>
<description><h2 id="callapplybind的相同点">call,apply,bind的相同点</h2>
<p>(1)都是改变this指向的</p>
<p>(2)第一个参数都是this要指向的对象</p>
<p>(3)都可以利用<strong>后续参数</strong>传参</p>
<h2 id="callapplybind的区别">call,apply,bind的区别</h2>
<p>(1)call和bind的参数是<strong>依次传参</strong>,一一对应的</p>
<p>(2)但apply只有两个参数,第二个参数为<strong>数组</strong></p>
<p>(3)call和apply都是对函数进行直接调用,而bind方法返回的仍是一个函数</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="c1">//call可以调用函数,call可以改变函数中this指向
</span></span></span><span class="line"><span class="cl"><span class="c1">//dog.eat.call(cat,&#39;鱼&#39;,&#39;肉&#39;);
</span></span></span><span class="line"><span class="cl"><span class="c1">//dog.eat.apply(cat,[&#39;鱼&#39;,&#39;肉&#39;]);
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">let</span> <span class="nx">fun</span> <span class="o">=</span> <span class="nx">dog</span><span class="p">.</span><span class="nx">eat</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="nx">cat</span><span class="p">,</span> <span class="s1">&#39;鱼&#39;</span><span class="p">,</span> <span class="s1">&#39;肉&#39;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="nx">fun</span><span class="p">();</span>
</span></span></code></pre></td></tr></table>
</div>
</div></description>
</item>
<item>
<title>get和post的区别</title>
<link>https://js0205.github.io/p/get%E5%92%8Cpost%E7%9A%84%E5%8C%BA%E5%88%AB/</link>
<pubDate>Wed, 30 Nov 2022 22:04:02 +0800</pubDate>
<guid>https://js0205.github.io/p/get%E5%92%8Cpost%E7%9A%84%E5%8C%BA%E5%88%AB/</guid>
<description><p>(1)GET:发送一个请求来<strong>取得</strong>服务器上的某一资源 <strong>检索-&gt;获取</strong></p>
<p>POST:向URL指定的资源<strong>提交数据</strong>或<strong>附加新的数据</strong> <strong>创建-&gt;更新</strong></p>
<p>post用于修改和写入数据,get一般用于搜索排序和筛选之类的操作(淘宝、支付宝的搜索查询都是get提交),目的是资源的获取,读取数据</p>
<p>(2)存放的位置不同</p>
<p>GET:参数包含在URL中。以?开始以param=value&amp;param2=value2的形式附加在URI字段之后</p>
<p>POST:参数存放在请求数据包的消息体中。request body,post更安全</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">import</span> <span class="nx">request</span> <span class="nx">from</span> <span class="s1">&#39;./request&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="c1">//注册
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">export</span> <span class="kr">const</span> <span class="nx">RegisterApi</span> <span class="o">=</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">request</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">&#39;/register&#39;</span><span class="p">,</span> <span class="nx">params</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">//登录
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">export</span> <span class="kr">const</span> <span class="nx">LoginApi</span> <span class="o">=</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">request</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">&#39;/login&#39;</span><span class="p">,</span> <span class="nx">params</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">//获取文章列表
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">export</span> <span class="kr">const</span> <span class="nx">ArticleApi</span> <span class="o">=</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">request</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/article&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="nx">params</span> <span class="p">});</span>
</span></span><span class="line"><span class="cl"><span class="c1">//添加文章
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">export</span> <span class="kr">const</span> <span class="nx">ArticleAddApi</span> <span class="o">=</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">request</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">&#39;/article/add&#39;</span><span class="p">,</span> <span class="nx">params</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">//查看文章
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">export</span> <span class="kr">const</span> <span class="nx">ArticleSearchApi</span> <span class="o">=</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">request</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="sb">`/article/</span><span class="si">${</span><span class="nx">params</span><span class="p">.</span><span class="nx">id</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div></description>
</item>
<item>
<title>Ajax</title>
<link>https://js0205.github.io/p/ajax/</link>
<pubDate>Wed, 30 Nov 2022 20:06:33 +0800</pubDate>
<guid>https://js0205.github.io/p/ajax/</guid>
<description><h2 id="ajax的作用">Ajax的作用</h2>
<p>(1)不刷新页面更新网页</p>
<p>(2)在页面加载后从服务器请求数据</p>
<p>(3)在页面加载后从服务器接收数据</p>
<p>(4)在后台向服务器发送数据</p>
<h2 id="ajax工作原理xmlhttprequest对象">Ajax工作原理——XMLHttpRequest对象</h2>
<p>(1)Ajax不是新的编程语言,而是一门提供网页局部刷新的技术。</p>
<p>Ajax最大的优点是在<strong>不重新加载整个页面</strong>的情况下,<strong>与服务器交换数据</strong>并<strong>更新部分网页</strong>内容</p>
<p><img src="https://js0205.github.io/p/ajax/image1.png"
width="765"
height="415"
srcset="https://js0205.github.io/p/ajax/image1_hu5b1975de207732fb6c6772e210ccdac0_200026_480x0_resize_box_3.png 480w, https://js0205.github.io/p/ajax/image1_hu5b1975de207732fb6c6772e210ccdac0_200026_1024x0_resize_box_3.png 1024w"
loading="lazy"
alt="流程图"
class="gallery-image"
data-flex-grow="184"
data-flex-basis="442px"
></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="c1">//封装一个ajax请求
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">function</span> <span class="nx">ajax</span><span class="p">(</span><span class="nx">option</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//创建XMLHttpRequest对象
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="kr">const</span> <span class="nx">xhr</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">XMLHttpRequest</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//初始化参数的内容
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{};</span>
</span></span><span class="line"><span class="cl"> <span class="nx">options</span><span class="p">.</span><span class="nx">type</span> <span class="o">=</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">type</span> <span class="o">||</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">toUpperCase</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"> <span class="nx">options</span><span class="p">.</span><span class="nx">dataType</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">dataType</span> <span class="o">||</span> <span class="s1">&#39;json&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">params</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">data</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//发送请求
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">type</span> <span class="o">===</span> <span class="s1">&#39;GET&#39;</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">xhr</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">url</span> <span class="o">+</span> <span class="s1">&#39;?&#39;</span> <span class="o">+</span> <span class="nx">params</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="nx">xhr</span><span class="p">.</span><span class="nx">send</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">type</span> <span class="o">===</span> <span class="s1">&#39;POST&#39;</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">xhr</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="s1">&#39;POST&#39;</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">url</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="nx">xhr</span><span class="p">.</span><span class="nx">send</span><span class="p">(</span><span class="nx">params</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="c1">//接收请求
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="nx">xhr</span><span class="p">.</span><span class="nx">onreadystatechange</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="nx">xhr</span><span class="p">.</span><span class="nx">readyState</span> <span class="o">===</span> <span class="mi">4</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kd">let</span> <span class="nx">status</span> <span class="o">=</span> <span class="nx">xhr</span><span class="p">.</span><span class="nx">status</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="nx">status</span> <span class="o">&gt;=</span> <span class="mi">200</span> <span class="o">&amp;&amp;</span> <span class="nx">status</span> <span class="o">&lt;</span> <span class="mi">300</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">options</span><span class="p">.</span><span class="nx">success</span> <span class="o">&amp;&amp;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">success</span><span class="p">(</span><span class="nx">xhr</span><span class="p">.</span><span class="nx">responseText</span><span class="p">,</span> <span class="nx">xhr</span><span class="p">.</span><span class="nx">responseXML</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">options</span><span class="p">.</span><span class="nx">fail</span> <span class="o">&amp;&amp;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">fail</span><span class="p">(</span><span class="nx">status</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>(2)XMLHttpRequest对象在调用send()前需要调用哪个方法?答案是:open()</p>
<ul>
<li>创建Ajax对象</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">xhr</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">XMLHttpRequest</span><span class="p">();</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>xhr发送请求</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="nx">xhr</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="s1">&#39;get&#39;</span><span class="p">,</span> <span class="s1">&#39;test.html&#39;</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span><span class="c1">//与服务器建立连接