1. 模拟实现find
let arrFind = [12,15,19]
Array.prototype.myFind = function(fn) { //必须使用function不能使用箭头函数,this指向调用的数组
if(typeof fn != 'function'){
throw TypeError('err function')
}
for(let i = 0; i < this.length; i++){
if(fn(this[i])){
return this[i]
}
}
return undefined // 没有匹配则返回undefined
}
let find = arrFind.myFind(item => { // 返回第一个判断为true的值
return item > 12
})
console.log('find:'+find) // find:15
2. 模拟实现findIndex
let arrFindIndex = [12,15,19]
Array.prototype.myFindIndex = function(fn) { //必须使用function不能使用箭头函数,this指向调用的数组
if(typeof fn != 'function'){
throw TypeError('err function')
}
for(let i = 0; i < this.length; i++){
if(fn(this[i])){