重写concat方法
Array.prototype.myConcat = function(param){
var temp = [];
for(var i=0;i<this.length;i++){
temp.push(this[i]);
}
if(Array.isArray(param)){
for(var j=0;j<param.length;j++){
temp.push(param[j]);
}
}else{
temp.push(param);
}
return temp;
}
重写slice方法:
Array.prototype.mySlice = function(){
var newArr = [];
if(arguments[0]===(undefined||null)&&arguments[1]===undefined){
return this;
}
if(arguments[0]===(undefined||null)&&arguments[1]>this.length){
arguments[1]=this.length;
for(var i=0;i<arguments[1];i++){
newArr.push(this[i]);
}
}
if(arguments[0]<0){
arguments[0]=this.length+arguments[0];
}
if(arguments[1]<0){
arguments[1]=this.length+arguments[1];
}
if(arguments[0]<0){
arguments[0]=0;
}
if(arguments[0]!==null){
for(var i=arguments[0];i<(arguments[1]?arguments[1]:this.length);i++){
newArr.push(this[i]);
}
}
return newArr;
}
var arr = [1,2,3,4];
console.log(arr.mySlice(null,5));