数组方法重构

一、mypop

// mypop
Array.prototype.myPop = function () {
    var temp = this[this.length - 1];
    this.length--
    return temp
}
var arr = [1, 2, 3, 4, 5];
console.log(arr);      //[ 1, 2, 3, 4, 5 ]
var result = arr.myPop();
console.log(result);    //5
console.log(arr)        //[ 1, 2, 3, 4 ]

二、myPush

// myPush
Array.prototype.myPush = function () {
    for (var i = 0; i < arguments.length; i++) {
        this[this.length] = arguments[i]
    }
    return this.length
}
var arr = [1, 2, 3, 4, 5];
console.log(arr);       //[ 1, 2, 3, 4, 5 ]
var result = arr.myPush('hello');
console.log(result);    //6
console.log(arr);       //[ 1, 2, 3, 4, 5, 'hello' ]

三、myShift

// myShift
Array.prototype.myShift = function () {
    var result = this[0];
    for (i = 0; i < this.length; i++) {
        this[i] = this[i + 1]
    }
    this.length--
    return result
}
var arr = [1, 2, 3, 4, 5];
console.log(arr);         //[ 1, 2, 3, 4, 5 ]
var result = arr.myShift();
console.log(result);       //1
console.log(arr);          //[ 2, 3, 4, 5 ]

四、myUnshift

// myUnshift
Array.prototype.myUnshift = function () {
    var sum = this.length + arguments.length;
    for (var i = sum; i > 0; i--) {
        if (i > arguments.length) {
            this[i - 1] = this[i - 1 - arguments.length]
        } else {
            this[i - 1] = arguments[i - 1]
        }
    }
    return sum
}
var arr = [1, 2, 3];
console.log(arr);      //[ 1, 2, 3 ]
var result = arr.myUnshift('hello', 'a', 'b');
console.log(result);   //6
console.log(arr);      //[ 'hello', 'a', 'b', 1, 2, 3 ]

五、myForEach

// myForEach
Array.prototype.myForEach = function (fun) {
    for (var i = 0; i < this.length; i++) {
        fun(this[i], i, this)
    }
}
var arr = [1, 2, 3];
console.log(arr);     //[ 1, 2, 3 ]
var result = arr.myForEach(function (item, index, arr) {
    console.log(item, index, arr)     //1 0 [ 1, 2, 3 ]
                                      //2 1 [ 1, 2, 3 ]
                                      //3 2 [ 1, 2, 3 ]
});

六、myEvery

// myEvery
Array.prototype.myEvery = function (fun, obj) {
    for (var i = 0; i < this.length; i++) {
        if (!(obj ? fun.bind(obj)(this[i]) : fun(this[i]))) {
            return false
        } //如果有第二个参数,需要修改this指向
        //if(!fun(this[i])){
        //    return false
        //} ---没有第二个参数的逻辑操作
    }
    return true
};
var arr = [1, 2, 3, 4, 5];
var result = arr.myEvery(function (item, index, arr) {
    console.log(this) //{ name: 'zhangsan' }
    return item > 1  //一项条件不满足 直接返回false
}, { name: 'zhangsan' }) //第二个参数可以是任意数据类型)
console.log(result)     //false

七、mySome

// mySome
Array.prototype.mySome = function (fun, obj) {
    for (var i = 0; i < this.length; i++) {
        if ((obj ? fun.bind(obj)(this[i]) : fun(this[i]))) {
            return true
        }
    }
    return false
};
var arr = [1, 2, 3, 4, 5];
var result = arr.mySome(function (item, index, arr) {
    console.log(this) //{ name: 'zhangsan' }{ name: 'zhangsan' }  有第二个参数,this指向第二个参数;这里this打印两次,首先没有执行条件前,打印了一次,后面数组项1不满足条件再打印一次,数组项2满足条件直接跳出判断不打印
    return item > 1  //一项满足 直接返回true
}, { name: 'zhangsan' }) //第二个参数可以是任意数据类型)

八、myMap

// myMap
Array.prototype.myMap = function (fun, obj) {
    var result = [];
    for (i = 0; i < this.length; i++) {
        //obj?test():test2()
        result.push(obj ? fun.bind(obj)(this[i]) : fun(this[i]))
    }
    return result
}
var arr = [1, 2, 3, 4, 5];
var result = arr.myMap(function (item, index, arr) {
    console.log(this)//没有第二个参数this指向全局对象node 里面是global html文档中指向window  //有第二个参数this指向第二个参数
    return item + 1
}, { name: 'zhangsan' })
console.log(result)

九、myFilter

// myFilter
Array.prototype.myFilter = function (fun, obj) {
    var result = [];
    for (i = 0; i < this.length; i++) {
        if (obj ? fun.bind(obj)(this[i]) : fun(this[i])) {
            result.push(this[i])
        }
    }
    return result
}

var arr = [1, 2, 3, 4, 5];
var result = arr.myFilter(function (item, index, arr) {
    console.log(this)//global  //obj //打印五次
    return item > 2
}, { name: 'zhangsan' })
console.log(result)     // [ 3, 4, 5 ]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值