Array.prototype.find2 = function (callback) {
if (this === null) {
throw new Error(this + 'is null or not defined')
}
if (typeof callback != 'function') {
throw new Error(`${callback} is not a function`)
}
for (var i = 0; i < this.length; i++) {
if (callback(this[i])) {
return this[i]
}
}
}
let arr = [1,2,3,4,5,6,7]
console.log(arr.find2(i => i== 2)) // 2
实现array对象的find()方法,也是比较常用的方法,自己写一下,可以理解实现原理,对于以后的使用也更有帮助