一:会改变原数组的方法:不会返回新数组
1:push():像数组中最后一位添加一个或多个元素,返回新的长度
Array.prototype.myPush = function (...value) {
value.forEach((item) => {
this[this.length] = item
})
return this.length
}
let arr = [1, 2, 3]
arr.myPush(4, 5, 6)
2:pop():删除数组中的最后一个元素,返回该元素,有参数还是没参数都是一样
Array.prototype, myPop = function () {
if (!this.length) return undefined
let end = this[this.length - 1]
this[this.length - 1] = null
this.length = this.length - 1
return end
}
let arr1 = [1, 2, 3]
3:shift():删除数组的第一个元素,并返回该元素
Array.prototype.myshift = function () {
if (!this.length) return undefined
let start = this[0]
this[0] = null
this.length = this.length - 1
return start
}
4:unshift():向数组的开头添加一个或多个元素,返回数组长度
Array.prototype.myUnshift = function (...value) {
for (var i = (this.length + arguments.length - 1); i > arguments.length - 1; i--) {
this[i] = this[i - arguments.length]
}
for (var k = 0; k < arguments.length; k++) {
this[k] = arguments[k]
}
return this.length
}
5:reverse():颠倒数组中元素顺序
Array.prototype.myReverse = function(){
for(