Array.prototype.filter = function(fn,context = this){
if(typeof fn != 'function'){
throw new TypeError(`${fn} is not a function`)
}
let arr = this;
let reuslt = []
for(var i = 0;i < arr.length; i++){
const temp= fn.call(context,arr[i],i,arr);
if(temp){
result.push(arr[i]);
}
}
return result
}
Array.prototype.filter = function(fn, context = this){
if (typeof fn !== 'function') {
throw new TypeError(fn + 'is not a function');
}
return this.reduce((acc, value, index) => {
// fix稀疏数组的情况
if (index in list) {
const ret = fn.call(context, value, index, this);
if(ret) acc.push(ret)
}
return acc;
}, []); // 注意这里的[]不能省
}