本文整理归纳了一些js常用函数,其中包括对数据操作,字符替换操作,日期及加解密操作函数,还有一些简单的验证函数。便于大家查阅参考。相信对大家会有所帮助。
1.typeof、constructor、instanceof对数组的判断
代码如下:
2.函数自调用
代码如下:
调用
})(10,20);3.去除两边空格
function trim (str) {
return str.replace(/^s+|s+$/g,'');
}
console.log(' woshi sss ');// woshi sss
console.log(trim(' woshi sss '));//woshi sss
4.数字判断
代码如下:
5.数组判断
代码如下:
6.length的理解
代码如下:
属性
7、base64_encode加密,解密
代码如下:
>2);
out+=base64EncodeChars.charAt((c1&0x3)<<4);
out+="==";
break;
}
c2=str.charCodeAt(i++);
if(i==len){
out+=base64EncodeChars.charAt(c1>>2);
out+=base64EncodeChars.charAt(((c1&0x3)<<4)|((c2&0xF0)>>4));
out+=base64EncodeChars.charAt((c2&0xF)<<2);
out+="=";
break;
}
c3=str.charCodeAt(i++);
out+=base64EncodeChars.charAt(c1>>2);
out+=base64EncodeChars.charAt(((c1&0x3)<<4)|((c2&0xF0)>>4));
out+=base64EncodeChars.charAt(((c2&0xF)<<2)|((c3&0xC0)>>6));
out+=base64EncodeChars.charAt(c3&0x3F);
}
return out;
}
function base64decode(str){
var c1,c3,c4,base64DecodeChars=new Array(-1,-1,62,63,52,53,54,55,56,57,58,59,60,61,1,2,3,4,7,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1);
var i,out;
len=str.length;
i=0;
out="";
while(i>4));
/* c3 */
do{
c3=str.charCodeAt(i++)&0xff;
if(c3==61) return out;
c3=base64DecodeChars[c3];
}while(i>2));
/* c4 */
do{
c4=str.charCodeAt(i++)&0xff;
if(c4==61) return out;
c4=base64DecodeChars[c4];
}while(i=0x0001)&&(c<=0x007F)){
out+=str.charAt(i);
} else if(c>0x07FF){
out+=String.fromCharCode(0xE0|((c>>12)&0x0F));
out+=String.fromCharCode(0x80|((c>>6)&0x3F));
out+=String.fromCharCode(0x80|((c>>0)&0x3F));
} else {
out+=String.fromCharCode(0xC0|((c>>6)&0x1F));
out+=String.fromCharCode(0x80|((c>>0)&0x3F));
}
}
return out;
}
function utf8to16(str){
var out,c;
var char2,char3;
out="";
len=str.length;
i=0;
while(i>4){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out+=str.charAt(i-1);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2=str.charCodeAt(i++);
out+=String.fromCharCode(((c&0x1F)<<6)|(char2&0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2=str.charCodeAt(i++);
char3=str.charCodeAt(i++);
out+=String.fromCharCode(((c&0x0F)<<12)|((char2&0x3F)<<6)|((char3&0x3F)<<0));
break;
}
}
return out;
}
8、日期时间
getDay函数:返回星期几,值为0~6,其中0表示星期日,1表示星期一,...,6表示星期六。例:
代码如下: