重构数组方法需要注意的点
- 调用方式
- 参数,是否改变原数组
- 实现的功能
- 返回值
pop()方法
功能:删除数组的最后一个元素
返回值:删除的元素
参数:无
Array.prototype.mypop=function(){
if(this.length==''){
return undefined;
}
else{
var result=this[this.length-1];
this.length--;
return result;
}
}
var arr=[1,2,3,4,5];
console.log(arr);
var result=arr.mypop();
console.log(result);
console.log(arr);
push()方法
功能:向数组的末尾添加一个或多个元素
返回值:数组新长度
参数:item1, item2, …, itemx(必须。要添加到数组的元素)
Array.prototype.mypush=function(){
for(i=0;i<arguments.length;i++){
this[this.length]=arguments[i];
}
return this.length;
}
var arr=[1,2,3,4,5];
console.log(arr);
var result=arr.mypush('a','b','c');
console.log(result);
console.log(arr);
shift()方法
功能:删除数组的第一个元素
返回值:数组第一个元素的值
参数:无
Array.prototype.myshift=function(){
var result=this[0];
for(i=0;i<this.length;i++){
this[i]=this[i+1];
}
//数组长度减一即可实现删除最后一个元素
this.length--;
return result;
}
var arr=[1,2,3,4,5];
console.log(arr);
var result=arr.myshift();
console.log(result);
console.log(arr);
unshift()方法
功能:向数组的开头添加一个或更多元素
返回值:数组新长度
参数:item1,item2, …, itemx(可选。向数组起始位置添加一个或者多个元素)
Array.prototype.myunshift=function(){
//arguments为一个类数组对象,接收传递过来的参数
var sum=this.length+arguments.length;
//实现逻辑:把数组原来的元素往后移,再把传递过来的参数放到数组前面的空位
for(i=sum;i>0;i--){
if(i>arguments.length){
this[i-1]=this[i-1-arguments.length];
// 相当于this[5]=this[2];
}else{
this[i-1]=arguments[i-1];
}
}
return sum;
}
var arr=[1,2,3];
console.log(arr);
var result=arr.myunshift('a','b','c');
console.log(result);
console.log(arr);
forEach()方法
功能:调用数组的每个元素,并将元素传递给回调函数
返回值:undefined
参数:array.forEach(function(currentValue, index, arr), thisValue)
Array.prototype.myforEach=function(fun){
for(var i=0;i<this.length;i++){
fun(this[i],i,this);//执行传递过来的函数
}
}
var arr=[1,2,3];
var result=arr.myforEach(function(item,index,arr){
console.log(item,index,arr);
});
console.log(result);
every()方法
功能:检测数组所有元素是否都符合指定条件(通过函数提供)
返回值:布尔值,true/false
参数:array.every(function(currentValue,index,arr), thisValue)
Array.prototype.myevery=function(fun,obj){
for(i=0;i<this.length;i++){
//if里的条件为三目运算符,若存在第二参数则改变this的指向
if(!obj?fun.bind(obj)(this[i]):fun(this[i])){
return false;
}
}
return true;
}
var arr=[1,2,3,4,5];
var result=arr.myevery(function(item){
console.log(this);//this指向全局对象,若存在第二个参数则指向第二个参数
return item>1;//短路原则
},{name:'zhangsan'});//第二参数
console.log(result);
some()方法
功能:检测数组中的元素是否满足指定条件(函数提供)
返回值:布尔值,true/false
参数:array.some(function(currentValue,index,arr),thisValue)
Array.prototype.mysome=function(fun,obj){
for(i=0;i<this.length;i++){
if(!obj?fun.bind(obj)(this[i]):fun(this[i])){
return true;
}
}
return false;
}
var arr=[1,2,3,4,5];
var result=arr.mysome(function(item){
console.log(this);
return item>2;
},{name:'zhangsan'});
console.log(result);
map()方法
功能:返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值
返回值:返回一个新数组
参数:array.map(function(currentValue,index,arr), thisValue)
Array.prototype.mymap=function(fun,obj){
var result=[];
for(i=0;i<this.length;i++){
result.push(fun(this[i]));
}
return result;
}
var arr=[1,2,3,4,5];
var result=arr.mymap(function(item){
console.log(this);
return item+1;
},{name:'zhangsan'});
console.log(result);
filter()方法
功能:创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素
返回值:返回数组,包含了符合条件的所有元素
参数:array.filter(function(currentValue,index,arr), thisValue)
Array.prototype.myfilter=function(fun,obj){
var result=[];
for(i=0;i<this.length;i++){
if(obj?fun.bind(obj)(this[i]):fun(this[i])){
result.push(this[i]);
}
}
return result;
}
var arr=[1,2,3,4,5];
var result=arr.myfilter(function(item){
console.log(this);
return item>2;
},{name:'zhangsan'});
console.log(result);