js实现prototype扩展的方法(字符串,日期,数组扩展)
本文实例讲述了js实现prototype扩展的方法。分享给大家供大家参考,具体如下:
String.prototype.isEmpty=function(){return!(/.?[^/s]+/.test(this));}//检测字符串是否为空
//替换字符
String.prototype.reserve=function(type){
if(type=='int')returnthis.replace(/^/d/g,'');//替换字符串中除了数字以外的所有字符
elseif(type=='en')returnthis.replace(/[^A-Za-z]/g,'');//替换字符串中除了英文以外的所有字符
elseif(type=='cn')returnthis.replace(/[^/u4e00-/u9fa5/uf900-/ufa2d]/g,'');//替换字符串中除了中文以外的所有字符
elsereturnthis;
}
//字符串反转
String.prototype.reverse=function(){
returnthis.split('').reverse().join('');
}
//以一个中文算两个字符长度计算字符串的长度
String.prototype.cnLength=function(){returnthis.replace(/[^/x00-/xff]/g,'**').length;}
//替换字符串中的空格
String.prototype.trim=function(type,char){
vartype=type?type.toUpperCase():'';
switch(type){
case'B'://替换所有欲清除字符,未定义char则默认为替换空格
returnthis.replace(char?newRegExp(char,'g'):/(/s+|)/g,'');
case'O'://将两个以上的连续欲清除字符替换为一个,未定义char则默认为替换空格
returnchar?this.replace(newRegExp(char+'{2,}','g'),char):this.replace(/[/s]{2,}/g,'');
case'L'://替换除左边欲清除字符,未定义char则默认为替换空格
returnthis.replace(char?newRegExp('^('+char+')*','g'):/^(/s|)*/g,'');
case'R'://替换除右边欲清除字符,未定义char则默认为替换空格
returnthis.replace(char?newRegExp('('+char+')*$','g'):/(/s|)*$/g,'');
default://替换除左右两边欲清除字符,未定义char则默认为替换空格
returnthis.replace(char?newRegExp('^('+char+')*|('+char+')*$','g'):/(^/s*|)|(|/s*$)/g,'');
}
}
//判断字符串是否是数字
String.prototype.isNumer=function(flag){
if(isNaN(this)){returnfalse;}
switch(flag){
case'+':return/(^/+?|^/d?)/d*/.?/d+$/.test(this);//正数
case'-':return/^-/d*/.?/d+$/.test(this);//负数
case'i':return/(^-?|^/+?|/d)/d+$/.test(this);//整数
case'+i':return/(^/d+$)|(^/+?/d+$)/.test(this);//正整数
case'-i':return/^-/d+$/.test(this);//负整数
case'f':return/(^-?|^/+?|^/d?)/d*/./d+$/.test(this);//浮点数
case'+f':return/(^/+?|^/d?)/d*/./d+$/.test(this);//正浮点数
case'-f':return/^-/d*/./d$/.test(this);//负浮点数
default:returntrue;//缺省
}
}
//仿PHP的str_pad
String.prototype.pad=function(input,length,type){
if(!input)returnthis;
if(!length||length<1)varlength=1;
varinput=Array(length+1).join(input),value;
vartype=type?type.toUpperCase():'';
switch(type){
case'LEFT':returninput+this;
case'BOTH':returninput+this+input;
default:returnthis+input;
}
}
//获取url对应参数的值
String.prototype.getQuery=function(name){
varreg=newRegExp('(^|&)'+name+'=([^&]*)(&|$)');
varr=this.substr(this.indexOf('/?')+1).match(reg);
returnr[2]?unescape(r[2]):null;
}
//判断是否是日期格式(YYYY-MM-DDYYYY/MM/DDYYYY.MM.DD)
String.prototype.isDate=function(){
result=this.match(/^(/d{1,4})(-|//|.)(/d{1,2})/2(/d{1,2})$/);
if(!result)returnfalse;
vard=newDate(result[1],result[3]-1,result[4])
varstr=d.getFullYear()+result[2]+(d.getMonth()+1)+result[2]+d.getDate();
returnthis==str;
}
//将字符串转为日期
String.prototype.toDate=function(){
varmDate=newDate(Date.parse(str));
if(isNaN(mDate)){
vararr=this.split('-');
mDate=newDate(arr[0],arr[1],arr[2]);
}
returnmDate;
}
//格式化日期,newDate().format('yyyy/mm/dd')
Date.prototype.format=function(format){
varformat=format.toLowerCase();
vartype={
'm+':this.getMonth()+1,
'd+':this.getDate(),
'h+':this.getHours(),
'i+':this.getMinutes(),
's+':this.getSeconds(),
'q+':Math.floor((this.getMonth()+3)/3),
'ms':this.getMilliseconds()
}
if(/(y+)/.test(format))format=format.replace(RegExp.$1,(this.getFullYear()+'').substr(4-RegExp.$1.length));
for(varkintype){
if(newRegExp('('+k+')').test(format)){
format=format.replace(RegExp.$1,RegExp.$1.length==1?type[k]:('00'+type[k]).substr((''+type[k]).length));
}
}
returnformat;
}
//添加日期,对应参数分别是:类型(y-年,q-季,m-月,w-周,d-日,h-时,i-分,s-秒)和增加的值
Date.prototype.addDate=function(type,num){
vartype=type.toLowerCase();
switch(type){
case's':returnnewDate.parse(Date.parse(this)+(1000*num));
case'i':returnnewDate.parse(Date.parse(this)+(60000*num));
case'h':returnnewDate(Date.parse(this)+(3600000*num));
case'd':returnnewDate(Date.parse(this)+(86400000*num));
case'w':returnnewDate(Date.parse(this)+((86400000*7)*num));
case'm':returnnewDate(this.getFullYear(),(this.getMonth())+num,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
case'q':returnnewDate(this.getFullYear(),(this.getMonth())+num*3,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
case'y':returnnewDate((this.getFullYear()+num),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
}
}
//计算两个日期
Date.prototype.dateDiff=function(type,date){
if(typeofdate=='string')date=date.toDate();
switch(type){
case's':returnparseInt((date-this)/1000);
case'i':returnparseInt((date-this)/60000);
case'h':returnparseInt((date-this)/3600000);
case'd':returnparseInt((date-this)/86400000);
case'w':returnparseInt((date-this)/(86400000*7));
case'm':return(date.getMonth()+1)+((date.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1);
case'y':returndate.getFullYear()-this.getFullYear();
}
}
//判断对象是否是数组
Object.prototype.isArray=function(){returnObject.prototype.toString.apply(this)=='[objectArray]';}
//判断数组内是否存在指定的元素
Array.prototype.inArray=function(value){
if(this.length<2)returnthis[0]==value;
this.sort(function(a){
returnnewRegExp('^'+value).test(a)?-1:1;
});
returnthis[0]==value;
}
//在数组中查找元素并返回第一次出现的位置索引,未找到则返回-1。
Array.prototype.indexOf=function(string){
varlen=this.length,i=0;
if(len<2)returnthis[0]==value?0:-1;
for(i;i<len;i++){
if(this[i]==string)returni;
}
return-1;
}
//[1,2,3].each(function(x){returnx+1})得到2,3,4
Array.prototype.each=function(c){
varret=[];
for(vari=0;i<this.length;i++){
ret.push(c(this[i]));
}
returnret;
}
//[1,-1,2].any(function(x){returnx<0})判断是否数组中有一个元素小于0
Array.prototype.any=function(c){
for(vari=0;i<this.length;i++){
if(c(this))returntrue;
}
returnfalse;
}
//[1,2,3].all(function(x){returnx>0})判断是否数组中所有的元素都大于0
Array.prototype.all=function(c){
for(vari=0;i<this.length;i++){
if(!c(this))returnfalse;
}
returntrue;
}
//移除数组指定的元素,如果指定了limit,则仅移除limit个指定元素,如果省略limit或者其值为0,则所有指定元素都会被移除。
Array.prototype.unset=function(string,limit){
varlen=this.length,i=0,count=0;
for(i;i<len;i++){
if(this[i]==string){
this.splice(i,1);
if(limit&&limit>0){
count++;
if(count==limit)break;
}
}
}
returnthis;
}
//移除数组中重复的元素
Array.prototype.unique=function(){
vararr=tmp=[],i,len=this.length;
if(len<2)returnthis;
for(i=0;i<len;i++){
if(tmp[this[i]]){
arr.push(this[i]);
tmp[this[i]]=true;
}
}
returnarr;
}
Array.prototype.min=function(){returnMath.min.apply(null,this)}//求数组中最小值
Array.prototype.max=function(){returnMath.max.apply(null,this)}//求数组中最大值
更多关于JavaScript扩展相关内容感兴趣的读者可查看本站专题:《JavaScript扩展技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。