标签:html .com 执行 style ack window this his https
原文链接:https://www.cnblogs.com/jocyci/p/5508279.html
var ary = [12,23,24,42,1]; var res = ary.forEach(function (item,index,input) { input[index] = item*10; }) console.log(res);//-->undefined; console.log(ary);//-->会对原来的数组产生改变;
var ary = [12,23,24,42,1]; var res = ary.map(function (item,index,input) { return item*10; }) console.log(res);//-->[120,230,240,420,10]; console.log(ary);//-->[12,23,24,42,1];
/** * forEach遍历数组 * @param callback [function] 回调函数; * @param context [object] 上下文; */ Array.prototype.myForEach = function myForEach(callback,context){ context = context || window; if(‘forEach‘ in Array.prototye) { this.forEach(callback,context); return; } //IE6-8下自己编写回调函数执行的逻辑 for(var i = 0,len = this.length; i < len;i++) { callback && callback.call(context,this[i],i,this); } }
/** * map遍历数组 * @param callback [function] 回调函数; * @param context [object] 上下文; */ Array.prototype.myMap = function myMap(callback,context){ context = context || window; if(‘map‘ in Array.prototye) { return this.map(callback,context); } //IE6-8下自己编写回调函数执行的逻辑 var newAry = []; for(var i = 0,len = this.length; i < len;i++) { if(typeof callback === ‘function‘) { var val = callback.call(context,this[i],i,this); newAry[newAry.i] = val; } } return newAry; }
JavaScript中的数组遍历forEach()与map()方法以及兼容写法
标签:html .com 执行 style ack window this his https
原文地址:http://www.cnblogs.com/kewenxin/p/7920521.html