JS重写reverse、push、pop、unshift、shift

  1. reverse
//反转数组
Array.prototype.rev = function () {
    var later = this.length - 1,
        temp;
    for (let i = 0; i < this.length; i++) {
        temp = this[i];
        this[i] = this[later];
        this[later] = temp;
        if (i == later || later - i == 1) {
            break
        }
        later--;
    }
    return this
}

var arr = [8, 6, 3, 2, 9, 5, 7, 1, 4]
console.log(arr.rev())
  1. push
//push
Array.prototype.Push = function () {
    for (var i = 0; i < arguments.length; i++) {
        this[this.length] = arguments[i]

    }
    return this.length
}

var arr = [8, 6, 3, 2, 9, 5, 7, 1, 4]
console.log(arr.Push(555, 77))
console.log(arr)
  1. pop
//pop
Array.prototype.Pop = function () {
    var temp = this[this.length - 1]
    this.length = this.length - 1;
    return temp
}

var arr = [8, 6, 3, 2, 9, 5, 7, 1, 4]
console.log(arr.Pop())
console.log(arr)
  1. unshift
//unshift
Array.prototype.unShift = function () {
    for (let i = 0; i < arguments.length; i++) {
        var later = this.length
        for (let index = 0; index < this.length; index++) {
            this[later] = this[later - 1] //后一个值等于前一个值
            later--
        }
        this[0] = arguments[i]
    }
    return this.length
}

var arr = [8, 6, 3, 2, 9, 5, 7, 1, 4]
console.log(arr.unShift('ssss', 111))
console.log(arr)
  1. shift
//shift重写
Array.prototype.Shift = function () {
    var first = this[0];
    for (let index = 0; index < this.length; index++) {
        this[index] = this[index + 1] //前一个值等于后一个值
    }
    this.length--
    return first
}

var arr = [8, 6, 3, 2, 9, 5, 7, 1, 4]
console.log(arr.Shift())
console.log(arr)

你可能感兴趣的:(JS)