专栏名称: SegmentFault思否
SegmentFault (www.sf.gg)开发者社区,是中国年轻开发者喜爱的极客社区,我们为开发者提供最纯粹的技术交流和分享平台。
目录
相关文章推荐
程序员的那些事  ·  等了 13 ... ·  2 天前  
程序员的那些事  ·  趣图:好比成人玩积木还以为自己是天才 ·  4 天前  
OSC开源社区  ·  “开源疯王”Matt ... ·  1 周前  
51好读  ›  专栏  ›  SegmentFault思否

javascript 实用技巧和写法建议

SegmentFault思否  · 公众号  · 程序员  · 2017-09-12 08:00

正文

1.前言

从大学到现在,接触前端已经有几年了,感想方面,就是对于程序员而言,想要提高自己的技术水平和编写易于阅读和维护的代码,我觉得不能每天都是平庸的写代码,更要去推敲,去摸索和优化代码,总结当中的技巧,积极听取别人的建议,这样自己的技术水平会提高的更快。那么今天,我在这里就分享一下关于javascript方面的写作的实用技巧和建议,这些技巧和建议是我平常在开发项目上会用到的,希望能让大家学到知识,更希望能起到一个交流意见的作用,也就是说大家有什么好的技巧或者建议,欢迎分享,或者觉得我的想法存在什么问题,欢迎指出!

2.更短的数组去重写法

  1.    [...new Set([2,"12",2,12,1,2,1,6,12,13,6])]

  2.    //[2, "12", 12, 1, 6, 13]

  3.    //es6的新特性

3.对象深浅拷贝

关于对象的深浅拷贝,我个人见解就是有一下几点:

  1. 深拷贝和浅拷贝只针对像Object, Array这样的引用类型数据。

  2. 浅拷贝是对对象引用地址进行拷贝,并没有开辟新的栈,也就是拷贝后的结果是两个对象指向同一个引用地址,修改其中一个对象的属性,则另一个对象的属性也会改变。

  3. 深拷贝则是开启一个新的栈,两个对象对应两个不同的引用地址,修改一个对象的属性,不会改变另一个对象的属性。

浅拷贝

  1.    var myInfo={name:'守候',sex:'男'};

   var newInfo=myInfo;

  1.    newInfo.sex='女';

  1.    console.log(myInfo)   //{name: "守候", sex: "女"}

假-深拷贝

假-深拷贝这个是自己随性命名的,大家看看就好,别当真!

  1.    var myInfo={name:'守候',sex:'男'};

  1.    var newInfo=Object.assign({},myInfo)

  1.    newInfo.sex='女';

  1.    console.log(myInfo)   //{name: "守候", sex: "男"}

  2.    console.log(newInfo)   //{name: "守候", sex: "女"}

真-深拷贝

真-深拷贝这个是自己随性命名的,大家看看就好,别当真!

看着深浅拷贝,区别写法很简单,但是那个上面的深拷贝写法是有问题的。看下面案例

  1.    var arr=[{a:1,b:2},{a:3,b:4}]

  2.    var newArr=Object.assign([],arr)

  3.    //截断数组

  4.    newArr.length=1

  5.    console.log(newArr)//[{a:1,b:2}]

  6.    console.log(arr)//[{a:1,b:2},{a:3,b:4}]

  7.    //操作newArr,这里看着对arr没影响,实际上已经挖了一个坑,下面就跳进去

  8.    newArr[0].a=123

  9.    //修改newArr[0]这个对象,也是影响了arr[0]这个对象

  10.    console.log(arr[0])//{a: 123, b: 2}

为什么会这样呢,因为Object.assign并不是深拷贝,是披着深拷贝外衣的浅拷贝。最多也是Object.assign会课拷贝第一层的值,对于第一层的值都是深拷贝,而到第二层的时候就是 复制引用。类似的情况还有,slice方法和concat方法等。

要解决这个问题,就得自己封装方法!如下

  1.    //利用递归来实现深拷贝,如果对象属性的值是引用类型(Array,Object),那么对该属性进行深拷贝,直到遍历到属性的值是基本类型为止。  

  2.    function deepClone(obj){    

  3.      if(!obj&& typeof obj!== 'object'){      

  4.        return;    

  5.      }    

  6.      var newObj= obj.constructor === Array ? [] : {};    

  7.      for(var key in obj){      

  8.        if(obj[key]){          

  9.          if(obj[key] && typeof obj[key] === 'object'){  

  10.            newObj[key] = obj[key].constructor === Array ? [] : {};

  11.            //递归

  12.            newObj[key] = deepClone(obj[key]);          

  13.          }else{            

  14.            newObj[key] = obj[key];        

  15.          }      

  16.        }    

  17.      }    

  18.      return newObj;

  19.    }

  20.    var arr=[{a:1,b:2},{a:3,b:4}]

  21.    var newArr=deepClone(arr)

  22.    console.log(arr[0])//{a:1,b:2}

  23.    newArr[0].a=123

  24.    console.log(arr[0])//{a:1,b:2}

还有一个方法就是简单粗暴法,我现在在用的一个方法!原理很简单,就是先把对象转成字符串,再把字符串转成对象!也能实现同样效果

  1.    var newArr2=JSON.parse(JSON.stringify(arr));

  2.    console.log(arr[0])//{a:1,b:2}

  3.    newArr2[0].a=123

  4.    console.log(arr[0])//{a:1,b:2}

上面所说的浅拷贝,真假深拷贝(自己随性命名的),这几种情况,在开发上都有可能要用到,至于要使用哪一种方式,视情况而定!

4.使用事件委托

一个简单的需求,比如想给ul下面的li加上点击事件,点击哪个li,就显示那个li的innerHTML。这个貌似很简单!代码如下!

  1.    

  2.    

  3.        

  4.             charset="UTF-8">

  5.            </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><ul><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> </span><span class="atn" style="box-sizing: border-box; color: rgb(160, 212, 104); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">id</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">=</span><span class="atv" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"ul-test"</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">></span></ul></span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">3</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">4</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">5</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">6</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">7</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">8</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">9</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><script><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> </span><span class="atn" style="box-sizing: border-box; color: rgb(160, 212, 104); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">type</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">=</span><span class="atv" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"text/javascript"</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> oUl=document.getElementById(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"ul-test"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> oLi=oUl.getElementsByTagName(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"li"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,len=oLi.length;i<len;i++){</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                oLi[i].addEventListener(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"click"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                    alert(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">this</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">.innerHTML)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                })</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"></script></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">很简单,这样就实现了,实际上这里有坑,也待优化!</p><ol style="" class=" list-paddingleft-2"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); line-height: 22px; font-size: 14px !important;">for循环,循环的是li,10个li就循环10次,绑定10次事件,100个就循环了100次,绑定100次事件!</span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); line-height: 22px; font-size: 14px !important;">如果li不是本来就在页面上的,是未来元素,是页面加载了,再通过js动态加载进来了,上面的写法是无效的,点击li是没有反应的!</span></p></li></ol><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">所以就者需要用事件委托(即使不考虑上面的第二种情况,也是建议使用事件委托)!代码如下</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="dec" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><meta/><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> </span><span class="atn" style="box-sizing: border-box; color: rgb(160, 212, 104); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">charset</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">=</span><span class="atv" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"UTF-8"</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">></span></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><title/></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><ul><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> </span><span class="atn" style="box-sizing: border-box; color: rgb(160, 212, 104); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">id</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">=</span><span class="atv" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"ul-test"</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">></span></ul></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">3</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">4</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">5</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">6</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">7</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">8</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><li/></span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">9</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><script><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> </span><span class="atn" style="box-sizing: border-box; color: rgb(160, 212, 104); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">type</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">=</span><span class="atv" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"text/javascript"</span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> oUl=document.getElementById(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"ul-test"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            oUl.addEventListener(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"click"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(ev){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> ev=ev||window.event;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> target=ev.target||ev.srcElement;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//如果点击的最底层是li元素</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">if</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(target.tagName.toLowerCase()===</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'li'</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                    alert(target.innerHTML)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                }</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            })</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"></script></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="tag" style="box-sizing: border-box; color: rgb(237, 85, 101); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">这样写,即使是动态添加进来的li点击也有反应,还有一个就是ul只有一个,事件绑定在ul上,无论li多少个,都是添加一次事件!但是也是可能会有问题,如果li下面还有子元素,那么点击的时候,target可能不是li,而是鼠标点击那个位置的最底层元素!如下图,如果鼠标点击白色区域,那个target就是body元素,鼠标点击绿色区域target就是div元素,鼠标点击蓝色区域target就是ul,点击橙色就是li。</p><p><img data-s="300,640" data-type="png" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_png/aVp1YC8UV0fFbbFg8cDsGbEge3kseVibQLJicgfibiapoD7yvnsVVLoSyNmvhCXlkC03H14OvUGXXdNKMQb0U4zKgg/0?wx_fmt=png" class="" data-ratio="1.0025188916876575" data-w="397"/></p><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">5.使用对象作为函数参数</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">大家试想下这样一个函数--函数接受几个参数,但是这几个参数都不是必填的,函数该怎么处理?是不是下面这样</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> personInfo(name,phone,card){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//以上函数,可以任意传参数。比如我想传card等于1472586326。这下是不是这样写</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    personInfo(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">''</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">''</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'1472586326'</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">)</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">有没有觉得上面写法奇怪,不太优雅?下面这里看着舒服一点!</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> personInfo(opt){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    personInfo({card:</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'1472586326'</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">})</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">再想一下,如果一个函数,参数很多,怎么处理?</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> test(arg1,arg2,arg3,arg4,arg5,arg6,arg7){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">密集恐惧症复发没有复发?下面这样看着会舒服一点!</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> personInfo(opt){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">最后再想一下,如果需求改了,操作函数也要改!函数也要增加一个参数。</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//原来函数</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> personInfo(name,phone,card){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//修改后</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> personInfo(name,age,phone,card){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">这样就是参数修改一次,函数的参数就要修改一次!如果是用对象,就不会出现这样问题!</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//修改前后都是这样,变得是函数的操作内容和调用时候的传参!</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> personInfo(opt){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">看了上面的几个栗子,总结来说,就是当函数的参数不固定的时候,参数多(三个或者三个以上)的时候,建议用一个对象记录参数,这样会比较方便,也为以后如果参数要改留了条后路!</p><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">6.使用push和apply合并数组</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">合并数组这个已经是老生常谈的话题了,方法也是多种多样!</p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box; color: rgb(0, 0, 0);">concat</strong></p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> arr1=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">3</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">4</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">5</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">],arr2=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">6</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">7</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">8</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">9</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">10</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">];</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    arr1=arr1.concat(arr2)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(arr1)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">concat会一个全新的数组,表示arr1和arr2两个数组的组合,并让arr1和arr2不变。简单吧?</p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">但如果arr1和arr2的长度都很长,那就产生了一个很长很长的数组,内存又被占用了那么多。但是数组长度没限制!</p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box; color: rgb(0, 0, 0);">for</strong></p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> arr1=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">3</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">4</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">5</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">],arr2=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">6</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">7</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">8</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">9</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">10</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">];</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,len=arr2.length;i<len/></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        arr1.push(arr2[i])</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(arr1)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">这里是往arr1循环添加arr2的元素,但是有一个情况,arr1的长度远小于arr2的长度,是不是循环arr1性能更好,循环次数更少。处理这个很简单,但是万一不知道arr1和arr2到底哪个长度更少呢?而且,for循环不够优雅!(当然,这个可以用迭代方法来替代)</p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box; color: rgb(0, 0, 0);">reduce</strong></p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> arr1=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">3</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">4</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">5</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">],arr2=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">6</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">7</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">8</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">9</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">10</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">];</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    arr1 = arr2.reduce( </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(coll,item){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">         coll.push( item );</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">         </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">return</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> coll;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">     }, arr1 );</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">     console.log(arr1)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">逼格高了一点,而且用ES6的箭头函数还可以减少一些代码量,但它仍然需要一个函数,每个元素都需要调用一次。</p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box; color: rgb(0, 0, 0);">push.apply</strong></p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> arr1=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">3</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">4</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">5</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">],arr2=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">6</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">7</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">8</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">9</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">10</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">];</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    arr1.push.apply(arr1,arr2);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(arr1)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">逼格看着高,代码少,也不会产生新的数组,也不难理解,就是调用 <code class="prettyprint code-in-text prettyprinted" style=" box-sizing: border-box; background: rgb(243, 241, 241); color: rgb(88, 88, 88); line-height: 18px; ; ; ; ; ; "><span class="pln" style="box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; display: inline-block; padding-right: 2px; padding-left: 2px; font-size: 14px;">arr1.push</span></code>这个函数实例的 <code class="prettyprint code-in-text prettyprinted" style=" box-sizing: border-box; background: rgb(243, 241, 241); color: rgb(88, 88, 88); line-height: 18px; ; ; ; ; ; "><span class="pln" style="box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; display: inline-block; padding-right: 2px; padding-left: 2px; font-size: 14px;">apply</span></code>方法,同时把 <code class="prettyprint code-in-text prettyprinted" style=" box-sizing: border-box; background: rgb(243, 241, 241); color: rgb(88, 88, 88); line-height: 18px; ; ; ; ; ; "><span class="pln" style="box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; display: inline-block; padding-right: 2px; padding-left: 2px; font-size: 14px;">arr2</span></code>当作参数传入,这样 <code class="prettyprint code-in-text prettyprinted" style=" box-sizing: border-box; background: rgb(243, 241, 241); color: rgb(88, 88, 88); line-height: 18px; ; ; ; ; ; "><span class="pln" style="box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; display: inline-block; padding-right: 2px; padding-left: 2px; font-size: 14px;">arr1.push</span></code>这个方法就会遍历 <code class="prettyprint code-in-text prettyprinted" style=" box-sizing: border-box; background: rgb(243, 241, 241); color: rgb(88, 88, 88); line-height: 18px; ; ; ; ; ; "><span class="pln" style="box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; display: inline-block; padding-right: 2px; padding-left: 2px; font-size: 14px;">arr2</span></code>数组的所有元素,达到合并的效果。相当于 <code class="prettyprint code-in-text prettyprinted" style=" box-sizing: border-box; background: rgb(243, 241, 241); color: rgb(88, 88, 88); line-height: 18px; ; ; ; ; ; "><span class="pln" style="box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; display: inline-block; padding-right: 2px; padding-left: 2px; font-size: 14px;">arr1.push.apply(arr1,[6,7,8,9,10]);</span></code>,最后相当于 <code class="prettyprint code-in-text prettyprinted" style=" box-sizing: border-box; background: rgb(243, 241, 241); color: rgb(88, 88, 88); line-height: 18px; ; ; ; ; ; "><span class="pln" style="box-sizing: border-box; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; display: inline-block; padding-right: 2px; padding-left: 2px; font-size: 14px;">arr1.push(6,7,8,9,10)</span></code>。遗憾的就是,这个方法对数组长度有限制,网上说法是不同浏览器,不同的长度限制,一般不超过10万!</p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">之前是建议用push.apply,但是现在保留意见,就是大家觉得哪个方式用哪个方式!这个没有一定的对错!</p><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">7.toFixed保留整数</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">在开发上,经常会遇到最多保留多少位小数或者类似的问题,针对这个,使用toFixed可以很简单的解决问题,但是如果数据是要和后台交互的,而且后台存储的数据一般是保存数字类型,而使用toFixed后生成的是一个字符串,这下,就需要把toFixed生成的是一个字符串转成数字类型,转发很多。今天我说一个最简单--+。代码如下</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> a=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">123.36896335</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">.toFixed(</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(a)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//'123.37'</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    a=+a</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(a)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//123.37</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">PS:a=a|0和~~a也可以实现,但是生成的是一个整数,如下</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> a=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">123.36896335</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">.toFixed(</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(a)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//'123.37'</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    a=a|</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">  </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(a)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//123 </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//---------------------------------分割线</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> a=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">123.36896335</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">.toFixed(</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(a)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//'123.37'</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    a=~~a  </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(a)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//123</span></code></span></p></li></ol></pre><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">8.其它类型数据转布尔数据</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">下面的转换,大家一看就明白了,不多说。</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    console.log(!!</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'123'</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//true</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    !!</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">12</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//true</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    !!-</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//true</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    !![]</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//true</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    !!</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">''</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//false</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    !!</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">null</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//false</span></code></span></p></li></ol></pre><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">9.缓存变量</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box; color: rgb(0, 0, 0);">for循环缓存length</strong></p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> arr=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">3</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">4</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">5</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">6</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">]</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,i<arr.length/></span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//------------------------分割线</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> arr=[</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">1</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">2</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">3</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">4</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">5</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">6</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">]</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,len=arr.length;i<len/></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">第一段就是每一次循环的时候,都要查询一次arr.length。第二段代码就是缓存了arr.length,每次对比len就好,理论上是第二段代码的写法比较好,性能比较高!但是随着浏览器的发展,这个细节的性能上的影响貌似远远小于预期,现在还是建议缓存!我写了下面的测试用例(谷歌浏览器测试)!</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> arr100=[], arr10000=[];</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;i<</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">100</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;i++){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        arr100.push(i)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;i<</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">10000</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;i++){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        arr10000.push(i)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//缓存情况</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> testCache(arr){</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        console.time();</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,len=arr.length;i<len/></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"/></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        console.timeEnd()</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//不缓存情况</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> testNoCache(arr){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        console.time();</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,len=arr.length;i<len/></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"/></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        console.timeEnd()</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    testCache(arr100)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//default: 0.007ms</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    testCache(arr10000)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//default: 0.035ms</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    testNoCache(arr100)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//default: 0.012ms</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    testNoCache(arr10000)</span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//default: 0.109ms</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//这只是一个最简单的数组,如果遍历的是一个nodeList(元素列表),效果可能会更明显。</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box; color: rgb(0, 0, 0);">元素事件</strong></p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">这里我用jquery来讲解,比较容易理解,原生js也是这个道理!如下代码</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    $(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'.div1'</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">).click(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">       ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    })</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//--------------------------分割线   </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> $div1=$(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'.div1'</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    $div1.click(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">       ...</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    })</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">上面的代码,改变的也是缓存了$('.div1'),但是这里就建议是第二种写法了,因为第一种点击一次就要查询一次.div1,Dom的操作还是能减少就减少!</p><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">10.使用innerHTML添加元素</h4><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">比如有一个需求,往</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">`ul`</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">里面添加</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">10</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">个</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">`li`</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,两种方法,如下代码</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            <meta charset="</span"/><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"UTF-8"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">></span></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"><title/></span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"/></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            <ul id="</span"><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"ul-test"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">></span></ul></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"/></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        <script type="</span"><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"text/javascript"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> oUl=document.getElementById(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">"ul-test"</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//createElement方式</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            console.time();</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;i<</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">10</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;i++){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> oLi=document.createElement(</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'li'</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                oLi.innerHTML=i;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                oUl.appendChild(oLi);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            console.timeEnd();</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//innerHTML方式</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            console.time();</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> _html=</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">''</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">for</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> i=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;i<</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">10</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;i++){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                _html+=</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'<li>'</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">+i+</span><span class="str" style="box-sizing: border-box; color: rgb(255, 206, 84); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">'</li>'</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            oUl.innerHTML=_html;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            console.timeEnd();</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        </script></span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">大家把代码用浏览器打开,发现基本是第二种方式更快,第8点也说了,DOM操作能少就少!第一种要操作10次DOM,第二种只需要操作1次DOM。还有一个就是,这个只是很简单的li,如果是下面的列表呢?用第一种方式,得createElement多少次,innerHTML多少次,appendChild多少次?代码多,各个节点的逻辑和嵌套关系也乱!用第二种方式就是一个拼接字符串的操作,比第一种方式好多了,如果用es6的模板字符串,就更简单了!</p><p><img data-s="300,640" data-type="png" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_png/aVp1YC8UV0fFbbFg8cDsGbEge3kseVibQ3mY1U5pYOvem6RHG6P7OPNofm43OZVicvlHxtXDpLiaYYTEuNa443yrg/0?wx_fmt=png" class="" data-ratio="1.0756756756756756" data-w="370"/></p><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">11.将参数转成数组</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">函数里的arguments,虽然拥有length属性,但是arguments不是一个数组,是一个类数组,没有push,slice等方法。有些时候,需要把arguments转成数组,转的方法也不止一个,推荐的是是下面的写法!</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> _arguments=</span><span class="typ" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">Array</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">.prototype.slice.apply(arguments)</span></code></span></p></li></ol></pre><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">12.函数节流</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">这里拿一个栗子说,比如mousemove,onscroll,onresize这些事件触发的时候,可能已经触发了60次事件,这样很消耗性能,而且实际上,我们并不需要这么频繁的触发,只要大约100毫秒触发一次就好!那么这样就需要函数节流了! <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">普通写法</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> count = </span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> beginCount() {</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        count++;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        console.log(count);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    document.onmousemove = </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> () {</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">       beginCount();</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    };</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">效果</p><p><img data-type="gif" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_gif/aVp1YC8UV0fFbbFg8cDsGbEge3kseVibQeU5wM5DicpBPPyESiaxpqfy3PtLNk3WfqF4cwHS4N4cZvdhb9lCvNpNQ/0?wx_fmt=gif" class="" data-ratio="0.745" data-w="800"/></p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">节流写法</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> count = </span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> beginCount() {</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        count++;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        console.log(count);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> delayFn(method, thisArg) {</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        clearTimeout(method.props);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        method.props = setTimeout(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> () {</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">            method.call(thisArg)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        },</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">100</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    document.onmousemove = </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> () {</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        delayFn(beginCount)</span></code></span> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-1837452791782084" data-ad-slot="7041996284"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code class=" language- " style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    };</span></code></span></p></li></ol></pre><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">效果</p><p><img data-type="gif" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_gif/aVp1YC8UV0fFbbFg8cDsGbEge3kseVibQK0rXv6WXmTF43ZV6mOpKJa3toYLYKROKCswSu5sB7YiauOAU4xQvvWA/0?wx_fmt=gif" class="" data-ratio="0.745" data-w="800"/></p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">这种方式,其实是有问题的,在不断触发停下来等待100ms才开始执行,中间操作得太快直接无视。于是在网上找到下面这种方案!</p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">第二种节流写法</p><pre class="prettyprint linenums prettyprinted" style=" box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding: 8px 0px 6px; background-color: rgb(47, 54, 64); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; ; ; ; ; ; "><ol class="linenums list-paddingleft-2" style="list-style-type: none;"><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> delayFn2 (fn, delay, mustDelay){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">         </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> timer = </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">null</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">         </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> t_start;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">         </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">return</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> context = </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">this</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">, args = arguments, t_cur = +</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">new</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> </span><span class="typ" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">Date</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">();</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//先清理上一次的调用触发(上一次调用触发事件不执行)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             clearTimeout(timer);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//如果不存触发时间,那么当前的时间就是触发时间</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">if</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(!t_start){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                 t_start = t_cur;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//如果当前时间-触发时间大于最大的间隔时间(mustDelay),触发一次函数运行函数</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">if</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(t_cur - t_start >= mustDelay){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                 fn.apply(context, args);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                 t_start = t_cur;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//否则延迟执行</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">else</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> {</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                 timer = setTimeout(</span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">(){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                     fn.apply(context, args);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">                 }, delay);</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">             }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">         };</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    }</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">var</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> count=</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">0</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="kwd" style="box-sizing: border-box; color: rgb(79, 193, 233); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">function</span><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;"> fn1(){</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        count++;</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">        console.log(count)</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    } </span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    </span><span class="com" style="box-sizing: border-box; color: rgb(101, 109, 120); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">//100ms内连续触发的调用,后一个调用会把前一个调用的等待处理掉,但每隔200ms至少执行一次</span></code></span></p></li><li><p><span style="box-sizing: border-box; color: rgb(74, 74, 74); display: block; line-height: 22px; font-size: 14px !important; word-break: inherit !important;"><code style="box-sizing: border-box; margin-left: -20px; display: flex; overflow: initial; line-height: 12px; word-wrap: normal; border-width: 0px; border-style: initial; border-color: initial; font-size: 10px; font-family: inherit !important; white-space: pre !important;"><span class="pln" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">    document.onmousemove=delayFn2(fn1,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">100</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">,</span><span class="lit" style="box-sizing: border-box; color: rgb(172, 146, 236); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">200</span><span class="pun" style="box-sizing: border-box; color: rgb(230, 233, 237); line-height: 20px; font-size: 13px !important; white-space: inherit !important;">)</span></code></span></p></li></ol></pre><p><img data-type="gif" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_gif/aVp1YC8UV0fFbbFg8cDsGbEge3kseVibQVicKKTV8LFicjPThC3nnWqtPMulHf46k28k53n5ZKogI6liaiaZbcNZicMQ/0?wx_fmt=gif" class="" data-ratio="0.745" data-w="800"/></p><blockquote style=" box-sizing: border-box; margin-top: 1em; margin-bottom: 1.2em; padding: 15px 15px 15px 1rem; color: rgb(129, 145, 152); border-left-width: 6px; border-left-color: rgb(220, 230, 240); font-size: 14px; line-height: 22px; background: rgb(242, 247, 251); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif; white-space: normal; "><p style="box-sizing: border-box; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74);">我现在函数节流用得很少,这两个写法是比较基础的,希望大家能共享下自己的比较好的方法!</p></blockquote><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">13.其他写作建议</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">关于其它的一些写法技巧和建议,都是比较老生常谈的,比如命名规范,函数单一性原则等。这一部分内容我自己总结和别人写的基本一致!我就不展开说了(感觉展开说也基本是复制粘贴别人的文章,这事我不干),所以我推荐大家去看这篇文章(<span style="color: rgb(30, 107, 184);">如何优雅的编写 JavaScript 代码</span>)。有些知识我也是从这里获得的!</p><h4 style=" box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1rem; color: rgb(21, 153, 87); line-height: 1.35; font-size: 18px; white-space: normal; ; ; ; ; ; ; ; ; ; ; ; ; ">14.小结</h4><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">好了,关于我自己总结的一些实用技巧和建议,就到这里了!关于javascript的技巧和建议,这点大家还是要多看网上的资源,也要自己多总结,毕竟我自己总结的只是我自己发现的,只是冰山一角。但还是希望这篇文章能帮到大家,让大家学习到知识。当然,更希望的是能起到一个交流意见的作用。如果大家有什么建议,技巧。也欢迎分享。觉得我哪里写错了,写得不够好,也欢迎指出!让大家一起互相帮助,互相学习!</p><hr style="box-sizing: border-box; margin-top: 1.5rem; margin-bottom: 1.5rem; border-style: dashed none none; border-top-color: rgb(165, 165, 165); border-right-width: initial; border-right-color: initial; border-left-width: initial; border-left-color: initial; border-bottom-width: initial; border-bottom-color: initial; height: 1px; color: rgb(80, 97, 109); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif; font-size: 16px; white-space: normal;"/><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box; color: rgb(0, 0, 0);">相关文章推荐</strong></p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><a href="http://mp.weixin.qq.com/s?__biz=MjM5NTEwMTAwNg==&mid=2650211731&idx=2&sn=7ddd5ca205224763331beb29eac50c5b&chksm=befe05b289898ca4de2cd7b56937da90ec1e93180032bc3172bd60029ef317d19490d5a38201&scene=21#wechat_redirect" target="_blank"><span style="text-decoration: underline;">JavaScript实现[网易云音乐Web站登录窗口]拖拽功能</span></a></p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><a href="http://mp.weixin.qq.com/s?__biz=MjM5NTEwMTAwNg==&mid=2650211033&idx=1&sn=c522b16a2a05549268b6e72f2cf0e9e8&chksm=befe02f889898beee07b0d4bef4bfdf2b1794db27350b6b526f7bffc7b7687d7bc1e84fbf8f8&scene=21#wechat_redirect" target="_blank"><span style="text-decoration: underline;">编写自己的代码库(javascript 常用实例的实现与封装)</span></a></p><p style="box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; font-size: 16px; white-space: pre-line; line-height: 30px; color: rgb(74, 74, 74); font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><a href="http://mp.weixin.qq.com/s?__biz=MjM5NTEwMTAwNg==&mid=2650210975&idx=3&sn=b5ebfd0a95c80a78cfd71ecba2b3fc3c&chksm=befe02be89898ba885000783f7158c8b02015b94b8f7ad2cafbd6af7cecbdb862219c256eb96&scene=21#wechat_redirect" target="_blank"><span style="text-decoration: underline;">思路清奇:通过 JavaScript 获取移动设备的型号</span></a></p><p><img data-s="300,640" data-type="png" referrerpolicy="no-referrer" data- referrerpolicy="no-referrer" src="http://mmbiz.qpic.cn/mmbiz_png/aVp1YC8UV0fHw6ibf682ianNiagqMwKRqrzoVNjJzQ7oShmGdAs2r1nzawteP0QZFAzaibd4wu4rcvAa81WToxPjvg/0?wx_fmt=png" class="" data-ratio="0.4666666666666667" data-w="960"/></p> </div> </div> </div> </div> <div class="topic_buttons"> <div class="fr gray f11" style="line-height: 12px; padding-top: 3px; text-shadow: 0px 1px 0px #fff;"> </div> </div> </div> <div class="sep5"></div> <div class="box"> <div class="cell"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- jintian_wenzhi --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-1837452791782084" data-ad-slot="8042312523" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="sep5"></div> <div class="sep10"></div> <div class="box"> <div class="cell"> <span class="gray">推荐文章</span> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/sz_mmbiz_jpg/2A8tXicCG8yn42YRfR66iazDeHmRvhpPztqaoa7prlg4FZddf17ibIre7dLZLB0diaSO1kRh6S38wDOHzbtp4tPZpw/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>程序员的那些事</span>  ·  <a href="/t/!GFlY2h4NX!oMQ==">等了 13 年!微信能看到删除你的人:正在灰度测试中</a> </span> <div class="sep5"></div> <span class="fade">2 天前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/sz_mmbiz_jpg/2A8tXicCG8ymbTapdyekgpUpHl9fNKP4RRGsuDEO4K2icjbOaLeqzUnubuIKysmfUF2bGtYqv5LhlTQWicibWnnqQw/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>程序员的那些事</span>  ·  <a href="/t/WFJraG51Y3JZVQ==">趣图:好比成人玩积木还以为自己是天才</a> </span> <div class="sep5"></div> <span class="fade">4 天前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/sz_mmbiz_jpg/dkwuWwLoRK9oTe14MjGqLcxpgsiciaib3cicrfAYwTibicIuvBScohX2b0h7Zf26IrOn3RqhRRyTZQQKFtCAWtiaXTf8w/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>OSC开源社区</span>  ·  <a href="/t/STY1eDJvYTFMVQ==">“开源疯王”Matt Mullenweg正在摧毁WordPress</a> </span> <div class="sep5"></div> <span class="fade">1 周前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/CKvMdchsUwlYR9ficcrho92pnApwRLLcEt1zpXZOuUt3jSWGDPyqR1QTlXMwlnJmN9a1iaNhF91gtFrPstnAg41A/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>macrozheng</span>  ·  <a href="/t/RzRHekVxMmxrag==">大厂程序员提倡“防御性编程”:故意把代码写得很烂,万一自己被裁,要确保留下的代码不可维护!</a> </span> <div class="sep5"></div> <span class="fade">5 天前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/CKvMdchsUwlYR9ficcrho92pnApwRLLcEt1zpXZOuUt3jSWGDPyqR1QTlXMwlnJmN9a1iaNhF91gtFrPstnAg41A/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>macrozheng</span>  ·  <a href="/t/RzRHekVxMmxrag==">大厂程序员提倡“防御性编程”:故意把代码写得很烂,万一自己被裁,要确保留下的代码不可维护!</a> </span> <div class="sep5"></div> <span class="fade">5 天前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/sz_mmbiz_jpg/MQVibZibkKWNO3grgrqophArnib6ZKUd7CfUpJ9cxHqHQYveTTkflibPoxLnxmrBdb9emxDA8YgmJYkiciaHUjd8AEbw/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>51CTO官微</span>  ·  <a href="/t/R0hWTXJBNmx5!Q==">从数据仓库到数据中台再到数据飞轮:浅谈数据技术进化史</a> </span> <div class="sep5"></div> <span class="fade">1 周前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/O9F3NTo58yqVicnb4ItmDxJnJ2wxibo8iaUiaquCfZsk6BaDmTjWvknb4ozQFnK5uibpicmXy7JmSdoFPDo9dbs3GfibQ/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>新华网</span>  ·  <a href="/t/OWxw!WVUS2N2cA==">油价“三连涨”收官 全年汽油涨幅破千</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/gJfmK0YtTACF69NNRNhic8xtayEyI6LjaHdIoA5CS9Udg6gQQ0kUGojeLhJwGXLBJzN2Xh8ZsiabOjicjImh13y3g/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>尚榜</span>  ·  <a href="/t/M0VMMDczWlNybA==">【今日热点】邯郸一男子刺杀妻子、岳父后投案自首!家庭悲剧为何再次上演?</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/b2YlTLuGbKDibsF5T3PS1JBqZeOQGnw0uD91QjKzCpN2TAOQMAh67tSArOYkutCibYSOJcCVo30ANRa04QAfDsMw/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>虎嗅APP</span>  ·  <a href="/t/S3!IemRhS3FZ!Q==">《长城》败了,中美合拍这个题还要做下去吗?</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/krpsNAVgchhC5mpIR2iaxlb4IqkFcQic6ZHnKCoA7cZdPVphHf6RibcTc39zmBKBTydgpOGHKpZG9CVb55LKUNQUw/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>不贱不散</span>  ·  <a href="/t/V3VZZW1ZUVVP!w=="> 哥们儿!太悲催了!</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody> <tr> <td width="32" valign="top" align="center"> <img src="http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/LbGNn3xQbIIxQ699yicbAuMKjPicIL2V2e65nIWComaH1D37UzpqkpyUQCpyhcOkibTprtwG1H0w58w26QxtTK0yg/0?wx_fmt=jpeg" referrerpolicy="no-referrer" class="avatar" style="max-width: 32px; max-height: 32px;" border="0" align="default"> </td> <td width="10" valign="top"></td> <td width="auto" valign="top" align="left"> <span class="item_hot_topic_title"> <span>19楼</span>  ·  <a href="/t/SH!VNnhYM29KZA==">Plus 红色按钮将杭州送上热搜,怎么回事?</a> </span> <div class="sep5"></div> <span class="fade">7 年前</span> </td> </tr> </tbody> </table> </div> </div> <div class="sep5"></div> <div class="box"> <div class="cell"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- jintian_wenzhi --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-1837452791782084" data-ad-slot="8042312523" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="sep5"></div> </div> </div> <div class="c"></div> <div class="sep20"></div> </div> <div id="Bottom"> <div class="content footer"> <div class="inner"> <div class="sep10"></div> <div class="fr"> </div> <strong> <a href="http://www.sov5.org" class="dark" target="_blank">Sov5搜索</a>   <span class="snow">·</span>   <a href="http://baike.sov5.org" class="dark" target="_blank">小百科</a>   <span class="snow">·</span>   <a class="go-mobile" href="javascript:;">移动版</a> </strong> <div class="sep20"></div> 51好读 - 好文章就要读起来! </div> </div> </div> <script type="text/javascript" src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/9.2.0/highlight.min.js"></script> <script type="text/javascript" src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript" src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jqueryui/1.9.2/jquery-ui.min.js"></script> <script type="text/javascript" src="/static/js/vendors/jquery/jquery.autosize.js?v=ac4d62e3842f55aa2b78d2c2ef1af833"></script> <script type="text/javascript" src="/static/js/vendors/lscache/lscache.min.js?v=bf403ab76d287d394375662defac76c3"></script> <script type="text/javascript" src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/select2/3.0.0/select2.min.js"></script> <script type="text/javascript" src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery.selectboxit/3.8.0/jquery.selectBoxIt.min.js"></script> <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery.textcomplete/0.3.3/jquery.textcomplete.min.js" type="text/javascript"></script> <script type="text/javascript" src="/static/js/site/base/common.js?v=97646d06e4a04b2bf43b7b467cfd321e"></script> <script type="text/javascript" src="/static/js/site/base/v2ex.js?v=d41d8cd98f00b204e9800998ecf8427e"></script> <script type="text/javascript" src="/static/js/site/base/base.js?v=edcfd5298fe2acdc62d7fb498c373f99"></script> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-0SBZ564CXS"></script> <script> window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', 'G-0SBZ564CXS'); </script> <script> var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?55a854de13a9633b2937a3c24817fc7b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <!-- 多条广告如下脚本只需引入一次 --> <script type="text/javascript" src="//cpro.baidustatic.com/cpro/ui/cm.js" async="async" defer="defer"> </script> <script type="text/javascript" src="/static/js/site/post/post.js?v=e7935b685b5f2aef4de680492fd620cf"></script> <script type="text/javascript"> jQuery.browser = {}; (function () { jQuery.browser.msie = false; jQuery.browser.version = 0; if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) { jQuery.browser.msie = true; jQuery.browser.version = RegExp.$1; } })(); </script> <script type="text/javascript" src="/static/js/site/post/post_common.js?v=15968870dc6780d5664012d59b217894"></script> <script type="text/javascript" src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery-hashchange/1.3/jquery.ba-hashchange.min.js"></script> <script type="text/javascript" src="/static/js/site/post/post_folder.js?v=d3abab84fa5b19533d2131bf98e690f7"></script> </body> </html>