原型重写数组

83 阅读1分钟

高阶数组的方法重写

  Array.prototype.myForEach = function (callback) {
        //原型方法里的this是谁? 谁调用myForEach,this是谁
        for (var i = 0; i < this.length; i++) {
            callback(this[i], i, this);
        }
    }
    var arr = ["a", "b", "c", "d"];
    arr.myForEach(function (item, index, arr) {
        console.log(item, index, arr);
    });

    var list = [];
    Array.prototype.Mymap = function (callback) {
        for (var i = 0; i < this.length; i++) {
            var num = callback(this[i], i, this)
            list.push(num)
        }
        return list
    }
    var arr = [11, 22, 33, 44, 55];
    var res = arr.Mymap(function (item) {
        return item * 10;
    })
    console.log(res);

    console.log("---------------------------");
    //some:默认返回false,条件有一个满足,就返回true
    var arr = [11, 22, 33, 44, 55];
    Array.prototype.mySome = function (callback) {
        for (var i = 0; i < this.length; i++) {
            if (callback(this[i], i, this)) {
                return true;
            }
        }
        return false;
    }
    var res = arr.mySome(function (item) {
        return item > 30;
    })
    console.log(res);

    console.log("---------------------------");


    var arr = [11, 22, 33, 44, 55];
    Array.prototype.myEvery = function (callback) {
        for (var i = 0; i < this.length; i++) {
            if (!callback(this[i], i, this))
                return false;
        }
        return true;
    };
    var res = arr.myEvery(function (item) {
        return item > 10;
    })
    console.log(res);

    console.log("---------------------------");

    var list = [];
    Array.prototype.myFilter = function (callback) {
        for (var i = 0; i < this.length; i++) {
            if (callback(this[i], i, this)) {
                list.push(this[i]);
            }
        }
        return list;
    }
    var arr = [11, 22, 33, 44, 55];
    var res = arr.myFilter(function (item) {
        return item > 20;
    })
    console.log(res);

    console.log("---------------------------");

    Array.prototype.mySort = function (callback) {
        //冒泡排序
        for (var i = 0; i < this.length - 1; i++) {
            for (var j = 0; j < this.length - 1 - i; j++) {
                if (callback(this[j], this[j + 1]) > 0) {
                    var temp = this[j];
                    this[j] = this[j + 1];
                    this[j + 1] = temp;
                }
            }
        }
        return this;
    }
    var arr = [21, 11, 15, 17, 82, 12, 44];
    var res = arr.mySort(function (a, b) {
        return b - a;
    })
    console.log(res);

    console.log("---------------------------");

    Array.prototype.myReduce=function(callback,value){            
        var index;
        var pre;
        if(typeof value=='undefined'){
            index=1;
            pre=this[0];
        }else{
            index=0;
            pre=value;
        }
        for(index;index<this.length;index++){
            pre=callback(pre,this[index],index,this);
        }
        return pre;
    }
    var arr=[1,2,3,4];
    var res=arr.myReduce(function(pre,cur){
        return pre+cur;
    },20)
    console.log(res);