**题目:** 请编写一个方法 `mapArr`,实现与 ES5 规范中数组的 map 方法同样的功能 **测试用例:** ```js var data = [1, 2, 3, 4]; var result = data.mapArr(function(it, idx, arr) { console.log('当前项', it); console.log('当前索引', idx); console.log('数组', arr); return it * 2; }); console.log(result); // [2, 4, 6, 8] ``` ---- **参考答案:** ```js Array.prototype.mapArr = function(fn, context) { var arr = Array.prototype.slice.call(this); var mappedArr = []; for (var i = 0; i < arr.length; i++) { mappedArr.push(fn.call(context, arr[i], i, this)); } return mappedArr; } ```
Activity