1、实现push方法
let arr = [];
Array.prototype.push = function() {
for( let i = 0 ; i < arguments.length ; i++){
this[this.length] = arguments[i] ;
}
return this.length;
}
2、实现filter方法
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;
}
3、实现map方法
Array.prototype.map = function(fn) {
if (typeof fn !== "function") {
throw Error('参数是函数');
}
const res = [];
for (let i = 0, len = this.length; i < len; i++) {
res.push(fn(this[i]));
}
return res;
}
评论 (0)