在没风的地方找太阳  在你冷的地方做暖阳 人事纷纷  你总太天真  往后的余生  我只要你 往后余生  风雪是你  平淡是你  清贫也是你 荣华是你  心底温柔是你  目光所致  也是你 想带你去看晴空万里  想大声告诉你我为你着迷 往事匆匆  你总会被感动  往后的余生  我只要你 往后余生  冬雪是你  春花是你  夏雨也是你 秋黄是你  四季冷暖是你  目光所致  也是你 往后余生  风雪是你  平淡是你  清贫也是你 荣华是你  心底温柔是你  目光所致  也是你
jQuery火箭图标返回顶部代码 - 站长素材

如何手动实现map(forEach以及filter也类似)

 

思路

map 迭代方法接收两个参数:

  • 对每一项执行的函数
    • 该函数接收三个参数:
      • 数组项的值
      • 数组项的下标
      • 数组对象本身
  • 指定 this 的作用域对象

map 方法返回每次函数调用结果组成的数组。

代码表示:

arr.map(function(item, index, arr) {}, this);

实现

由此,实现 fakeMap 方法如下

方法一:

Array.prototype.fakeMap = function fakeMap(fn, context) {
  if (typeof fn !== "function") {//判断fn是否能为函数,如果不是,跑出错误
    throw new TypeError(`${fn} is not a function`);
  }
  
  let arr = this;
  let temp = [];
  for (let i = 0; i < arr.length; i++) {
    // 迭代执行
    let result = fn.call(context, arr[i], i, arr);
    temp.push(result);
  }
  return temp;
};

检测:

let arr = ["x", "y", "z"];

arr.fakeMap((item, index, arr) => console.log(item, index, arr));

输出:

x 0 [ ‘x’, ‘y’, ‘z’ ]
y 1 [ ‘x’, ‘y’, ‘z’ ]
z 2 [ ‘x’, ‘y’, ‘z’ ]

this 指向:

let arr = ["x", "y", "z"];
let obj = { a: 1 };

arr.fakeMap(function() {
  console.log(this.a);
}, obj);

输出

1
1
1

更新

Array.prototype.myMap = function (fn, context) {
  const array = this
  context = Object(context) || global   // 严格模式下
  const resultArr = []
  
  for (let i = 0; i < array.length; i++) {
    let item = array[i]
    result = fn.call(context, item, i, array)
    resultArr.push(result)
  }
  return resultArr
};

注: 严格模式下,context 为 null 或 undefined 时 Object(context) 返回空对象,不会被赋值为global

 

方法二:  

Array.prototype.myMap = function () {
  var arr = this
  var [fn, thisValue] = Array.prototype.slice.call(arguments)
  var result = []
  for (var i = 0; i < arr.length; i++) {
    result.push(fn.call(thisValue, arr[i], i, arr))
  }
  return result
}
var arr0 = [1, 2, 3]
console.log(arr0.myMap(v => v + 1))

 

posted @ 2020-03-19 10:31  艺术诗人  阅读(627)  评论(0编辑  收藏  举报