Array.prototype.map(func)方法的实现

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]

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.duanlonglong.com/qdjy/401.html