slice要领重写及罕见运用

数组的原型上有一个要领叫做slice,给差别的参数组合会返回差别的效果,因为组合异常多,这里研究一下差别组合的效果并基于原型链对该要领举行仿写
斟酌状况:

slice(a,b) slice(a) slice(undefined,b) slice()
a<b,a>=b,a为负数(0),b为负数(0),b超越有用长度了,a,b非有用数字

源码以下: 不想看源码的请直接看最下面的效果…

Array.prototype.mySlice = function (a, b) {
        var newArr = [];
        //处置惩罚没有参数的状况
        if (a === undefined && b === undefined) {
            return this
        }
        //处置惩罚只要一个参数,或者是第二个参数大于数组长度的状况
        if (b === undefined || b > this.length) {
            b = this.length
        }
        if (a === undefined) {
            a = 0
        }
        //处置惩罚参数不是数字的状况(上面代码已将undefined参数转化为非undefined参数而且保证参数为两个了)
        if (typeof a !== 'number'  || typeof b !== 'number' ) {
            console.log('参数必需是数字')
        }
        //处置惩罚第一个参数为负数以及第一个参数大与第二个参数以及第二个参数是负数的状况的状况

        if (b < 0) {
            b = this.length + b
        }
        if (a < 0) {
            a = this.length + a
        }
        //先将a,b恢复成正数再比较
        if (a >= b || b === 0) {
            return []
        }
        //一般 slice(a,b)
        for (var i = a; i < b; i++) {
            newArr.push(this[i])
        }
        return newArr
    }

斟酌到考证正确性,增添一个考证函数

function check(testArr, n, m) {
        console.log(`[${testArr}].slice(${n},${m})--->[${testArr.slice(n, m)}]`, testArr.slice(n, m).toString() === testArr.mySlice(n, m).toString());
    }
     
    check([1, 2, 3, 4, 5], 1, 3);
    check([1, 2, 3, 4, 5], 1);
    check([1, 2, 3, 4, 5], undefined, 3);
    check([1, 2, 3, 4, 5], 1, 7);
    check([1, 2, 3, 4, 5]);
    check([1, 2, 3, 4, 5], -1, 3);
    check([1, 2, 3, 4, 5], -3, 3);
    check([1, 2, 3, 4, 5], 1, 0);
    check([1, 2, 3, 4, 5], 1, -1);
    check([1, 2, 3, 4, 5], 1, -2);
    check([1, 2, 3, 4, 5], 1, -3);
    check([1, 2, 3, 4, 5], 3, 1);

效果以下:
《slice要领重写及罕见运用》

比较罕见的运用:

  1. 数组的复制

       arr.slice()
  2. 类数组转化为数组(dom类数组不兼容IE6,7,可以用ES6中arr.from()替代)

    Array.prototype.slice(类数组)
    
  3. 子数组的猎取
    3.1 一般的自数组猎取

       arr.slice(m,n) (0<m<n<arr.length)

    3.2 模仿屡次pop() shift()

       比方n次pop   arr = arr.slice(undefined,-n)
       比方n次shift   arr = arr.slice(n)
    
    原文作者:Runningfyy
    原文地址: https://segmentfault.com/a/1190000015805382
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞