减少前端代码耦合
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><img src="http://mmbiz.qpic.cn/mmbiz_gif/bhuLuOhAhORq6t3ak7jaaWKBU4a0ricPicEHEfMh1K1TQQcAL6509Vt2MfsIbXHd46ia0rKk6b5keCNISboI9CNBw/0?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></span></strong></span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">什么是</span><span style="color: black;"><strong style="color: blue;">代码耦合</strong></span><span style="color: black;">?代码耦合的表现是改了一点毛发而牵动了全身,<span style="color: black;">或</span>是想要改点东西,<span style="color: black;">必须</span>在一堆代码里面找半天。<span style="color: black;">因为</span>前端<span style="color: black;">必须</span>组织js/css/html,耦合的问题可能会更加<span style="color: black;">显著</span>,下面<span style="color: black;">根据</span>耦合的<span style="color: black;">状况</span>分别说明:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">1. 避免全局耦合</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这应该是比较<span style="color: black;">平常</span>的耦合。全局耦合<span style="color: black;">便是</span>几个类、模块共用了全局变量<span style="color: black;">或</span>全局数据结构,<span style="color: black;">尤其</span>是一个变量跨了几个文件。例如下面,在html里面定义了一个变量:</span></p><span style="color: black;">
<span style="color: black;">var</span> PAGE = <span style="color: black;">20</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;"></<span style="color: black;">script</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">></p><<span style="color: black;">script</span> <span style="color: black;">src</span>=<span style="color: black;">"main.js"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">></p></<span style="color: black;">script</span>>
</span>
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面在head标签里面定义了一个PAGE的全局变量,<span style="color: black;">而后</span>在main.js里面<span style="color: black;">运用</span>。<span style="color: black;">这般</span>子PAGE<span style="color: black;">便是</span>一个全局变量,并且跨了两个文件,一个html,一个js。<span style="color: black;">而后</span>在main.js里面<span style="color: black;">忽然</span>冒出来了个PAGE的变量,后续<span style="color: black;">守护</span>这个代码的人看到这个变量到处找不到它的定义,最后找了半天<span style="color: black;">发掘</span>原来是在xxx.html的head标签里面定义了。<span style="color: black;">这般</span>就有点egg pain了,并且<span style="color: black;">这般</span>的变量容易和本地的变量<span style="color: black;">出现</span>命名冲突。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此</span><span style="color: black;">倘若</span><span style="color: black;">必须</span>把数据写在页面上的话,一个改进的办法是在页面写一个form,数据写成form里面的控件数据,如下:</span></p><span style="color: black;"><form id=<span style="color: black;">"page-data"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">></p> <span style="color: black;"><<span style="color: black;">input</span> <span style="color: black;">type</span>=<span style="color: black;">"hidden"</span> <span style="color: black;">name</span>=<span style="color: black;">"page"</span> <span style="color: black;">value</span>=<span style="color: black;">"2"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">></p> <<span style="color: black;">textarea</span> <span style="color: black;">name</span>=<span style="color: black;">"list"</span> <span style="color: black;">style</span>=<span style="color: black;">"display:none"</span>>[{"userName": ""yin"},{}]</<span style="color: black;">textarea</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">></p></<span style="color: black;">form</span>>
</span>
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面<span style="color: black;">运用</span>了input和textarea,<span style="color: black;">运用</span>textarea的优点是支持特殊符号。再把form的数据序列化,序列化<span style="color: black;">亦</span>是比较简单的,<span style="color: black;">能够</span>查看<strong style="color: blue;">Effective前端2:优化html标签</strong>(</span><span style="color: black;">http://yincheng.site/make-the-best-of-html-label</span><span style="color: black;">)</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第二种是全局数据结构,这种可能会<span style="color: black;">运用</span>模块化的<span style="color: black;">办法</span>,如下:</span></p><span style="color: black;"><span style="color: black;">//data.js</span><span style="color: black;">module</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.exports = {</p> houseList: <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//search.js 获取houseList的数据</span><span style="color: black;">var</span> data = <span style="color: black;">require</span>(<span style="color: black;">"data"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">data.houseList = ajax();</p><span style="color: black;">require</span>(<span style="color: black;">"format-data"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">).format();</p><span style="color: black;">//format-data.js 对houseList的数据做格式化</span><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">format</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">var</span> data = <span style="color: black;">require</span>(<span style="color: black;">"data"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> process(data);</p> <span style="color: black;">require</span>(<span style="color: black;">"show-result"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">).show();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//show-result.js 将数据<span style="color: black;">表示</span>出来</span><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">show</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> showData(<span style="color: black;">require</span>(<span style="color: black;">"data"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">).houseList)</p>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面四个模块各司其职,乍一眼看上去<span style="color: black;">好似</span>没什么问题,<span style="color: black;">然则</span><span style="color: black;">她们</span>都用了一个data的模块共用数据。<span style="color: black;">这般</span>确实很方便,<span style="color: black;">然则</span><span style="color: black;">这般</span>就全局耦合了。<span style="color: black;">由于</span>用的同一个data,<span style="color: black;">因此</span>你<span style="color: black;">没</span>法<span style="color: black;">保准</span>,其它人<span style="color: black;">亦</span>会加载了这个模块<span style="color: black;">而后</span>做了些修改,<span style="color: black;">或</span>是在你的某一个业务的异步回调<span style="color: black;">亦</span>改了这个。第二个问题:你不<span style="color: black;">晓得</span>这个data是从哪里来的,谁可能会对它做了修改,这个过程<span style="color: black;">针对</span>后续的模块<span style="color: black;">来讲</span>都是不透明的。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此</span>这种应该<span style="color: black;">思虑</span><span style="color: black;">运用</span>传参的方式,降低耦合度,把data<span style="color: black;">做为</span>一个参数传递:</span></p><span style="color: black;"><span style="color: black;">//去掉data.js</span><span style="color: black;">//search.js 获取数据并传递给下一个模块</span><span style="color: black;">var</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> houseList = ajax();</p><span style="color: black;">require</span>(<span style="color: black;">"format-data"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">).format(houseList);</p><span style="color: black;">//format-data.js 对houseList的数据做格式化</span><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">format</span>(<span style="color: black;">houseList</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> process(houseList);</p> <span style="color: black;">require</span>(<span style="color: black;">"show-result"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">).show(houseList);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//show-result.js 将数据<span style="color: black;">表示</span>出来</span><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">show</span>(<span style="color: black;">houseList</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">showData(houseList)</p>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>看到,search里面获取到data后,交给format-data处理,format-data处理完之后再给show-result。<span style="color: black;">这般</span>子就很清楚地<span style="color: black;">晓得</span>数据的处理流程,并且<span style="color: black;">保准</span>了houseList不会被某个异步回调不小心改了。<span style="color: black;">倘若</span>单独从某个模块<span style="color: black;">来讲</span>,show-result这个模块并不需要关心houseList的经过了<span style="color: black;">那些</span>流程和处理,它只<span style="color: black;">必须</span>关心输入是符合它的格式<span style="color: black;">需求</span>的就<span style="color: black;">能够</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这个时候你可能会有一个问题:这个data被逐层传递了这么多次,还不如像最上面的那样写一个data的模块,<span style="color: black;">大众</span>都去改那里,岂不是简单了<span style="color: black;">非常多</span>?对,<span style="color: black;">这般</span>是简单了,<span style="color: black;">然则</span>一个数据结构被跨了几个文件<span style="color: black;">运用</span>,<span style="color: black;">这般</span>会<span style="color: black;">显现</span>我上面说的问题。有时候可能<span style="color: black;">显现</span><span style="color: black;">有些</span>意想不到的<span style="color: black;">状况</span>,到时候可能得找bug找个半天。<span style="color: black;">因此</span>这种解耦是值得的,除非你定义的变量并不会跨文件,它的<span style="color: black;">功效</span>域<span style="color: black;">仅在</span>它所在的文件,<span style="color: black;">这般</span>会好<span style="color: black;">非常多</span>。<span style="color: black;">或</span>是data是常量的,data里面的数据定义好之后值就再<span style="color: black;">亦</span>不会改变,<span style="color: black;">这般</span>应当<span style="color: black;">亦</span>是可取的。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">2. js/css/html的耦合</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这种耦合在前端里面应该最<span style="color: black;">平常</span>,<span style="color: black;">由于</span>这三者<span style="color: black;">一般</span><span style="color: black;">拥有</span>交集,<span style="color: black;">必须</span><span style="color: black;">运用</span>js<span style="color: black;">掌控</span>样式和html结构。<span style="color: black;">倘若</span><span style="color: black;">运用</span>js<span style="color: black;">掌控</span>样式,<span style="color: black;">非常多</span>人都<span style="color: black;">爱好</span>在js里面写样式,例如当页面滑动到某个<span style="color: black;">地区</span>之后要把某个条吸顶:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/bhuLuOhAhOS30b0mkEHaRaNdSKwsfCFL6407L29s3rSbnia5N46P2Pn0Vn7jhgGb4oAicTwX2n2dThzzxxDYk16w/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">页面滑到下面那个灰色的条再继续往下滑的时候,那个灰色条就要保持吸顶状态:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/bhuLuOhAhOS30b0mkEHaRaNdSKwsfCFLYNNKqtZJN5HvRLlATOIuJZqjY5MyUyLc98GF7PNN2eslY4yUHIZ8bg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">可能不少人会这么写:</span></p><span style="color: black;">$(<span style="color: black;">".bar"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">).css({</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> position: fixed;</p> top: <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> left: <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>});
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>当用户往上滑的时候取消fixed:</span></p><span style="color: black;">$(<span style="color: black;">".bar"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">).css({</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> position: static;</p>});
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你用react,你可能会设置一个style的state数据,但其实这都<span style="color: black;">同样</span>,都把css杂合到js里面了。某个想要<span style="color: black;">检测</span>你样式的人,想要给你改个bug,他<span style="color: black;">检测</span>浏览器<span style="color: black;">发掘</span>有个标签style里的属性,<span style="color: black;">而后</span>他找半天找不到是在哪里设置的,最后他<span style="color: black;">发掘</span>是在某个js的某个隐蔽的角落设置了。你在js里面设置了样式,<span style="color: black;">而后</span>css里面<span style="color: black;">亦</span>会有样式,在改css的时候,<span style="color: black;">倘若</span>不<span style="color: black;">晓得</span>js里面<span style="color: black;">亦</span>有设置了样式,<span style="color: black;">那样</span>可能会<span style="color: black;">出现</span>冲突,在某种<span style="color: black;">要求</span>下触发了js里面设置样式。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此</span>不<span style="color: black;">举荐</span>直接在js里面更改样式属性,而应该<span style="color: black;">经过</span>增删类来<span style="color: black;">掌控</span>样式,<span style="color: black;">这般</span>子样式还是回归到css文件里面。例如上面<span style="color: black;">能够</span>改成<span style="color: black;">这般</span>:</span></p><span style="color: black;"><span style="color: black;">//<span style="color: black;">增多</span>fixed</span>$(<span style="color: black;">".bar"</span>).addClass(<span style="color: black;">"fixed"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">//取消fixed</span>$(<span style="color: black;">".bar"</span>).removeClass(<span style="color: black;">"fixed"</span>);
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">fixed的样式:</span></p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.bar.fixed{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> position: fixed;</p> left: <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> top: <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>看到,<span style="color: black;">这般</span>的<span style="color: black;">规律</span>就非常清晰,并且回滚fixed,不<span style="color: black;">必须</span>把它的position还原为static,<span style="color: black;">由于</span>它不<span style="color: black;">必定</span>是static,<span style="color: black;">亦</span>有可能是relative,这种方式在取消掉一个类的时候,不<span style="color: black;">必须</span>去关心<span style="color: black;">本来</span>是什么,该是什么就会是什么。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">然则</span>有一种是避免不了的,<span style="color: black;">便是</span>监听scroll事件<span style="color: black;">或</span>mousemove事件,动态地改变位置。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这种<span style="color: black;">经过</span><span style="color: black;">掌控</span>类的方式还有一个好处,<span style="color: black;">便是</span>当你给容器动态地增删一个类时,你<span style="color: black;">能够</span>借助子元素<span style="color: black;">选取</span>器,用这个类<span style="color: black;">掌控</span>它的子元素的样式,<span style="color: black;">亦</span>是很方便。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">还有<span style="color: black;">非常多</span>人可能会觉得html和css/js脱耦,那<span style="color: black;">便是</span><span style="color: black;">不可</span>在html里面写style,<span style="color: black;">不可</span>在html里面写script标签,<span style="color: black;">然则</span>凡事都不是绝对的,<span style="color: black;">倘若</span>有一个标签,它和其它标签就一个font-size不<span style="color: black;">同样</span>,那你直接给它写一个font-size的内联样式,又何尝不可呢,在性能上<span style="color: black;">来讲</span>,<span style="color: black;">倘若</span>你写个class,它还得去匹配这个class,比不上style<span style="color: black;">有效</span>吧。<span style="color: black;">或</span>是你这个html文件就<span style="color: black;">那样</span>20、30行css,那直接在head标签加个style,直接写在head里面好了,<span style="color: black;">这般</span>你就少管理了一个文件,并且浏览器<span style="color: black;">不消</span>去加载一个外链的文件。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">有时候直接在html写script标签是必要的,它的<span style="color: black;">优良</span><span style="color: black;">亦</span>是<span style="color: black;">不消</span>加载外链文件,处理速度会<span style="color: black;">火速</span>,几乎和dom渲染<span style="color: black;">同期</span>,这个在<span style="color: black;">处理</span>页面闪动的时候比较有用。<span style="color: black;">由于</span><span style="color: black;">倘若</span>要用js动态地改变<span style="color: black;">已然</span>加载好的dom,放在外链里面肯定会闪一下,而直接写的script就不会有这个问题,即使这个script是放在了body的后面。例如下面:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/bhuLuOhAhOS30b0mkEHaRaNdSKwsfCFLff0cHribqo1lygAhWz91qEqC1aft5TJrfrLiabUWyheb8AWak96SMt2Q/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">原始数据是带p标签的,<span style="color: black;">然则</span>在textarea里面展示的时候<span style="color: black;">必须</span>把p改成换行\r\n,<span style="color: black;">倘若</span>在dom渲染之后再在外链里面更新dom就会<span style="color: black;">显现</span>上面的闪动的<span style="color: black;">状况</span>。你可能会说我用react,数据都是动态渲染的,渲染前<span style="color: black;">已然</span>处理好了,不会<span style="color: black;">显现</span>上面的<span style="color: black;">状况</span>。<span style="color: black;">那样</span>,好吧,<span style="color: black;">最少</span>你<span style="color: black;">认识</span>一下吧。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">和耦合相对的是内聚,写代码的原则<span style="color: black;">便是</span>低耦合、高聚合。<span style="color: black;">所说</span>内聚<span style="color: black;">便是</span>说一个模块的职责功能<span style="color: black;">非常</span>紧密,不可分割,这个模块<span style="color: black;">便是</span>高内聚的。<span style="color: black;">咱们</span>先从重复代码说起。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">3. 减少重复代码</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">假设有一段代码在<span style="color: black;">另一</span>一个<span style="color: black;">地区</span><span style="color: black;">亦</span>要被用到,但又不太<span style="color: black;">同样</span>,<span style="color: black;">那样</span>最简单的<span style="color: black;">办法</span>当然是copy一下,<span style="color: black;">而后</span>改一改。这<span style="color: black;">亦</span>是不少人采取的办法,<span style="color: black;">这般</span>就<span style="color: black;">引起</span>了:<span style="color: black;">倘若</span>以后要改一个相同的<span style="color: black;">地区</span>就得<span style="color: black;">同期</span>改好多个<span style="color: black;">地区</span>,就很麻烦了。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如有一个搜索的界面:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/bhuLuOhAhOS30b0mkEHaRaNdSKwsfCFLHXAbFVZRdicWAibuSIcc7VyhZNwkA0Vaf4MicKl3R2rb8ld4vCicR8wohw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">用户<span style="color: black;">能够</span><span style="color: black;">经过</span>点击search按钮触发搜索,<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">经过</span>点击下拉<span style="color: black;">或</span><span style="color: black;">经过</span>输入框的change触发搜索,<span style="color: black;">因此</span>你可能会这么写:</span></p><span style="color: black;">$(<span style="color: black;">"#search"</span>).on(<span style="color: black;">"click"</span>, <span style="color: black;"><span style="color: black;">function</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">var</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> formData = getFormData();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $.ajax({</p> url: <span style="color: black;">/search</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> data: formData,</p> success: <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">data</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> showResult(data);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> });</p>});
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在change里面又重新发请求:</span></p><span style="color: black;">$(<span style="color: black;">"input"</span>).on(<span style="color: black;">"change"</span>, <span style="color: black;"><span style="color: black;">function</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">//把用户的搜索<span style="color: black;">要求</span>展示进行改变</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> changeInputFilterShow();</p> <span style="color: black;">var</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> formData = getFormData();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$.ajax({</p> url: <span style="color: black;">/search</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> data: formData,</p> success: <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">data</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> showResult(data);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> });</p>});
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">change里面<span style="color: black;">必须</span>对搜索<span style="color: black;">要求</span>的展示进行更改,和click事件不太<span style="color: black;">同样</span>,<span style="color: black;">因此</span>图一时之快就把代码拷了一下。<span style="color: black;">然则</span><span style="color: black;">这般</span>是<span style="color: black;">有害</span>于代码的<span style="color: black;">守护</span>的,<span style="color: black;">因此</span>你可能会想到把获取数据和发请求的那部分代码单独抽离封装在一个函数,<span style="color: black;">而后</span>两边都调一下:</span></p><span style="color: black;"><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">getAndShowData</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">var</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> formData = getFormData();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $.ajax({</p> url: <span style="color: black;">/search</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> data: formData,</p> success: <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">data</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">showResult(data);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> });</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>$(<span style="color: black;">"#search"</span>).on(<span style="color: black;">"click"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, getAndShowData);</p>$(<span style="color: black;">"input"</span>).on(<span style="color: black;">"change"</span>, <span style="color: black;"><span style="color: black;">function</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> changeInputFilterShow();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> getAndShowData();</p>});
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在抽成一个函数的<span style="color: black;">基本</span>上,又<span style="color: black;">发掘</span>这个函数其实有点大,<span style="color: black;">由于</span><span style="color: black;">这儿</span>面要获取表单数据,还要对数据进行格式化,用做请求的参数。<span style="color: black;">倘若</span>用户触发得比较快,还要记录上次请求的xhr,在每次发请求前cancle掉上一次的xhr,并且可能对请求做一个loading效果,<span style="color: black;">增多</span>用户体验,还要对出错的<span style="color: black;">状况</span>进行处理,<span style="color: black;">所有</span>都要在ajax里面。<span style="color: black;">因此</span>最好对getAndShowData继续拆分,很自然地会想到把它分离成一个模块,一个单独的文件,叫做search-ajax。所有发请求的处理都在这个模块里面统一操作。对外只<span style="color: black;">供给</span>一个search.ajax的接口,传的参数为当前的页数<span style="color: black;">就可</span>。所有<span style="color: black;">必须</span>发请求的都调一下这个模块的这个接口就好了,除了上面的两种<span style="color: black;">状况</span>,还有点击分页的情景。<span style="color: black;">这般</span>不管哪种情景都很方便,我不<span style="color: black;">必须</span>关心请求是怎么发的,结果是怎么处理的,我只要传一个当前的页数给你就好了。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">再往下,会<span style="color: black;">发掘</span>,在<span style="color: black;">表示</span>结果那里,即上面代码的第7行,<span style="color: black;">必须</span>对有结果、<span style="color: black;">没</span>结果的<span style="color: black;">状况</span>分别处理,<span style="color: black;">因此</span>又搞了一个函数叫做showResult,这个函数有点大,它里面的<span style="color: black;">规律</span><span style="color: black;">亦</span>比较<span style="color: black;">繁杂</span>,有结果的时候除了更新列表结果,还要更新结果总数、更新分页的状态。<span style="color: black;">因此呢</span>这个showResult一个函数难以担当大任。<span style="color: black;">因此</span>把这个show-result<span style="color: black;">亦</span>当独分离出一个模块,负责结果的处理。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">到此,<span style="color: black;">咱们</span>整一个search的UML图应该是<span style="color: black;">这般</span>的:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="http://mmbiz.qpic.cn/mmbiz_png/bhuLuOhAhOS30b0mkEHaRaNdSKwsfCFLFdPuLOo6WiabwKFxNo2hAGnpRWYicAHRFq1KssnYpuhsHgMe8gaNM0Xw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>上面把发请求的又再单独封装<span style="color: black;">成为了</span>一个模块,<span style="color: black;">由于</span>这个除了搜索发请求外,其它的请求<span style="color: black;">亦</span><span style="color: black;">能够</span>用到。<span style="color: black;">同期</span>search-result会用到两个展示的模板。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因为</span>不只一个页面会用到搜索的功能,<span style="color: black;">因此</span>再把上面继续抽象,把它封装成一个search-app的模块,<span style="color: black;">必须</span>用到的页面只需require这个search-app,调一下它的init函数,<span style="color: black;">而后</span>传些定制的参数就<span style="color: black;">能够</span>用了。这个search-app就相当于一个搜索的插件。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此</span>整一个的思路是<span style="color: black;">这般</span>的:<span style="color: black;">显现</span>了重复代码 -> 封装成一个函数 -> 封装成一个模块 -> 封装成一个插件,抽象级别<span style="color: black;">持续</span><span style="color: black;">加强</span>,将共有的特性和有差异的<span style="color: black;">地区</span>分离出来。当你走在抽象与封装的路上的时候,那你应该<span style="color: black;">亦</span>是走在了大神的路上。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当然,<span style="color: black;">倘若</span>两个东西并<span style="color: black;">无</span><span style="color: black;">一起</span>点,<span style="color: black;">然则</span>你硬是要搞在<span style="color: black;">一块</span>,那是不可取的。我<span style="color: black;">这儿</span>说的封装并不是说,你<span style="color: black;">必定</span>要<span style="color: black;">运用</span>requirejs、es6的import<span style="color: black;">或</span>是webpack的require,关键在于你要有这种模块化的思想,并不<span style="color: black;">指的是</span><span style="color: black;">工具</span>上的,不管你用的哪一个,只要你有这种抽象的想法,那都是可取的。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">模块化的极端是拆分粒度太细,一个简单的功能,明明十行代码写在<span style="color: black;">一块</span>就<span style="color: black;">能够</span>搞定的事情,硬是写了<span style="color: black;">7、</span>八层函数栈,<span style="color: black;">每一个</span>函数<span style="color: black;">仅有</span>两、三行。<span style="color: black;">这般</span>除了把你的<span style="color: black;">规律</span>搞得太<span style="color: black;">繁杂</span>之外,并<span style="color: black;">无</span>太多的好处。当你<span style="color: black;">显现</span>了重复代码,<span style="color: black;">或</span>是一个函数太大、功能太多,又或是<span style="color: black;">规律</span>里面写了三层循环又再嵌套了三层if,再或是你预感到你写的这个东西其他人<span style="color: black;">亦</span>可能会用到,这个时候你才<span style="color: black;">思虑</span>模块化,进行拆分比较合适。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面不管是search-result还是search-ajax<span style="color: black;">她们</span>在功能上都是高度内聚的,<span style="color: black;">每一个</span>模块都有自己的职责,不可拆分,这在面向对象编程里面叫做单一责职原则,一个模块只负责一个功能。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">再举一个例子,我在<strong style="color: blue;"><span style="color: black;">怎么样</span>实现前端裁剪上传<span style="color: black;">照片</span>功能</strong>(</span><span style="color: black;">http://yincheng.site/crop-upload-photo</span><span style="color: black;">)里面<span style="color: black;">说到</span>一个上传裁剪的实现,<span style="color: black;">这儿</span>面<span style="color: black;">包括</span>裁剪、压缩上传、进度条三大功能,<span style="color: black;">因此</span>我把它拆成三个模块:</span><img src="http://mmbiz.qpic.cn/mmbiz_png/bhuLuOhAhOS30b0mkEHaRaNdSKwsfCFLGlnkTa7bON7CdRRwibtJjalnJgBvTuUKXauxJNMLIPq7EibOGSbFUKOg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span><span style="color: black;">说到</span>的模块大部分是一个单例的object,不会去实例它,<span style="color: black;">通常</span><span style="color: black;">能够</span>满足大部分的需求。在这个单例的模块里面,它自己的“私有”函数<span style="color: black;">通常</span>是<span style="color: black;">经过</span>传参调用,<span style="color: black;">然则</span><span style="color: black;">倘若</span><span style="color: black;">必须</span>传递的数据比较多的时候,就有点麻烦了,这个时候<span style="color: black;">能够</span><span style="color: black;">思虑</span>把它封装成一个类。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">4. 封装成一个类</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的裁剪上传里面的进度条progress-bar,一个页面里可能有几个要上传的<span style="color: black;">地区</span>,<span style="color: black;">每一个</span>上传的<span style="color: black;">地区</span>都会有进度条,<span style="color: black;">每一个</span>进度条都有自己的数据,<span style="color: black;">因此</span><span style="color: black;">不可</span>像在最上面说的,在一个文件的最上面定义<span style="color: black;">有些</span>变量<span style="color: black;">而后</span>为这个模块里面的函数共用,只能是<span style="color: black;">经过</span>传递参数的形式,即在最<span style="color: black;">起始</span>调用的时候定义<span style="color: black;">有些</span>数据,<span style="color: black;">而后</span>一层一层地传递下去。<span style="color: black;">倘若</span>这些数据<span style="color: black;">非常多</span>的话就有点麻烦。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此</span>稍微变通一下,把progress-bar封装成一个类:</span></p><span style="color: black;"><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">ProgressBar</span>(<span style="color: black;">$container</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">this</span>.$container = $container; <span style="color: black;">//进度条外面的容器</span> <span style="color: black;">this</span>.$meter = <span style="color: black;">null</span>; <span style="color: black;">//进度条可视部分</span> <span style="color: black;">this</span>.$bar = <span style="color: black;">null</span>; <span style="color: black;">//进度条存放可视部分的容器</span> <span style="color: black;">this</span>.$barFullWidth = $container.width() * <span style="color: black;">0.9</span>; <span style="color: black;">//进度条的宽度</span> <span style="color: black;">this</span>.show(); <span style="color: black;">//new一个对象的时候就<span style="color: black;">表示</span></span>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">或</span>你用ES6的class,<span style="color: black;">然则</span>本质上是<span style="color: black;">同样</span>的,<span style="color: black;">而后</span>这个ProgressBar的成员函数就<span style="color: black;">能够</span><span style="color: black;">运用</span>定义的这些“私有”变量,例如设置进度条的进度函数:</span></p><span style="color: black;">ProgressBar.prototype.setProgress = <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">percentage, time</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> time = <span style="color: black;">typeof</span> time === <span style="color: black;">"undefined"</span> ? <span style="color: black;">100</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> : time;</p> <span style="color: black;">this</span>.$meter.stop().animate({width: <span style="color: black;">parseInt</span>(<span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$barFullWidth * percentage)}, time);</p>};
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这个<span style="color: black;">运用</span>了两个私有变量,<span style="color: black;">倘若</span>再加上原先两个,用传参的方式就得传四个。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>类是模块化的一种思想,<span style="color: black;">另一</span>一种常用的还有策略模式。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">5. <span style="color: black;">运用</span>策略模式</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">假设要实现下面三个弹框:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="http://mmbiz.qpic.cn/mmbiz_png/bhuLuOhAhOS30b0mkEHaRaNdSKwsfCFLGnpGcPSypBhSvliagmMPuOdbcrjqzaicrYMicXvXkWwlu7cYzxbPjLnpQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这三个弹框<span style="color: black;">没</span>论是在样式上还是在功能上都是<span style="color: black;">同样</span>的,<span style="color: black;">独一</span>的区别是上面标题文案是不<span style="color: black;">同样</span>的。最简单的可能是把<span style="color: black;">每一个</span>弹框的html都copy一下,<span style="color: black;">而后</span>改一改。<span style="color: black;">倘若</span>你用react,你可能会用拆分组件的方式,上面一个组件,下面一个组件,<span style="color: black;">那样</span>好吧,你就<span style="color: black;">这般</span>搞吧。<span style="color: black;">倘若</span>你没用react,你可能得想办法组织下你的代码。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你有策略模式的思想,你可能会想到把上面的标题当作一个个的策略。<span style="color: black;">首要</span>定义<span style="color: black;">区别</span>弹框的类型,一一标志<span style="color: black;">区别</span>的弹框:</span></p><span style="color: black;"><span style="color: black;">var</span> popType = [<span style="color: black;">"register"</span>, <span style="color: black;">"favHouse"</span>, <span style="color: black;">"saveSearch"</span>];</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">定义三种popType一一对应上面的三个弹框,<span style="color: black;">而后</span>每种popType都有对应的文案:</span></p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Data.text.pop = {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> register: {</p> titlte: <span style="color: black;">"Create Your Free Account"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> subTitle: <span style="color: black;">"Search Homes and Exclusive Property Listings"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">},</p> favHouse: {title: <span style="color: black;">"xxx"</span>, subTitle: <span style="color: black;">"xxx"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> },</p> saveSearch: {title: <span style="color: black;">"xxx"</span>, subTitle: <span style="color: black;">"xxx"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>};
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">{tittle: “”, subtitle: “”}这个就当作是弹框文案策略,<span style="color: black;">而后</span>再写弹框的html模板的时候引入一个占位变量:</span></p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{{title}}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {{subTitile}}</p> <span style="color: black;"><span style="color: black;"><!--其它内容--></span> </<span style="color: black;">div</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">></p></<span style="color: black;">section</span>>
</span>
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在渲染这个弹框的时候,<span style="color: black;">按照</span>传进来的popType映射到<span style="color: black;">区别</span>的文案:</span></p><span style="color: black;"><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">showPop</span>(<span style="color: black;">popType</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Mustache.render(popTemplate, Data.text.pop)</p>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>用Data.text.pop映射到了对应的文案,<span style="color: black;">倘若</span>用react你把一个个的标题封装成一个组件,其实思想是<span style="color: black;">同样</span>的。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">然则</span>这个并不是严格的策略模式,<span style="color: black;">由于</span>策略<span style="color: black;">便是</span>要有执行的东西嘛,<span style="color: black;">咱们</span><span style="color: black;">这儿</span>其实是一个写死的文案,<span style="color: black;">然则</span><span style="color: black;">咱们</span>借助了策略模式的思想。接下来继续说<span style="color: black;">运用</span>策略模式做一些执行的事情。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在上面的弹框的触发机制分别是:用户点击了注册、点击了<span style="color: black;">保藏</span>房源、点击了<span style="color: black;">保留</span>搜索<span style="color: black;">要求</span>。<span style="color: black;">倘若</span>用户<span style="color: black;">无</span>登陆就会弹一个注册框,当用户注册完之后,要继续执行用户<span style="color: black;">本来</span>的操作,例如该<span style="color: black;">保藏</span>还是<span style="color: black;">保藏</span>,<span style="color: black;">因此</span><span style="color: black;">必要</span>要有一个注册后的回调,并且这个回调做的事情还不<span style="color: black;">同样</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当然,你<span style="color: black;">能够</span>在回调里面写<span style="color: black;">非常多</span>的if else或者是case:</span></p><span style="color: black;"><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">popCallback</span>(<span style="color: black;">popType</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">switch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(popType){</p> <span style="color: black;">case</span> <span style="color: black;">"register"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">: </p> <span style="color: black;">//do nothing</span> <span style="color: black;">break</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">case</span>: <span style="color: black;">"favHouse"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">: </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> favHouse();</p> <span style="color: black;">break</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">case</span>: <span style="color: black;">"saveSearch"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> saveSearch();</p> <span style="color: black;">break</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">然则</span>当你的case<span style="color: black;">非常多</span>的时候,看起来可能就不是<span style="color: black;">尤其</span>好了,<span style="color: black;">尤其</span>是if else的那种写法。这个时候就<span style="color: black;">能够</span><span style="color: black;">运用</span>策略模式,<span style="color: black;">每一个</span>回调都是一个策略:</span></p><span style="color: black;"><span style="color: black;">var</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> popCallback = {</p> favHouse: <span style="color: black;"><span style="color: black;">function</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">//do sth.</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> },</p> saveSearch: <span style="color: black;"><span style="color: black;">function</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">//do sth.</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">按照</span>popType映射调用相应的callback,如下:</span></p><span style="color: black;"><span style="color: black;">var</span> popCallback = <span style="color: black;">require</span>(<span style="color: black;">"pop-callback"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">if</span>(<span style="color: black;">typeof</span> popCallback === <span style="color: black;">"function"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> popCallback();</p>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这般</span>它<span style="color: black;">便是</span>一个完整的策略模式了,<span style="color: black;">这般</span>写有<span style="color: black;">非常多</span>好处。<span style="color: black;">倘若</span>以后<span style="color: black;">必须</span><span style="color: black;">增多</span>一个弹框类型popType,<span style="color: black;">那样</span>只要在popCallback里面添加一个函数就好了,<span style="color: black;">或</span>要删掉一个popType,相应地注释掉某个函数<span style="color: black;">就可</span>。并不<span style="color: black;">必须</span>去改动原有代码的<span style="color: black;">规律</span>,而采用if else的方式就得去修改原有代码的<span style="color: black;">规律</span>,<span style="color: black;">因此</span><span style="color: black;">这般</span>对扩展是开放的,而对修改是封闭的,这<span style="color: black;">便是</span>面向对象编程里面的开闭原则。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在js里面实现策略模式<span style="color: black;">或</span>是其它设计模式都是很自然的方式,<span style="color: black;">由于</span>js里面function<span style="color: black;">能够</span>直接<span style="color: black;">做为</span>一个普通的变量,而在C++/Java里面<span style="color: black;">必须</span>用<span style="color: black;">有些</span>技巧,玩<span style="color: black;">有些</span>OO的把戏<span style="color: black;">才可</span>实现。例如上面的策略模式,在Java里面<span style="color: black;">必须</span>先写一个接口类,里面定义一个接口函数,<span style="color: black;">而后</span><span style="color: black;">每一个</span>策略都封装成一个类,分别实现接口类的接口函数。而在js里面的设计模式<span style="color: black;">常常</span>几行代码就写出来,这可能<span style="color: black;">亦</span>是做为函数式编程的一个优点。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">前端和设计模式经常打交道的还有<span style="color: black;">拜访</span>者模式。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">6. <span style="color: black;">拜访</span>者模式</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">事件监听<span style="color: black;">便是</span>一个<span style="color: black;">拜访</span>者模式,一个典型的<span style="color: black;">拜访</span>者模式<span style="color: black;">能够</span>这么实现,<span style="color: black;">首要</span>定义一个Input的类,初始化它的<span style="color: black;">拜访</span>者列表</span></p><span style="color: black;"><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">Input</span>(<span style="color: black;">inputDOM</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">//用来存放<span style="color: black;">拜访</span>者的数据结构</span> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.visitiors = {</p> <span style="color: black;">"click"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">: [],</p> <span style="color: black;">"change"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">: [],</p> <span style="color: black;">"special"</span>: [] <span style="color: black;">//自定义事件</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.inputDOM = inputDOM;</p>}
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">供给</span>一个对外的添加<span style="color: black;">拜访</span>者的接口:</span></p><span style="color: black;">Input.prototype.on = <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">eventType, callback</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">if</span>(<span style="color: black;">typeof</span> <span style="color: black;">this</span>.visitiors !== <span style="color: black;">"undefined"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.visitiors.push(callback);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>};
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">使用者调用on,传递两个参数, 一个是事件类型,即<span style="color: black;">拜访</span>类型,<span style="color: black;">另一</span>一个是<span style="color: black;">详细</span>的<span style="color: black;">拜访</span>者,<span style="color: black;">这儿</span>是回调函数。Input就会将<span style="color: black;">拜访</span>者添加到它的<span style="color: black;">拜访</span>者列表。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">同期</span>Input还<span style="color: black;">供给</span>了一个删除<span style="color: black;">拜访</span>者的接口:</span></p><span style="color: black;">Input.prototype.off = <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">eventType, callback</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">var</span>visitors =<span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.visitiors;</p> <span style="color: black;">if</span>(<span style="color: black;">typeof</span> visitiors !== <span style="color: black;">"undefined"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">var</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> index = visitiors.indexOf(callback);</p> <span style="color: black;">if</span>(index >= <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> visitiors.splice(index, <span style="color: black;">1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>};
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这般</span>子,Input就和<span style="color: black;">拜访</span>者<span style="color: black;">创立</span>起了关系,<span style="color: black;">或</span>说<span style="color: black;">拜访</span>者<span style="color: black;">已然</span>成功地向接收者都订阅了<span style="color: black;">信息</span>,一旦接书者收到了<span style="color: black;">信息</span>会向它的<span style="color: black;">拜访</span>者一一传递:</span></p><span style="color: black;">Input.prototype.trigger = <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">eventType, event</span>)</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">var</span> visitors = <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.visitiors;</p> <span style="color: black;">var</span> eventFormat = processEvent(event); <span style="color: black;">//获取<span style="color: black;">信息</span>并做格式化</span> <span style="color: black;">if</span>(<span style="color: black;">typeof</span> visitors !== <span style="color: black;">"undefined"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">for</span>(<span style="color: black;">var</span> i = <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">; i < visitors.length; i++){</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">visitors(eventFormat);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>};
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">trigger可能是用户调的,<span style="color: black;">亦</span>可能是底层的控件调用的。在其它<span style="color: black;">行业</span>,它可能是一个光感控件触发的。不管<span style="color: black;">怎么样</span>,一旦有人触发了trigger,接收者就会一一下发<span style="color: black;">信息</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你<span style="color: black;">晓得</span>了事件监听的模式是<span style="color: black;">这般</span>的,可能对你写代码会有<span style="color: black;">帮忙</span>。例如点击下面的搜索<span style="color: black;">要求</span>的X,要把上面的搜索框清空,<span style="color: black;">同期</span>还要触发搜索,并把输入框右边的X去掉。要附带着做几件事情。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"><span style="color: black;">这个时候你可能会<span style="color: black;">这般</span>写:</span></p><span style="color: black;">$(<span style="color: black;">".icon-close"</span>).on(<span style="color: black;">"click"</span>, <span style="color: black;"><span style="color: black;">function</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> $(<span style="color: black;">this</span>).parent().remove(); <span style="color: black;">//删除本身的展示</span> $(<span style="color: black;">"#search-input"</span>).val(<span style="color: black;">""</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>searchAjax.ajax();<span style="color: black;">//触发搜索</span> $(<span style="color: black;">"#clear-search"</span>).hide(); <span style="color: black;">//<span style="color: black;">隐匿</span>输入框x</span>});
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但其实<span style="color: black;">这般</span>有点<span style="color: black;">负担</span>,<span style="color: black;">由于</span>在上面的搜索输入框肯定<span style="color: black;">亦</span>会相应的操作,当用户输入为空时,自动<span style="color: black;">隐匿</span>右边的x,并且输入框change的时候会自动搜索,<span style="color: black;">亦</span><span style="color: black;">便是</span>说所有附加的事情输入框那边<span style="color: black;">已然</span>有了,<span style="color: black;">因此</span>其实只<span style="color: black;">必须</span>触发下输入框的change事件就好了:</span></p><span style="color: black;">$(<span style="color: black;">".icon-close"</span>).on(<span style="color: black;">"click"</span>, <span style="color: black;"><span style="color: black;">function</span>()</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> $(<span style="color: black;">this</span>).parent().remove(); <span style="color: black;">//删除本身的展示</span> $(<span style="color: black;">"#search-input"</span>).val(<span style="color: black;">""</span>).trigger(<span style="color: black;">"change"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>});
</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">输入框为空时,该怎么处理,search输入框会相应地处理,下面那个<span style="color: black;">要求</span>展示的x不<span style="color: black;">必须</span>去关心。触发了change之后,会把相应的<span style="color: black;">信息</span>下发给search输入框的<span style="color: black;">拜访</span>者们。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当然,你用react你可能不会<span style="color: black;">这般</span>想了,你应该是在<span style="color: black;">科研</span>组件间怎么通信地好。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上文提及<span style="color: black;">运用</span>传参避免全局耦合,<span style="color: black;">而后</span>在js里面<span style="color: black;">经过</span><span style="color: black;">掌控</span>class减少和css的耦合,和耦合相对的是内聚,出发点是重复代码,减少拷贝代码会有一个抽象和封装的过程:function -> 模块 -> 插件/框架,封装常用的还有封装成一个类,方便<span style="color: black;">掌控</span>私有数据。<span style="color: black;">这般</span>可实现高内聚,除此<span style="color: black;">办法</span>,还有设计模式的思想,上面介绍了策略模式和<span style="color: black;">拜访</span>者模式的原理和应用,以及在写代码的启示。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">期盼</span>上文能对你有所启迪,如有不对之处还请指出。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">—— 上期头条 ——</strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">前端工程师<span style="color: black;">必须</span>实用网站</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">『极乐科技』专栏原创作者征集进行时…</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(点击阅读原文直达极乐知乎专栏)</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你乐分享、愿交流、钻技术;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你<span style="color: black;">期盼</span>记录自己成长过程;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你期待与大牛比肩;</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你<span style="color: black;">巴望</span>得到<span style="color: black;">更加多</span>关注…</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">欢迎加入<span style="color: black;">咱们</span>知乎专栏原创作者团队</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>原创作者团队有大牛,有妹纸,有逗比!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>原创作者团队能交流,善协作,爱学习!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">加入原创作者团队,结实<span style="color: black;">更加多</span>的他他她!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">另外</span>,<span style="color: black;">针对</span><span style="color: black;">优秀</span>原创内容,极乐科技会予以<span style="color: black;">关联</span>激励!让<span style="color: black;"><strong style="color: blue;">劳</strong></span><span style="color: black;"><strong style="color: blue;"><span style="color: black;">有</span></strong></span><span style="color: black;"><strong style="color: blue;"><span style="color: black;">所</span></strong></span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">得</span></span></strong></span>!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">倘若</span>你对此有兴趣,欢迎在<span style="color: black;">微X</span>平台留言!</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在阅读<span style="color: black;">文案</span>过程中如有疑问,请发布到极乐官网;<span style="color: black;">或</span>长按下方二维码,关注极官方<span style="color: black;">微X</span>平台,在平台下方留言,<span style="color: black;">咱们</span>将<span style="color: black;">第1</span>时间为您解答。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></p>
软文发布论坛开幕式圆满成功。 http://www.fok120.com 我深受你的启发,你的话语是我前进的动力。 回顾过去一年,是艰难的一年;展望未来,是辉煌的一年。 你的见解真是独到,让我受益良多。
页:
[1]