标签:each function 这一 dex map 产生 prototype 原理 title
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.length] = val; } } return newAry; }
标签:each function 这一 dex map 产生 prototype 原理 title
原文地址:http://www.cnblogs.com/aiyunyun/p/6323453.html