随笔

String.prototype.change = function (type) {
switch (type) {
case "Date":
if (isNaN(this) && !isNaN(Date.parse(this))) {
return new Date(this);
} else {
console.log("时间格式错误!");
}
break;
case "Object":
if((this.indexOf("{")>=0&&this.indexOf("}")>=0) || (this.indexOf("[")>=0&&this.indexOf("]")>=0)){
return JSON.parse(this);
}else{
console.log("数组格式错误!");
}
break;
}
};
String.prototype.check=function (type) {
switch (type) {
case "money":
if(this.search(/^[0-9]+(.[0-9]{2})?$/g)==-1){
                console.log("数字格式错误,如有小数,只可两位小数!");
            }
break;
case "card":
if(this.search(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/g)==-1){
console.log("身份证校验失败!");
}
break;
}
}
Object.prototype.check=function () {
if(this&&typeof this==='object'){
if(typeof this.length==='number' && !(this.propertyIsEnumerable('length'))){
console.log("是数组!");
}else{
console.log("是对象!");
}
}
}
posted @ 2019-04-11 09:12  ୠZeroଙ  阅读(120)  评论(0编辑  收藏  举报