Array.prototype.forEach_ = function(fn,target=window) {
for(let i = 0; i<this.length;i++){
fn.call(target,this[i],i,this)
}
}
Array.prototype.map_ = function(fn,target=window) {
let res = []
for(let i = 0; i<this.length;i++){
res[i] = fn.call(target,this[i],i,this)
}
return res
}
Array.prototype.filter_ = function(fn){
let res = []
for (let i = 0; i < this.length; i++) {
if(fn(this[i])){
res.push(this[i])
}
}
return res
}
Array.prototype.every_ = function(fn,target=window){
let res = true
for (let i = 0; i < this.length; i++) {
if(!fn(this[i])){
res = false
}
}
return res
}
Array.prototype.some_ = function(fn,target=window){
let res = false
for (let i = 0; i < this.length; i++) {
if(fn(this[i])){
res = true
}
JS手写——数组常见方法的手写(forEach、map、filter、every、some、reduce、find、findIndex、fill、join、flat)
最新推荐文章于 2024-02-18 22:38:34 发布