var arr1 = [1,2,3,4,5];
alert(arr1.map(
function(element) {
return element * 2;
}
)); // 输出:[2,4,6,8,10]
function MyArray(arr) {
this.arr = arr;
}
MyArray.prototype.map = function(func) {
var newArr = [];
for (var i = 0; i < this.arr.length; i++)
newArr[i] = func(this.arr[i]);
return newArr;
};
var arr2 = new MyArray([1, 2, 3, 4, 5]);
alert(arr2.map(
function(element) {
return element * 2;
}
)); // 输出:[2,4,6,8,10]
浅谈Array.prototype.map(func)方法的实现
本文转载:CSDN博客