Array.prototype.hjhpush=function(){for(const argumentsKey in arguments){// console.log(arguments[argumentsKey])// console.log(this.length)this[this.length]= arguments[argumentsKey]}returnthis}
pop() 方法用于删除并返回数组的最后一个元素
Array.prototype.hjhpop=function(){var arr =[],
len =this.length -1,
popResult =this[len];/*方法一 start */for(let i =0; i < len; i++){//深度拷贝原数组
arr.push(this[i]);}this.length =0;//利用length = 0, 把原数组清空for(let i =0; i < len; i++){this.push(arr[i]);}return popResult;//功能:返回踢掉}
pop2() 方法用于删除并返回数组的最后一个元素
Array.prototype.hjhpop2=function(){var result;if(this.length){//拿到最后一个值
result =this[this.length-1]//利用length删除最后一个值this.length--}return result
}
shift 删除并返回数组的第一个元素
Array.prototype.hjhshift=function(){var arr =[],
len =this.length,
result =this[0];for(let i =1; i <len ; i++){
arr.push(this[i])}this.length =0;for(let i =0; i <len-1; i++){this.push(arr[i])}return result
}
shift2 删除并返回数组的第一个元素
Array.prototype.hjhshift2=function(){var arr ,
len =this.length,
result =this[0];//slice 裁减一下
arr =this.slice(1);this.length =0;for(let i =0; i <len-1; i++){this.push(arr[i])}return result
}
unshift 向数组的开头添加一个或更多元素,并返回新的长度。
/**
* 思路:
* 保存参数并转为数组-->
* 保存原数组-->原数组清空-->
* 参数先push -->
* 原数组push-->
* 返回数组
* */
Array.prototype.hjhunshift=function(){//存储参数let arr =[];let len =this.length;let arglen = arguments.length;//存储原数组for(let i =0; i < len; i++){
arr.push(this[i])}this.length =0;//清空原数组//给原空数组添加参数for(let i =0; i < arglen; i++){this.push(arguments[i])}//在合并原数组for(let i =0; i < len; i++){this.push(arr[i])}returnthis}
unshift2 向数组的开头添加一个或更多元素,并返回新的长度。
/**
* unshift
* 思路:
将类数组转化成真正的数组,并连接现在的数组
* 清空原数组-->
* 循环写入数据即可
* 返回数组
* */
Array.prototype.hjhunshift2=function(){let arr = Array.prototype.slice.call(arguments).concat(this);let len = arr.length;this.length =0;for(let i =0; i <len ; i++){this.push(arr[i])}returnthis}
join 拼接数组
/**
* 思路:
* 默认连接符为‘,’如果传入其他字符则按照其他字符来连接,
* 在连接过程中剔除了undefined和null如果里面是字符的话
* 如果值不是原始值,则隐式调用tostring方法转化为字符串
* 返回数组
* */
Array.prototype.hjhjoin=function(){var targetStr = arguments[0],//那才参数值
str =',',//默认值为逗号
result =this[0],//获取起一个值
len =this.length;//获取长度//判断 targetStr 的值如果不存在str就是默认值,如果存在就判断是不是空的if(targetStr !== undefined){if(targetStr ===null){
str ='null'}else{
str = targetStr.toString();}}for(let i =1; i < len; i++){//剔除空和undefinedif(this[i]!== undefined &&this[i]!==null){
result +=(str +this[i]);}else{
result += str;}}return result;}
concat 数组连接生成新的数组
/**
* 思路:
* 创建新的数组,保存旧的数组写入数据
* */
Array.prototype.hjhconcat=function(){const arr =[],
len =this.length,
arlen = arguments.length;// 写入新数组for(let i =0; i < len; i++){
arr.push(this[i])}for(let i =0; i <arlen ; i++){//数组展开要推入,多为数组之展开第一层,如果里面数组或者对象修改时可能影响原来的值if(Object.prototype.toString.call(arguments[i])=='[object Array]'){let arrlen = arguments[i].length;for(let j =0; j <arrlen ; j++){
arr.push(arguments[i][j]);}}else{
arr.push(arguments[i])}}return arr
}
reverse 反转数组
/**
* 思路:
* 创建新的数组,保存旧的数组倒叙写入数据
* */
Array.prototype.hjhreverse=function(){let arr =[],
len =this.length
for(let i = len-1; i >=0; i--){
arr.push(this[i])}this.length =0;for(let i =0; i <arr.length ; i++){this.push(arr[i])}return arr
}
forEach 循环数组
/**
* 思路:
* 传入数组 循环调用回调函数即可,
* this 指向如果不存在则指向当前默认的this即可
* */
Array.prototype.hjhforEach=function(fn){var _arr =this,
len =this.length,//拿到第二个参数为this指向
params02 = arguments[1]|| window;//this函数执行时的指向(严格模式指向undefined怎么办?)for(let i =0; i <len ; i++){
fn.apply(params02,[_arr[i],i,_arr])}}
forEach源码是这样写的
Array.prototype.hjhforEach2=function(callback, thisArg){varT,K;if(this==null){thrownewTypeError('this is null or not defined')}//构造函数创建一个对象包装器varO=Object(this);//获取这个对象的长度var len =O.length >>>0;if(typeof callback !=="function"){thrownewTypeError(callback +' is not a function');}//当arguments.length > 1 T为 undefined 所以不改变this指向if(arguments.length >1){T= thisArg;}K=0;while(K<len){var kValue;//如果kif(KinO){//拿到其中一个值
kValue =O[K]//执行回调函数改变this指向
callback.call(T,kValue,K,O)}K++}}
filter(源码) 过滤函数返回符合条件的数据
Array.prototype.hjhfilter=function(func, thisArg){'use strict';// console.log(typeof func === 'Function',typeof func === 'function')if(!((typeof func ==='Function'||typeof func ==='function')&&this))thrownewTypeError();//拿到数组的长度var len =this.length >>>0,//创建一个等长的数组
res =newArray(len),// preallocate array//定义当前的this,
t =this, c =0, i =-1;// 如果thisArg 不存在证明不用改写this指向if(thisArg === undefined){//如果i+1 不等于len的长度 也就是数组从0开始 到数组的长度结束while(++i !== len){//判断 i在不在当前的数组的 范围内if(i inthis){//如果func(t[i], i, t) 根据返回值决定是否保存数据if(func(t[i], i, t)){
res[c++]= t[i];}}}}else{//如果i+1 不等于len的长度 也就是数组从0开始 到数组的长度结束while(++i !== len){//判断 i在不在当前的数组的 范围内if(i inthis){//当前需要改写this的指向如果func(t[i], i, t) 没有返回值则不保存数据 有返回值保存数据if(func.call(thisArg, t[i], i, t)){
res[c++]= t[i];}}}}
res.length = c;//修改长度return res;};
仿写 filter 认定有this的情况
Array.prototype.hjhfilter2=function(fn){var _arr =this,
len =this.length,
seif = arguments[1]
newarr =[];for(let i =0; i < len; i++){
fn.apply(seif,[_arr[i],i,_arr])?newarr.push(_arr[i]):''}return newarr
}
map 仿写 返回一个新的数组
Array.prototype.hjhmap=function(fn){var _arr =this,
len =this.length,
self = arguments[1],
newarr=[];for(let i =0; i < len; i++){
newarr.push(fn.apply(self,[_arr[i],i,_arr]))}return newarr
}
every 判断每个值是否符合回调函数的值
Array.prototype.hjhevery=function(fn){var _arr =this,
len =this.length,
self = arguments[1],
result =true;for(let i =0; i < len; i++){// console.log(fn.apply(self,[_arr[i],i,_arr]))if(!fn.apply(self,[_arr[i],i,_arr])){returnfalse}}return result
}
some 判断数组中是否存在,并返回结果
Array.prototype.hjhsome=function( fn){var _arr =this,
len =this.length,
self= arguments[1],
result =false;for(let i =0; i <len; i++){letas= fn.apply(self,[_arr[i], i, _arr])if(as){
result=truebreak}}return result
}
reduce 仿写
Array.prototype.hjhreduce=function(fn,initVal){var _arr =this,
len =this.length,
self = arguments[2],
res =initVal;for(let i =0; i <len ; i++){//本次调用的res 就是上一次结果的返回值
res =(fn.apply(self,[res,_arr[i], i, _arr]))}return res
}
reduceRight 仿写
Array.prototype.hjhreduceRight=function(fn,initVal ){var _arr =this,
len =this.length,
self =arguments[2],
res =initVal;for(let i = len-1; i >=0; i--){
res =(fn.apply(self,[res,_arr[i], i, _arr]))}return res
}