const arr = [1, 2, 3];
Array.prototype.customFilter = function(cb) {
if(typeof cb !== 'function') {
throw new TypeError(`${typeof cb} ${cb} must be a function!`);
}
const result = [];
for(let i = 0; i < this.length; i++) {
if(cb(this[i], i, this)) {
result.push(this[i]);
}
}
return result;
}
// console.log(arr.customFilter(item => < 3));
console.log(arr.filter(function(item) {return item < this.a}, {a: 3}));
Click Run to execute JavaScript.
Code runs in an isolated environment.
Code runs in an isolated environment.