let arrCopywithin = ['a', 'b', 'c', , 'e']
console.log(arrCopywithin.copyWithin(0, -3, -2))
function arrayCopywhithin(arr, start, copyStart, copyEnd) {
if (!arr || arr.constructor !== Array) throw new TypeError(arr + 'is not an Array')
if (start === undefined) return arr
if (copyStart === undefined) copyStart = 0
if (copyEnd === undefined) copyEnd = arr.length
start = ~~start
copyStart = ~~copyStart
copyEnd = ~~copyEnd
if (start < 0) start = start + arr.length > 0 ? start + arr.length : 0
if (copyStart < 0) copyStart = copyStart + arr.length > 0 ? copyStart + arr.length : 0
if (copyEnd < 0) copyEnd = copyEnd + arr.length > 0 ? copyEnd + arr.length : 0
let arrCopy = []
for (let i = 0; i < arr.length; i++) {
i in arr ? arrCopy[arrCopy.length] = arr[i] : arrCopy.length++
}
let len = copyEnd - copyStart > arr.length - start ? arr.length - start : copyEnd - copyStart
for (let j = start; j < start + len; j++) {
let cpidx = copyStart + j - start
if (cpidx in arr) arr[j] = arrCopy[copyStart + j - start]
else {
arr[j] = null
delete arr[j]
}
}
return arr
}
let arrFill = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function arrayFill(arr, num, start, end) {
if (!arr || arr.constructor !== Array) throw new TypeError(arr + 'is not an Array')
if (end && typeof end !== 'number') return arr
if (arr.length === 0) return arr
if (start === undefined) start = 0
if (end === undefined) end = arr.length
start = ~~start
end = ~~end
if (start < 0) start = start + arr.length > 0 ? start + arr.length : 0
if (end < 0) end = end + arr.length > 0 ? end + arr.length : 0
for (let i = start; i < end; i++) {
if (i < arr.length) arr[i] = num
else break
}
return arr
}
console.log(arrayFill(arrFill, 0, 2, 15))
let arrForEach = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function arrayForEach(arr, fn, thisArg) {
if (!arr || arr.constructor !== Array) return new TypeError(arr + ' is not an Array')
if (!fn || typeof fn !== 'functio ') return new TypeError(fn + 'is not a function')
for (let i = 0; i < arr.length; i++) {
if (i in arr) fn(arr[i], i, arr)
}
}
arrayForEach(arrForEach , function(itm , index , arr){
console.log(itm , index)
return itm
})
try{
arrForEach.forEach(function (itm ,index ,arr){
if(itm === '要终止的条件') throw new Error( '报错')
console.log('此处写要执行的东西')
})
}catch(e){}