Array.prototype.reduceFaker = function(callback, initialValue) {
if (this.length === 0 && initialValue === undefined) {
throw new TypeError('Reduce of empty array with no initial value');
}
let accumulator = initialValue !== undefined ? initialValue : this[0];
for (let i = initialValue !== undefined ? 0 : 1; i < this.length; i++) {
accumulator = callback(accumulator, this[i], i, this);
}
return accumulator;
};
Array.prototype.filter = function(fn, argThis) {
if(typeof fn !== 'function') {
throw Error(`${fn}不是一个函数`)
}
const filterRes = [];
for (let i = 0; i < this.length; i++) {
if (fn.call(argThis, this[i], i, this)) {
filterRes.push(this[i]);
}
}
return filterRes;
}
let arr = [1, 2, 3];
console.log(arr.reduceFaker((total, curValue) => Number(total) + Number(curValue), 1));
const filterFn = function (item){
console.log(this);
return item > 2;
}
// console.log(arr.filter({a: 2}, 2))
console.log(arr.filter(filterFn), 2)
// console.log(arr.filter((item) => item > 2, null))
console