js中Array方法重写(四):mySlice() ; mySplice()

一、mySlice()

//mySplice    选取数组的的一部分,并返回一个新数组
Array.prototype.mySlice = function(start,end){
    var arr = [];
    if(arguments.length == 0){           //如果不传参数,返回一个原数组副本
        start = 0;
        end = this.length;
    }else{                                //加工传进来start参数,使他符合循环要求
        start = Number(start);
        if(Number.isNaN(start)){
            start = 0;
        }else if(start < 0){
            if(start < -(this.length)){
                start = -this.length;
            }
            start = start + this.length;
        }    
    }
    if(arguments.length == 2){            //加工传进来end参数,使他符合循环要求
        end = Number(end);
        if(Number.isNaN(end)){
            end = this.length;
        }else if(end < 0){
            if(end < -(this.length)){
                end = -this.length;
            }
            end = end + this.length;
        }else if(end > this.length){
            end = this.length;
        }
    }
    if(end == undefined){                //如果没传end参数,默认设为数组长度
        end = this.length;
    }
    for(var i = Math.floor(start); i < Math.floor(end) ; i++){
        arr.myPush(this[i]);
    }
    return arr;
}

二、mySplice()

//mySplice        从数组中添加或删除元素
Array.prototype.mySplice = function(){
    var index,howmany;
    if(arguments.length == 0){
        this.length = 0;
        return this;
    }else if(arguments.length == 1){        //调整index和howmany的值
        index = arguments[0];
        if(index >= this.length){
            index = this.length;
        }else if(index < -this.length){
            index = 0;
        }else if(index < 0 && index >= -(this.length)){
            index += this.length;
        }
        howmany = this.length - index;
    }else if(arguments.length >= 2){        //调整index和howmany的值
        index = arguments[0];
        if(index >= this.length){
            index = this.length;
        }else if(index < -this.length){
            index = 0;
        }else if(index < 0 && index >= -(this.length)){
            index += this.length;
        }
        howmany = arguments[1];
        if(index+howmany >= this.length){
            howmany = this.length - index;
        }

    }
    var t1 = index;
    var length = arguments.length - 2;
    var arr = [];
    for(var i = 0 ; i < howmany ; i++){             // 返回删除的数组
        arr[i] = this[t1++];
    }
    var t2 = index;
    for(var i = t2 + howmany ; i < this.length ; i++){  //删除操作后的数组
        this[t2++] = this[i];
    }
    this.length = this.length - howmany;

    if(arguments.length > 2){                            //插入数
        var lastLength = this.length; 
        var leap = lastLength - index;
        var arIndexRigtht = arguments.length - 1;
        this.length = this.length + arguments.length - 2;
        for(var j = this.length - 1 ; j >= index ; j--){        
            if(leap > 0){
                this[j] = this[j - arguments.length + 2];
                leap = leap -1 ;
            }else{
                this[j] = arguments[arIndexRigtht--];
            }
        }
    }
    return arr;
}

好吧,这两个方法感觉都写的很臃肿,日后能力提升了再修改吧。当然如果能提好的建议那就再好不过了。 ^_^
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值