SegmentFault (www.sf.gg)开发者社区,是中国年轻开发者喜爱的极客社区,我们为开发者提供最纯粹的技术交流和分享平台。 |
|
程序员的那些事 · 印度把 DeepSeek ... · 4 天前 |
|
程序员小灰 · 3个令人惊艳的DeepSeek项目,诞生了! · 3 天前 |
|
OSC开源社区 · 宇树王兴兴早年创业分享引围观 · 5 天前 |
|
OSC开源社区 · 2024: 大模型背景下知识图谱的理性回归 · 5 天前 |
|
程序猿 · “我真的受够了Ubuntu!” · 5 天前 |
从大学到现在,接触前端已经有几年了,感想方面,就是对于程序员而言,想要提高自己的技术水平和编写易于阅读和维护的代码,我觉得不能每天都是平庸的写代码,更要去推敲,去摸索和优化代码,总结当中的技巧,积极听取别人的建议,这样自己的技术水平会提高的更快。那么今天,我在这里就分享一下关于javascript方面的写作的实用技巧和建议,这些技巧和建议是我平常在开发项目上会用到的,希望能让大家学到知识,更希望能起到一个交流意见的作用,也就是说大家有什么好的技巧或者建议,欢迎分享,或者觉得我的想法存在什么问题,欢迎指出!
[...new Set([2,"12",2,12,1,2,1,6,12,13,6])]
//[2, "12", 12, 1, 6, 13]
//es6的新特性
关于对象的深浅拷贝,我个人见解就是有一下几点:
深拷贝和浅拷贝只针对像Object, Array这样的引用类型数据。
浅拷贝是对对象引用地址进行拷贝,并没有开辟新的栈,也就是拷贝后的结果是两个对象指向同一个引用地址,修改其中一个对象的属性,则另一个对象的属性也会改变。
深拷贝则是开启一个新的栈,两个对象对应两个不同的引用地址,修改一个对象的属性,不会改变另一个对象的属性。
浅拷贝
var myInfo={name:'守候',sex:'男'};
var newInfo=myInfo;
newInfo.sex='女';
console.log(myInfo) //{name: "守候", sex: "女"}
假-深拷贝
假-深拷贝这个是自己随性命名的,大家看看就好,别当真!
var myInfo={name:'守候',sex:'男'};
var newInfo=Object.assign({},myInfo)
newInfo.sex='女';
console.log(myInfo) //{name: "守候", sex: "男"}
console.log(newInfo) //{name: "守候", sex: "女"}
真-深拷贝
真-深拷贝这个是自己随性命名的,大家看看就好,别当真!
看着深浅拷贝,区别写法很简单,但是那个上面的深拷贝写法是有问题的。看下面案例
var arr=[{a:1,b:2},{a:3,b:4}]
var newArr=Object.assign([],arr)
//截断数组
newArr.length=1
console.log(newArr)//[{a:1,b:2}]
console.log(arr)//[{a:1,b:2},{a:3,b:4}]
//操作newArr,这里看着对arr没影响,实际上已经挖了一个坑,下面就跳进去
newArr[0].a=123
//修改newArr[0]这个对象,也是影响了arr[0]这个对象
console.log(arr[0])//{a: 123, b: 2}
为什么会这样呢,因为Object.assign并不是深拷贝,是披着深拷贝外衣的浅拷贝。最多也是Object.assign会课拷贝第一层的值,对于第一层的值都是深拷贝,而到第二层的时候就是 复制引用。类似的情况还有,slice方法和concat方法等。
要解决这个问题,就得自己封装方法!如下
//利用递归来实现深拷贝,如果对象属性的值是引用类型(Array,Object),那么对该属性进行深拷贝,直到遍历到属性的值是基本类型为止。
function deepClone(obj){
if(!obj&& typeof obj!== 'object'){
return;
}
var newObj= obj.constructor === Array ? [] : {};
for(var key in obj){
if(obj[key]){
if(obj[key] && typeof obj[key] === 'object'){
newObj[key] = obj[key].constructor === Array ? [] : {};
//递归
newObj[key] = deepClone(obj[key]);
}else{
newObj[key] = obj[key];
}
}
}
return newObj;
}
var arr=[{a: 1,b:2},{a:3,b:4}]
var newArr=deepClone(arr)
console.log(arr[0])//{a:1,b:2}
newArr[0].a=123
console.log(arr[0])//{a:1,b:2}
还有一个方法就是简单粗暴法,我现在在用的一个方法!原理很简单,就是先把对象转成字符串,再把字符串转成对象!也能实现同样效果
var newArr2=JSON.parse(JSON.stringify(arr));
console.log(arr[0])//{a:1,b:2}
newArr2[0].a=123
console.log(arr[0])//{a:1,b:2}
上面所说的浅拷贝,真假深拷贝(自己随性命名的),这几种情况,在开发上都有可能要用到,至于要使用哪一种方式,视情况而定!
一个简单的需求,比如想给ul下面的li加上点击事件,点击哪个li,就显示那个li的innerHTML。这个貌似很简单!代码如下!
charset="UTF-8">
</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></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></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></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></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></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></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></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></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></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></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></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(
|
程序员的那些事 · 印度把 DeepSeek 告上法庭!三哥的“既要又要”病又犯了? 4 天前 |
|
程序员小灰 · 3个令人惊艳的DeepSeek项目,诞生了! 3 天前 |
|
OSC开源社区 · 宇树王兴兴早年创业分享引围观 5 天前 |
|
OSC开源社区 · 2024: 大模型背景下知识图谱的理性回归 5 天前 |
|
程序猿 · “我真的受够了Ubuntu!” 5 天前 |
|
爱健身 · 一个DNA试验,炸出一朵八种混血的肉体... 7 年前 |
|
心理测试 · 结婚尽晚,离婚趁早 7 年前 |
|
凤凰网军事频道 · 北约高度关注!中国舰队赴俄进行中俄海军联演 7 年前 |
|
云无心是我 · 咖啡,速溶和现煮,有啥不一样 7 年前 |
|
金融行业网 · 罕见!三季度1238家上市公司抛售房产 7 年前 |