当前位置: 首页 > Web前端 > HTML

push、pop、slice、map、reduce在js中实现

时间:2023-03-28 00:26:28 HTML

pushmethodArray.prototype.push=function(...items){让O=Object(this);>>>0letargCount=items.length>>>0//2^53-1是最大的正整数if(len+argCount>2**53-1){thrownewTypeError("数组的个数是超过最大值")}for(leti=0;i>>0;如果(len===0){O.length=0;返回未定义;}长度——;设值=O[len];删除O[len];O.length=len;===空||this===undefined){thrownewTypeError("无法读取null的属性'map'");llbackfn)!="[objectFunction]"){thrownewTypeError(callbackfn+'isnotafunction')}letO=Object(this);让T=thisArg;letlen=O.length>>>0;让A=新数组(len);//赋值给新数组而不改变原数组的值for(letk=0;k>>0;letk=0;letaccumulator=initialValue;//reduce方法的第二个参数作为累加器的初始值if(accumulator===undefined){thrownewError('数组的每个元素都是空的');//处理不传递初始值for(;k