详解JS中Array对象扩展与String对象扩展
废话不多说了,直接给大家上array对象扩展代码了,具体代码如下所示:
/** *Createdbylaixiangranon2016/01/07. *Array扩展 */ (function(){ //遍历数组 if(typeofArray.prototype.forEach!="function"){ Array.prototype.forEach=function(fn,context){ for(vari=0;i<this.length;i++){ if(typeoffn==="function"&&Object.prototype.hasOwnProperty.call(this,i)){ fn.call(context,this[i],i,this); } } }; } //让数组中的每一个元素调用给定的函数,然后把得到的结果放到新数组中返回 if(typeofArray.prototype.map!="function"){ Array.prototype.map=function(fn,context){ vararr=[]; if(typeoffn==="function"){ for(vark=0,length=this.length;k<length;k++){ arr.push(fn.call(context,this[k],k,this)); } } returnarr; }; } //把符合条件的元素放到一个新数组中返回 if(typeofArray.prototype.filter!="function"){ Array.prototype.filter=function(fn,context){ vararr=[]; if(typeoffn==="function"){ for(vark=0,length=this.length;k<length;k++){ fn.call(context,this[k],k,this)&&arr.push(this[k]); } } returnarr; }; } //如果数组中的每个元素都能通过给定的函数的测试,则返回true,反之false if(typeofArray.prototype.every!="function"){ Array.prototype.every=function(fn,context){ varpassed=true; if(typeoffn==="function"){ for(vark=0,length=this.length;k<length;k++){ if(passed===false)break; passed=!!fn.call(context,this[k],k,this); } } returnpassed; }; } //类似every函数,但只要有一个通过给定函数的测试就返回true if(typeofArray.prototype.some!="function"){ Array.prototype.some=function(fn,context){ varpassed=false; if(typeoffn==="function"){ for(vark=0,length=this.length;k<length;k++){ if(passed===true)break; passed=!!fn.call(context,this[k],k,this); } } returnpassed; }; } //返回元素在数组的索引,没有则返回-1,从左到右 if(typeofArray.prototype.indexOf!="function"){ Array.prototype.indexOf=function(item,index){ varn=this.length, i=index==null?0:index<0?Math.max(0,n+index):index; for(;i<n;i++){ if(iinthis&&this[i]===item){ returni } } return-1 }; } //返回元素在数组的索引,没有则返回-1,从右到左 if(typeofArray.prototype.lastIndexOf!="function"){ Array.prototype.lastIndexOf=function(item,index){ varn=this.length, i=index==null?n-1:index<0?Math.max(0,n+index):index; for(;i>=0;i--){ if(iinthis&&this[i]===item){ returni; } } return-1; }; } //让数组元素依次调用给定函数,最后返回一个值(从左到右) if(typeofArray.prototype.reduce!="function"){ Array.prototype.reduce=function(callback,initialValue){ varprevious=initialValue,k=0,length=this.length; if(typeofinitialValue==="undefined"){ previous=this[0]; k=1; } if(typeofcallback==="function"){ for(k;k<length;k++){ this.hasOwnProperty(k)&&(previous=callback(previous,this[k],k,this)); } } returnprevious; }; } //让数组元素依次调用给定函数,最后返回一个值(从右到左) if(typeofArray.prototype.reduceRight!="function"){ Array.prototype.reduceRight=function(callback,initialValue){ varlength=this.length,k=length-1,previous=initialValue; if(typeofinitialValue==="undefined"){ previous=this[length-1]; k--; } if(typeofcallback==="function"){ for(k;k>-1;k-=1){ this.hasOwnProperty(k)&&(previous=callback(previous,this[k],k,this)); } } returnprevious; }; } //去掉重复项(唯一性),返回新数组 if(typeofArray.prototype.uniq!="function"){ Array.prototype.uniq=function(){ vararr=[]; arr[0]=this[0]; for(vari=1;i<this.length;i++){ if(arr.indexOf(this[i])==-1){ arr.push(this[i]); } } returnarr; }; } //指定删除数组中某值 if(typeofArray.prototype.remove!="function"){ Array.prototype.remove=function(item){ for(vari=this.length;i>=0;i--){ if(item===this[i]){ this.splice(i,1); } } returnthis; }; } //打乱数组顺序 if(typeofArray.prototype.shuffle!="function"){ Array.prototype.shuffle=function(){ vari=this.length; while(i){ varj=Math.floor(Math.random()*i); vart=this[--i]; this[i]=this[j]; this[j]=t; } returnthis; }; } //求数组的最大值 if(typeofArray.prototype.max!="function"){ Array.prototype.max=function(){ returnMath.max.apply({},this) }; } //求数组的最小值 if(typeofArray.prototype.max!="function"){ Array.prototype.min=function(){ returnMath.min.apply({},this) }; } //判断是否为数组 if(typeofArray.prototype.isArray!="function"){ Array.prototype.isArray=function(){ returnObject.prototype.toString.apply(this)==="[objectArray]"; }; } }());
下面是string对象扩展代码如下所示:
/** *Createdbylaixiangranon2015/12/12. *String扩展 */ (function(){ //十六进制颜色值的正则表达式 varreg=/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; //RGB颜色转换为16进制 if(typeofString.prototype.rgbToHex!="function"){ String.prototype.rgbToHex=function(){ varthat=this; if(/^(rgb|RGB)/.test(that)){ varaColor=that.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(","); varstrHex="#"; for(vari=0;i<aColor.length;i++){ varhex=Number(aColor[i]).toString(16); if(hex==="0"){ hex+=hex; } strHex+=hex; } if(strHex.length!==7){ strHex=that; } returnstrHex; }elseif(reg.test(that)){ varaNum=that.replace(/#/,"").split(""); if(aNum.length===6){ returnthat; }elseif(aNum.length===3){ varnumHex="#"; for(varj=0;j<aNum.length;j++){ numHex+=(aNum[j]+aNum[j]); } returnnumHex; } }else{ returnthat; } }; } //16进制颜色转为RGB格式 if(typeofString.prototype.hexToRgb!="function"){ String.prototype.hexToRgb=function(){ varsColor=this.toLowerCase(); if(sColor&®.test(sColor)){ if(sColor.length===4){ varsColorNew="#"; for(vari=1;i<4;i++){ sColorNew+=sColor.slice(i,i+1).concat(sColor.slice(i,i+1)); } sColor=sColorNew; } //处理六位的颜色值 varsColorChange=[]; for(varj=1;j<7;j+=2){ sColorChange.push(parseInt("0x"+sColor.slice(j,j+2))); } return"RGB("+sColorChange.join(",")+")"; }else{ returnsColor; } }; } //移除字符串首尾空白 if(typeofString.prototype.trim!="function"){ String.prototype.trim=function(){ returnthis.replace(/^\s+|\s+$/g,""); }; } }());