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;
}

好吧,這兩個要領覺得都寫的很痴肥,往後才能提升了再修正吧。固然假如能提好的發起那就再好不過了。 ^_^

    原文作者:yuan
    原文地址: https://segmentfault.com/a/1190000014801334
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞