Array.prototype.mySplice = function (start, count, ...data) {
count = count === undefined ? this.length - start : (count < 0 ? 0 : count)
let delArr = []
let startIndex = start
if (start < 0) {
startIndex = (this.length + start) <= 0 ? 0 : this.length + start
}
if (start > this.length) {
startIndex = this.length
}
let end = startIndex + count
delArr = this.slice(start, end)
let right = this.slice(end)
let newIndex = startIndex
this.length = this.length - delArr.length + data.length
data.concat(right).forEach(item => {
this[newIndex] = item
newIndex++
})
return delArr
}
const arr1 = [1, 2, 3, 4]
const arr2 = [1, 2, 3, 4]
console.log(arr1.mySplice(1, 20), arr1)
console.log(arr2.splice(1, 20), arr2)
console