forEach和map的区别,简单写了IE低版本的原形封装

今天有点'不务正业',旧的没有写完又开新的,没办法 -0- 今天遇到这个特感兴趣嘛
入正题了

forEach 和 map 的区别

参考:http://blog.csdn.net/boysky0015/article/details/72983766
作者;boysky0015

相同点:
.只能遍历数组
.都是循环遍历数组中的每一项和调用数组本身
.匿名函数中的this都是指向window
.ie6-8都不支持

不同点:
默认返回对象不同
map返回调用对象本身,forEach默认不返回

forEach

语法array.forEach(function(currentValue, index, arr), thisValue)
这里的 thisValue 是什么鬼,求大神告知
 1     //按菜鸟教程说
 2     //可选。传递给函数的值一般用 "this" 值。
 3     //如果这个参数为空, "undefined" 会传递给 "this" 值
 4 
 5     //按这么说 undefined 会传给this(window)
 6     //测试了 windowd 对象里面也没有
 7     var arr = [1,2,3,4,5];
 8     arr.forEach(function(currentValue,index,arr){
 9         console.log(currentValue + '---' + index + '---' + arr);
10         //1---0---1,2,3,4,5
11         //2---1---1,2,3,4,5
12         //3---2---1,2,3,4,5
13         //4---3---1,2,3,4,5
14         //5---4---1,2,3,4,5
15     });
16     console.log(arr);     //原数组对象值不变
17     for(key in window){
18         key.indexOf('un')>-1?console.log(key):false;
19     };
20 
21     //传参也不执行
22     var arr = [1,2,3,4,5],newArr = [];
23     arr.forEach(function(currentValue,index,arr){
24 
25     },function(){alert(1)}); //不执行

map

语法array.map(function(currentValue,index,arr), thisValue)
这里的 thisValue 是什么鬼,求大神告知
 1     //这就很奇怪了,怎么效果都是1样的
 2     var arr = [1,2,3,4,5];
 3     arr.map(function(currentValue,index,arr){
 4         console.log(currentValue + '---' + index + '---' + arr);
 5         //1---0---1,2,3,4,5
 6         //2---1---1,2,3,4,5
 7         //3---2---1,2,3,4,5
 8         //4---3---1,2,3,4,5
 9         //5---4---1,2,3,4,5
10     });
11     console.log(arr);     //原数组对象值不变

forEach 和 map 的区别

 1     var arr = [1,2,3,4,5];
 2     var newArr = arr.forEach(function(currentValue,index,arr){
 3        return currentValue += 2;
 4     });
 5     console.log(arr);       //[1, 2, 3, 4, 5]
 6     console.log(newArr);    //undefined
 7 
 8     var arr = [1,2,3,4,5];
 9     var newArr = arr.map(function(currentValue,index,arr){
10         return currentValue += 2;
11     });
12     console.log(arr);       //[1, 2, 3, 4, 5]
13     console.log(newArr);    //[3, 4, 5, 6, 7]
14 
15     //forEach reurn的方法
16     var arr = [1,2,3,4,5],newArr =[];
17     arr.forEach(function(currentValue,index,arr){
18         return newArr.push(currentValue += 2);
19     });
20     console.log(arr);       //[1, 2, 3, 4, 5]
21     console.log(newArr);    //undefined
22 
23     //map 有的时候还是挺方便的
24     var arr = [1,4,9];
25     var newArr = arr.map(Math.sqrt);
26     console.log(newArr)     //[1, 2, 3]

IE 6-8 封装原形上添加

 1     //forEach
 2 
 3     var arr = [1,2,3,4,5];
 4     Array.prototype.add_forEach = function add_forEach(val,i){
 5         for(var i=0;i<this.length;i++){
 6             val.call(window,this[i],i,this);
 7         }
 8     };
 9     arr.add_forEach(function(val,i,thisArr){
10         console.log(val + '--' + i + '--' + thisArr)
11     });
12 
13     //map
14     var arr = [1,2,3,4,5];
15     Array.prototype.add_map = function add_map(val,i,thisArr){
16         var newAry = [];
17         for(var i=0;i<this.length;i++){
18             if(typeof val === 'function') {
19                 val.call(window,this[i],i,this);
20                 newAry[newAry.length] = val;
21             }
22         };
23         return newAry;
24     };
25     arr.add_map(function(val,i,thisArr){
26         console.log(val + '--' + i + '--' + thisArr + this);
27     })
原文地址:https://www.cnblogs.com/me2o/p/7932377.html