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]
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!