应该怎么修改代码呀?才能运行过去
解决问题
Array.prototype._filter = function(fn) {
if (typeof fn !== "function") {
throw Error('参数必须是一个函数');
}
const res = [];
for (let i = 0, len = this.length; i < len; i++) {
fn(this[i]) && res.push(this[i]);
}
return res;
}
- this.forEach(function(x) {
+ var self = this
+ this.forEach(function(x, index) {
- if (callback(x) == true) {
+ if (callback(x, index, self)) {
newArray.push(x);
}
})
13 回答13k 阅读
7 回答2.1k 阅读
3 回答1.3k 阅读✓ 已解决
6 回答1.2k 阅读✓ 已解决
2 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
6 回答1.1k 阅读
把接收的形参补充上去就可以了,图上的测试用例试了没问题的
