自定义数组原型对象方法

430 阅读1分钟

自定义数组去重

 Array.prototype.unique = function () {
    let temp = [],
        len = this.length;
    for (let index = 0; index < len; index++) { 
        if(temp.indexOf(this[index]) == -1) { // 判断空数组是否存在该元素
            temp.push(this[index])
        }
    }
    return temp
}

自定义数组数字排序

Array.prototype.mysort = function () {
    let sortNumber = function (a,b) { // 排序函数
        return a - b
    }
    return this.sort(sortNumber)
}