map
Array.prototype.myMap=function(f){
let a=[];
for (let i=0; i<this.length; i++){ a.push(f(this[i]))
}
return a;
}
Array.prototype.myMap=function(){
if(arguments.length==1&& typeof arguments[0] = ="function") {var newarr=[];
for(var i=0; i<this.length; i++){arguments[0].call(window.this[i],i,this);
var new_num=arguments[0] (this[i],i,this); newarr.push(new_num); }}return newarr
}
Copy the code
reduce
Array.prototype.myReduce=function (f,value=0) {
for (let i =0; i<this.length; i++){ value=f(this[i],value)
}
return value
}
Copy the code
filter
Array.prototype.myFilter=function (f,value=0) {
let a=[]
for (let i =0; i<this.length; i++){let flag=f(this[i],value)
if (flag) a.push(this[i])
}
return a
}
Array.prototype.myFilter=function(){
if(arguments.length==1&&typeof arguments[0] = ="function") {var newarr=[];
for(var i=0; i<this.length; i++){arguments[0].call(window.this[i],i,this);
var flag=arguments[0] (this[i],i,this);
if(flag){
newarr.push(this[i]); }}}return newarr;
}
Copy the code
foreach
Array.prototype.myForEach=function (f) {
for (let i=0; i<this.length; i++){ f(this[i])
}
}
Array.prototype.myForEach=function(){
if(arguments.length==1&&typeof arguments[0] = ="function") {for(var i=0; i<this.length; i++){arguments[0].call(window.this[i],i,this)}}else if(arguments.length==2) {for(var i=0; i<this.length; i++){arguments[0].call(arguments[1].this[i],i,this); }}}Copy the code
some
Array.prototype.mySome=function (f) {
let flag=false;
for (let i=0; i<this.length; i++){ flag =f(this[i])
if (flag) return flag
}
return flag;
}
Array.prototype.mySome=function(){
if(arguments.length==1&&typeof arguments[0] = ="function") {for(var i=0; i<this.length; i++){arguments[0].call(window.this[i],i,this);
var flag=arguments[0] (this[i],i,this)
if(flag){
returnflag; }}}return false;
}
Copy the code
every
Array.prototype.myEvery=function (f) {
let flag=true
for (let i=0; i<this.length; i++){ flag =f(this[i])
if(! flag)return flag
}
return flag
}
Array.prototype.myEvery=function(){
var flag=true;
if(arguments.length==1&&typeof arguments[0] = ="function") {for(var i=0; i<this.length; i++){arguments[0].call(window.this[i],i,this)
flag=arguments[0] (this[i],i,this)
if(! flag){returnflag; }}}return flag;
}
Copy the code