Array.prototype.myFilter = function(callback,thisArg){
if(this === undefined) {
throw new TypeError('this is null or not undefined')
}
if(typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function')
}
const res = [];
// 让O成为回调函数的对象传递(强制转换对象)
// this ==> arr
const O = Object(this)
const len = O.length >>> 0
// >>>0 保证len为number,且为正整数
// 检查i是否在O的属性(会检查原型链)
for(i in O) {
if(callback.call(thisArg,O[i],i,O)) {
res.push(O[i])
}
}
// 回调函数调用传参
return res;
}
let arr = [1,2,3,4,5]
console.log(arr.myFilter(v => v > 1))
console